【发布时间】:2017-02-17 04:17:01
【问题描述】:
我正在使用以下代码发送电子邮件。当我试图发送邮件时,我收到了错误消息
MailMessage mail = new MailMessage(from.txt, to.txt, subject, body);
SmtpClient clint = new SmtpClient();
//for determile email smtp...
string x = from.txt;
int startIndex = x.IndexOf('@');
int endIndex = x.LastIndexOf('.');
int length = endIndex - startIndex;
string xx = x.Substring(startIndex + 1, length - 1);
if (xx == "gmail" || xx == "Gmail")
{
clint.Host = "smtp.gmail.com";
clint.Port = 587;
clint.EnableSsl = true;
}
if (xx == "Hotmail" || xx == "hotmail" || xx == "live" || xx == "Live")
{
clint.Host = "smtp.live.com";
clint.Port = 587;
clint.EnableSsl = true;
}
if (xx == "yahoo" || xx == "Yahoo")
{
clint.Host = "smtp.mail.yahoo.com";
clint.Port = 465;
clint.EnableSsl = true;
}
clint.Credentials = new System.Net.NetworkCredential(username, password);
clint.DeliveryMethod = SmtpDeliveryMethod.Network;
clint.UseDefaultCredentials = false;
clint.Send(mail);
MetroMessageBox.Show(this, "Email Successfully Send", "Success",
MessageBoxButtons.OK, MessageBoxIcon.Information);
以及如何将任何文件附加到此电子邮件
【问题讨论】:
-
用户名应该是完整的电子邮件地址。你是吗?
-
启用双重身份验证(又称两步验证),然后生成应用程序专用密码。使用新生成的密码通过 SMTP 进行身份验证。
-
为什么不直接使用服务提供商允许您使用的 SMTP 服务器并使其更容易使用?
-
如果您使用 gmail 测试此代码,您将必须启用从 here 访问您的 google 帐户中不太安全的应用程序。还要在
clint.Credentials = new System.Net.NetworkCredential(username, password);之前设置clint.UseDefaultCredentials = false;。