【发布时间】:2020-05-10 15:42:08
【问题描述】:
我在尝试发送电子邮件 (gmail) 时遇到问题,如果我没有打开“允许不太安全的应用程序”,它将不允许我发送电子邮件。我做错了什么?
try
{
smtpClient.Host = mServer;
smtpClient.Port = mPort;
smtpClient.EnableSsl = isenableSsl;
//Input new time out
if (mTimeout > 0)
{
smtpClient.Timeout = mTimeout;
}
//Check Authentication Mode
if (isAuthentication)
{
//Create Network Credentail if SMTP Server Turn On Authentication Mode
NetworkCredential credentials = new NetworkCredential();
credentials.UserName = mUserName;
credentials.Password = mPassword;
smtpClient.Credentials = credentials;
smtpClient.UseDefaultCredentials = false;
}
else
{
smtpClient.UseDefaultCredentials = true;
}
//Configuration Mail Information
if (string.IsNullOrEmpty(mDisplay)) mailMessage.From = new MailAddress(mFrom);
else mailMessage.From = new MailAddress(mFrom, mDisplay);
mailMessage.Sender = new MailAddress(mFrom);
mailMessage.ReplyTo = new MailAddress(mFrom);
//Set To Email Information
if (ToEmails.Count != 0)
{
mailMessage.To.Clear();
foreach (string mail in ToEmails)
{
mailMessage.To.Add(mail);
}
}
//Set Cc Email Information
mailMessage.CC.Clear();
foreach (string mail in CcEmails)
{
mailMessage.CC.Add(mail);
}
//Set Bcc Email Information
mailMessage.Bcc.Clear();
foreach (string mail in BccEmails)
{
mailMessage.Bcc.Add(mail);
}
//Set Mail Information
mailMessage.Subject = mSubject;
mailMessage.Body = mBody;
//Configuration Mail Option
mailMessage.IsBodyHtml = isBodyHtml;
mailMessage.SubjectEncoding = mSubjectEncoding;
mailMessage.BodyEncoding = mBodyEncoding;
mailMessage.Priority = mPriority;
mailMessage.DeliveryNotificationOptions = mDeliveryNotificationOptions;
//Clear Attachment File
mailMessage.Attachments.Clear();
//Add Attachments Internal File
AddAttachImage(ref mailMessage);
//Add Attachments External File
if (attachmentFiles.Count > 0)
{
AddAttachFile(ref mailMessage);
}
//Link Event Handler
smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
//Send Message Fuction
SetLogfileSendEmail(smtpClient, mailMessage);
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{ return true; };
smtpClient.Send(mailMessage);
}
catch (Exception ex)
{
strInformation.Append("(Error) Method smtpClient_SendCompleted : " + ex.Message);
WriteLogFile();
throw new Exception("SMTP Exception: " + ex.Message);
}
}
它也给了我这个
SMTP 异常:SMTP 服务器需要安全连接或客户端未通过身份验证。 服务器响应为:需要 5.7.0 身份验证。
我希望能够发送电子邮件,而不必在 gmail 帐户上打开“允许不太安全的应用程序”选项。
是否有任何其他方式让第三方应用发送电子邮件而无需“允许不太安全的应用”??
【问题讨论】:
-
我希望能够发送电子邮件,而不必在 gmail 帐户上打开“允许不太安全的应用程序”选项。抱歉我之前没说清楚。
标签: c# .net smtp smtpclient smtp-auth