我正在使用 google smtp 发送电子邮件。该代码在我的本地机器中正常工作。但是在生产服务器上,我收到的错误消息是 SendEmail The SMTP server requires a secure connection or client was not authenticated。服务器响应是:5.5.1 需要身份验证。了解更多信息
我试过了,client.UseDefaultCredentials = true;将其设置为 false 但它不起作用。
尝试使用此代码通过 Google 发送邮件
MailMessage oMail = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string FromMailID = "yourgmailid@gmail.com";
string UserName = "GmailUsername";
string Password = "GmailPassword";
MailAddress fromAddress = new MailAddress(FromMailID);
if (FilePath != "")//If attached file otherwise comment
{
Attachment PDFfile = new Attachment(FilePath);
oMail.Attachments.Add(PDFfile);
}
oMail.From = fromAddress;
oMail.To.Add(ToMailID);
oMail.Subject = Subject;
oMail.IsBodyHtml = true;
oMail.Body = Mailcontent.ToString();
smtpClient.Host = "smtp.gmail.com"; // We use gmail as our smtp client
smtpClient.Port = 25;//localhost
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new System.Net.NetworkCredential(UserName, Password);
smtpClient.Send(oMail);
我是一名优秀的程序员,十分优秀!