问题
I'm still getting "Failure sending mail." exception. The inner exception is "Unable to connect to the remote server" and the inner exception of that is "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". I'm pretty sure the cause of this is not the firewall setting. Does anyone know what I can do about it? Thanks.
var mail = new MailMessage("username@gmail.com", "destination@gmail.cz")
{
Subject = "Testing subject",
Body = "Testing body"
};
try
{
var client = new SmtpClient("smtp.google.com", 465)
{
EnableSsl = true,
Credentials = new NetworkCredential("username@gmail.com", "password")
};
client.Send(mail);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
回答1:
Here is the Google settings needed:
Incoming Mail (POP3) Server - requires SSL: pop.gmail.com
Use SSL: Yes
Port: 995
Outgoing Mail (SMTP) Server - requires TLS3 or SSL: smtp.gmail.com (use authentication)
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465
Account Name: your full email address (including @gmail.com or @your_domain.com)
Email Address: your email address (username@gmail.com or username@your_domain.com)
Password: your Gmail password
回答2:
I don't think 465 is the right port. Have you tried 587? And the SMTP server is smtp.gmail.com
.
回答3:
I am also working with Godaddy email server and below code worked fine for me:
Namespace:
System.Net.Mail
============
string senderID = "myemailID@mydomain.com";
string senderPassword = "123456";
string body = " Test email ";
MailMessage mail = new MailMessage();
mail.To.Add(username);
//mail.CC.Add(_cc);
mail.From = new MailAddress(senderID);
mail.Priority = MailPriority.High;
mail.Subject = "Test Email";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "relay-hosting.secureserver.net"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
(senderID, senderPassword); // ***use valid credentials***
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.Send(mail);
回答4:
If the hostname is correct and google supports SMTP over SSL, then it is being blocked by something and I would start checking firewalls.
回答5:
I don't think smtp.google.com is right server for gmail. Have you tried smtp.gmail.com on port 25?
Port 465 is for SMTP via SSL, which is not supported by the .NET SmtpClient. Instead, use port 25. The SMTP Client will use the STARTTLS feature to encrypt the communication.
回答6:
the correct port is 587 for smtpclient class
来源:https://stackoverflow.com/questions/7806944/failure-sending-mail-via-google-smtp