c#SmtpClient类无法使用gmail发送电子邮件
我使用gmail帐户发送电子邮件时遇到问题。我在拔头发 同样的设置在Thunderbird中也可以正常工作 这是密码。我也尝试了端口465,但没有成功c#SmtpClient类无法使用gmail发送电子邮件,c#,gmail,smtpclient,C#,Gmail,Smtpclient,我使用gmail帐户发送电子邮件时遇到问题。我在拔头发 同样的设置在Thunderbird中也可以正常工作 这是密码。我也尝试了端口465,但没有成功 SmtpClient ss = new SmtpClient("smtp.gmail.com", 587); ss.Credentials = new NetworkCredential("username", "pass"); ss.EnableSsl = true; ss.Timeout = 10000; ss.DeliveryMethod
SmtpClient ss = new SmtpClient("smtp.gmail.com", 587);
ss.Credentials = new NetworkCredential("username", "pass");
ss.EnableSsl = true;
ss.Timeout = 10000;
ss.DeliveryMethod = SmtpDeliveryMethod.Network;
ss.UseDefaultCredentials = false;
MailMessage mm = new MailMessage("donotreply@domain.com", "destination@domain.com", "subject here", "my body");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
ss.Send(mm);
这是错误
“SMTP服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:需要5.5.1身份验证。了解更多信息,请访问”
这是堆栈跟踪
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at email_example.Program.Main(String[] args) in C:\Users\Vince\Documents\Visual Studio 2008\Projects\email example\email example\Program.cs:line 23
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
在你的情况下,这意味着你必须用你登录谷歌的电子邮件地址发送它
所以可能有防火墙会干扰连接。我现在在测试代码时遇到了这个问题。试试建议的TELNET测试。你不会相信是什么解决了我的问题 凭据属性
ss.Credentials = new NetworkCredential("username", "pass");
必须在
ss.UseDefaultCredentials = false;
最后的工作代码清单是
SmtpClient ss = new SmtpClient("smtp.gmail.com", 587);
ss.EnableSsl = true;
ss.Timeout = 10000;
ss.DeliveryMethod = SmtpDeliveryMethod.Network;
ss.UseDefaultCredentials = false;
ss.Credentials = new NetworkCredential("username", "pass");
MailMessage mm = new MailMessage("donotreply@domain.com", "destination@domain.com", "subject here", "my body");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
ss.Send(mm);
这是一个错误吗?我可以验证在创建NetworkCredentials之前必须将UseDefaultCredentials设置为false。我也有同样的问题。只在端口25上工作。这可以工作,但性能不太友好。签出client.sendaync: 一个示例用例:
var message = new MailMessage("from", "to", "subject", "body");
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("login", "password"),
EnableSsl = true
};
client.SendCompleted += (s, e) =>
{
client.Dispose();
message.Dispose();
};
client.SendAsync(message, null);
在我的情况下效果很好:
private void btnTestConnection_Click(object sender, EventArgs e)
{
btnTestConnection.Enabled = false;
SmtpClient ss = new SmtpClient(txtSmtpHostName.Text.Trim(), Convert.ToInt32(numSmtpHostPort.Value));
ss.EnableSsl = chkSmtpSecureType.Checked;
ss.Timeout = 10000;
ss.DeliveryMethod = SmtpDeliveryMethod.Network;
ss.UseDefaultCredentials = false;
ss.Credentials = new NetworkCredential(txtSmtpAccount.Text.Trim(), txtSmtpPassword.Text);
MailMessage mm = new MailMessage(txtSmtpFromEmail.Text.Trim(), "test@domain.com", "subject", "my body");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
ss.SendCompleted += (s, b) =>
{
ss.Dispose();
mm.Dispose();
};
try
{
ss.Send(mm);
ss.Dispose();
mm.Dispose();
MessageBox.Show("Connection successfully");
}
catch (Exception ep)
{
MessageBox.Show("Connection error: " + ep.Message, "Smtp Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
btnTestConnection.Enabled = true;
}
这件工作做得很好。
在单独的文件MailTemplate.html
中创建邮件模板
添加正版网络凭据-登录名和密码
private void SendMail()
{
string filename = Server.MapPath("~/MailTemplate.html");
string username = UserName.Text.ToString();
string mailbody = System.IO.File.ReadAllText(filename);
mailbody = mailbody.Replace("##NAME##", username);
string to = Email.Text;
string from = "test@gmail.com";
MailMessage message = new MailMessage(from, to);
message.Subject = "Auto Response Email";
message.Body = mailbody;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("test@gmail.com", "test123#");
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Credentials = basicCredential;
try
{
client.Send(message);
SuccessMessage.Text = "Email Sending successfully";
}
catch (Exception ex)
{
ErrorMessage.Text = ex.Message.ToString();
}
}
MailTemplate.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Title</title>
</head>
<body>
<div style="border: thin solid #0066FF; width: 550px; margin: 25px auto; padding: 15px; font-family: 'Microsoft Himalaya'; font-size: x-large; font-style: normal; color: #0066FF; background-color: #EfEFF2;">
<br />
<p style="vertical-align: middle">Dear ##NAME##,</p>
</div>
</body>
</html>
标题
亲爱的姓名
我会说不,因为当我更改为UseDefaultCredentials=true时,我希望覆盖任何现有凭据。Kenny,您需要按正确的顺序声明属性似乎有点违反直觉。遇到相同的错误,在使用credentials属性之前,必须先设置UseDefaultCredentials。有点违反直觉,属性不应该依赖于order@VincePanuccio我用反射器检查了发生了什么。设置ss.UseDefaultCredentials=false实际上是将凭据重置为null。这就是你遇到麻烦的原因。但这也意味着ss.UseDefaultCredentials=false根本就不是必需的,它最终不会起任何作用。为什么在int上使用Convert.ToInt32?