public static bool SendMail(string toList, string from, string ccList, string subject, string body)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
if (ccList != null && ccList != string.Empty)
message.CC.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
smtpClient.Timeout = 30000;
smtpClient.Host = "smtp.gmail.com"; // We use gmail as our smtp client
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword");
smtpClient.Send(message);
return true;
}
catch (Exception ex)
{
return false;
}
}
该代码似乎没问题,但仍然无法工作,我无法弄清楚为什么? 任何其他方式使用gmail的smtp发送邮件。使用Gmail发送邮件
我认为你应该尝试端口495(对于SSL)你在哪里得到587? –