我有以下程序使用“smtp.gmail.com:587”发送电子邮件
namespace TestMailServer
{
class Program
{
static void Main(string[] args)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("[email protected] /cdn-cgi/l/email-protection");
mail.To.Add("[email protected] /cdn-cgi/l/email-protection");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected] /cdn-cgi/l/email-protection", "myPassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Console.WriteLine("Send out");
}
}
}
[电子邮件受保护] /cdn-cgi/l/email-protection, [电子邮件受保护] /cdn-cgi/l/email-protection确实存在并且[电子邮件受保护] /cdn-cgi/l/email-protection的密码是 myPassword。为什么我收到以下错误:
未处理的异常:
System.Net.Mail.SmtpException:
SMTP 服务器需要安全
连接或客户端未连接
已验证。服务器响应
是:5.5.1 需要身份验证。
了解更多信息,请访问
System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode)
状态代码,字符串响应)位于
System.Net.Mail.MailCommand.Send(SmtpConnection
conn、Byte[] 命令、字符串来自)
在
System.Net.Mail.SmtpTransport.SendMail(邮件地址
发件人、邮件地址集合
收件人,字符串deliveryNotify,
SmtpFailedRecipientException&
例外)在
System.Net.Mail.SmtpClient.Send(MailMessage
消息)在
TestMailServer.Program.Main(字符串[]
args) 在 D:\Visual Studio 中
2010\Projects\TestMailServer\TestMailServer\Program.cs:行
26 按任意键继续。 。 。
我不确定是什么导致了你的问题。以下是我用来通过 Gmail 帐户成功发送电子邮件的一些代码:
const string from = "...";
var fromAddr = new MailAddress(from, "Bug Tracker");
var toAddr = new MailAddress("...@...", "...");
var client = new SmtpClient {
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Timeout = 30 * 1000,
Credentials = new NetworkCredential(fromAddr.Address, "...")
};
using (var msg = new MailMessage(fromAddr, toAddr)) {
msg.Subject = "...";
msg.Body = string.Format("username: {0}\nversion: {1}\n\n{2}", Environment.UserName, Assembly.GetExecutingAssembly().GetName().Version.ToString(3), cbtext);
client.Send(msg);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)