使用 live/gmail smtp 发送电子邮件
Sending Email using live/gmail smtp
我正在使用 C# 通过 SMTP 和 gmail 服务器发送电子邮件。
下面是我用来发送电子邮件的代码。我遇到了一些错误,即
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
public static bool SendEmail(string from, string[] to, string[] cc, string[] bcc, string subject, string body, bool isBodyHtml, List<Attachment> attachmentList)
{
try
{
var mailMessage = new MailMessage();
var smtpClient = new SmtpClient();
mailMessage.From = new MailAddress(from);
mailMessage.To.Add(new MailAddress(string.Join(",", to)));
if (cc != null && cc.Any())
{
mailMessage.CC.Add(new MailAddress(string.Join(",", cc)));
}
if (bcc != null && bcc.Any())
{
mailMessage.Bcc.Add(new MailAddress(string.Join(",", bcc)));
}
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = isBodyHtml;
if (attachmentList != null)
{
foreach (var attachment in attachmentList)
{
mailMessage.Attachments.Add(attachment);
}
}
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587; //465
smtpClient.Credentials = new System.Net.NetworkCredential("username@email.com", "passsword");
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
我做错了什么以及如何使用 gmail 发送电子邮件。
如果您确定您的用户名和密码是正确的,但您仍然收到错误消息,那么这意味着 Gmail 已阻止您的应用程序。
尝试从这里打开它Grant Access to Less Secure Apps
您尚未将 UseDefaultCredentials
设置为 false
,因此尽管您提供了凭据,应用程序仍在尝试使用您的 windows 凭据登录 SMTP .尝试以下操作:
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("username@email.com", "passsword");
UseDefaultCredentials
需要设置为 false
,然后 设置新的网络凭据。
我正在使用 C# 通过 SMTP 和 gmail 服务器发送电子邮件。
下面是我用来发送电子邮件的代码。我遇到了一些错误,即
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
public static bool SendEmail(string from, string[] to, string[] cc, string[] bcc, string subject, string body, bool isBodyHtml, List<Attachment> attachmentList)
{
try
{
var mailMessage = new MailMessage();
var smtpClient = new SmtpClient();
mailMessage.From = new MailAddress(from);
mailMessage.To.Add(new MailAddress(string.Join(",", to)));
if (cc != null && cc.Any())
{
mailMessage.CC.Add(new MailAddress(string.Join(",", cc)));
}
if (bcc != null && bcc.Any())
{
mailMessage.Bcc.Add(new MailAddress(string.Join(",", bcc)));
}
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = isBodyHtml;
if (attachmentList != null)
{
foreach (var attachment in attachmentList)
{
mailMessage.Attachments.Add(attachment);
}
}
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587; //465
smtpClient.Credentials = new System.Net.NetworkCredential("username@email.com", "passsword");
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
我做错了什么以及如何使用 gmail 发送电子邮件。
如果您确定您的用户名和密码是正确的,但您仍然收到错误消息,那么这意味着 Gmail 已阻止您的应用程序。
尝试从这里打开它Grant Access to Less Secure Apps
您尚未将 UseDefaultCredentials
设置为 false
,因此尽管您提供了凭据,应用程序仍在尝试使用您的 windows 凭据登录 SMTP .尝试以下操作:
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("username@email.com", "passsword");
UseDefaultCredentials
需要设置为 false
,然后 设置新的网络凭据。