Reputation: 9866
I'm trying out very simple tutorial on how to send e-mail via .NET
and C#
. However when I try to execute the code I got the following exception:
An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll Additional information: The operation has timed out.
So what I have done is to find the server settings to which I want to send mails to. This is very popular in my country:
Incoming settings
Protocol -> POP
Email address -> [email protected]
Username -> [email protected]
Password -> password
POP server -> pop3.abv.bg
Security type -> SSL
Server port -> 995
Outgoing server settings
Username -> [email protected]
Password -> password
SMTP server -> smtp.abv.bg
Security type -> SSL
Server port -> 465
then I created a Console project
and in my main method I have this code:
SmtpClient client = new SmtpClient("smtp.abv.bg");
client.Port = 465;
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(
"[email protected]", "password");
MailMessage msg = new MailMessage();
msg.To.Add("[email protected]");
msg.From = new MailAddress("[email protected]");
msg.Subject = "Test subject";
msg.Body = "Test test test...";
client.Send(msg);
Since I have no experience with this I just try the code as you see it. No additional settings anywhere. The only thing that I think should not be problem but I think worth mentioning that here:
client.Credentials = new NetworkCredential(
"[email protected]", "password");
and here:
msg.To.Add("[email protected]");
I'm using the same e-mail. But I think this shouldn't be a problem. Any idea what am I doing wrong here?
Upvotes: 0
Views: 35327
Reputation: 1647
Here is a working example (with Gmail) I wrote years ago for testing.
public void sendEmail(string body)
{
if (String.IsNullOrEmpty(email))
return;
try
{
MailMessage mail = new MailMessage();
mail.To.Add(email);
mail.To.Add("[email protected]");
mail.From = new MailAddress("[email protected]");
mail.Subject = "sub";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("[email protected]", "pw"); // ***use valid credentials***
smtp.Port = 587;
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
print("Exception in sendEmail:" + ex.Message);
}
}
I would try this function with Gmail just to eliminate network related issues like firewall. When that will work, the rest is just to find the right settigs for the SMPT server
Upvotes: 6