I have been braining my brains for more than three hours trying everything I can think of to get the .NET4.5 SmtpClient to send a test email.
Things I have tried:
disabling windows firewall
disabling router firewall and enabling DMZ, biggest unknown
disabling antivirus
every possible permutation of port (25,26,465,587) and ssl (enabled/not) setting
watching the application code open a network connection in a connection viewer (the connection never gets past SYN_SENT)
the exception reported is sometimes "The operation has timed out." and sometimes "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond [ip.ad.re.ss]:587" depending on the settings used.
here is the code I am trying to get to work, it is being run in a Win7Pro IIS7 MVC 4 app:
string fromaddress = "address@gmail.com";
string toaddressstring = "recipient@gmail.com";
string subjecttext = "test";
string bodytext = "simple test email text";
System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
email.IsBodyHtml = true;
email.Body = "simple test email body text";
email.From = new System.Net.Mail.MailAddress(fromaddress);
email.To.Add(toaddressstring);
email.Subject = subjecttext;
System.Net.Mail.SmtpClient smtpcli = new System.Net.Mail.SmtpClient();
smtpcli.Host = "smtp.gmail.com";
smtpcli.Port = 587;
smtpcli.Timeout = 10000;
smtpcli.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpcli.EnableSsl = true;
smtpcli.UseDefaultCredentials = false;
smtpcli.Credentials = new NetworkCredential(fromaddress, "password");
smtpcli.Send(email);
I have totally run out of ideas, what else might be wrong? Any suggestions would be fantastic.
Seems that you cannot establish a TCP connection. I tested your code and is working well on my pc.
You can try troubleshooting checklist from google support page Problems sending mail with POP or IMAP
use this code and try to check in different ip from which ur using now.
var message = new MailMessage();
message.From = new MailAddress("from@gmail.com");
MailAddress to = new MailAddress("to@gmail.com");
message.Subject = "Subject";
string bodyString = "";
bodyString += "Hello";
message.Body = bodyString;
message.To.Add(to);
message.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.Credentials = new System.Net.NetworkCredential("user@gmail.com", "password");
smtp.EnableSsl = false;
smtp.Send(message);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.