我正在使用以下代码发送电子邮件。当我要发送邮件时,我收到错误消息
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);
以及如何将任何文件附加到此电子邮件
答案 0 :(得分:0)
对于你得到的错误,我在线阅读你应该在Credentials行之前使用UseDefaultCredentials,并且你的clint对象的EnableSsl应该设置为true。
以及如何将任何文件附加到此电子邮件
您可以通过向邮件对象添加附件:
mail.Attachments.Add(new Attachment(filename));
我忘了提及Gmail中的双重身份验证可能是个问题。如果是,请参阅我之前链接的链接中的第二个解决方案。