这对您来说可能非常简单,但我无法弄清楚为什么我在运行代码时收到此错误消息。我查看了同一网站上的一些相关问题,例如Sending email through Gmail SMTP server with C# 但没有一个是有帮助的。有人愿意帮忙吗? 使用不同的组件也是可以接受的所以如果有人得到一个可以理解的工作解决方案。
错误消息= SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.5.1需要身份验证。
了解更多信息
这是我的代码
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new MailAddress("bob@googlemail.com");
message.To.Add("bob@hotmail.com");
message.Subject = "Hello";
message.Body = "Hello Bob ";
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("MyGoogleMailAccount",
"mygooglemailpassword");
smtpClient.Send(message.From.ToString(), message.To.ToString(),
message.Subject, message.Body);
答案 0 :(得分:10)
我认为您的代码除了电子邮件地址之外没有任何问题。我使用此代码成功地将电子邮件从gmail发送到我的个人帐户(实际上是在LINQPad中运行)。只需将3个字符串值替换为您帐户的有效值,您就应该选择:
MailMessage message = new System.Net.Mail.MailMessage();
string fromEmail = "myaddr@gmail.com";
string fromPW = "mypw";
string toEmail = "recipient@receiver.com";
message.From = new MailAddress(fromEmail);
message.To.Add(toEmail);
message.Subject = "Hello";
message.Body = "Hello Bob ";
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
using(SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential(fromEmail, fromPW);
smtpClient.Send(message.From.ToString(), message.To.ToString(),
message.Subject, message.Body);
}
答案 1 :(得分:0)
MailMessage mail = new MailMessage("you@yourcompany.com", "user@hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);