如何使用 C# 通过 Gmail 发送电子邮件
How to send an e-mail with C# through Gmail
我在尝试通过网络服务发送电子邮件时遇到错误。我尝试启用对安全性较低的应用程序的访问,禁用两步验证并通过网络浏览器登录帐户。 None SO 上的解决方案对我有用。我仍然得到:
Error: System.Net.Mail.SmtpException: The SMTP server requires a
secure connection or the client was not authenticated. The server
response was: 5.5.1 Authentication Required.
我该怎么做才能解决这个问题?
namespace EmailService
{
public class Service1 : IService1
{
public string SendEmail(string inputEmail, string subject, string body)
{
string returnString = "";
try
{
MailMessage email = new MailMessage();
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
// set up the Gmail server
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
// draft the email
MailAddress fromAddress = new MailAddress("cse445emailservice@gmail.com");
email.From = fromAddress;
email.To.Add(inputEmail);
email.Subject = body;
email.Body = body;
smtp.Send(email);
returnString = "Success! Please check your e-mail.";
}
catch(Exception ex)
{
returnString = "Error: " + ex.ToString();
}
return returnString;
}
}
}
只需转到此处:Less secure apps,使用用于在 C# 代码中发送邮件的电子邮件和密码登录,然后选择 Turn On
。
另请转到此 link 并单击 继续 Allow access to your Google account
我也稍微编辑了一下:
public string sendit(string ReciverMail)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("YourMail@gmail.com");
msg.To.Add(ReciverMail);
msg.Subject = "Hello world! " + DateTime.Now.ToString();
msg.Body = "hi to you ... :)";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("YourMail@gmail.com", "YourPassword");
client.Timeout = 20000;
try
{
client.Send(msg);
return "Mail has been successfully sent!";
}
catch (Exception ex)
{
return "Fail Has error" + ex.Message;
}
finally
{
msg.Dispose();
}
}
如果上面的代码不起作用,尝试像下面的代码那样改变它:
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("YourMail@gmail.com", "YourPassword");
我在尝试通过网络服务发送电子邮件时遇到错误。我尝试启用对安全性较低的应用程序的访问,禁用两步验证并通过网络浏览器登录帐户。 None SO 上的解决方案对我有用。我仍然得到:
Error: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
我该怎么做才能解决这个问题?
namespace EmailService
{
public class Service1 : IService1
{
public string SendEmail(string inputEmail, string subject, string body)
{
string returnString = "";
try
{
MailMessage email = new MailMessage();
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
// set up the Gmail server
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
// draft the email
MailAddress fromAddress = new MailAddress("cse445emailservice@gmail.com");
email.From = fromAddress;
email.To.Add(inputEmail);
email.Subject = body;
email.Body = body;
smtp.Send(email);
returnString = "Success! Please check your e-mail.";
}
catch(Exception ex)
{
returnString = "Error: " + ex.ToString();
}
return returnString;
}
}
}
只需转到此处:Less secure apps,使用用于在 C# 代码中发送邮件的电子邮件和密码登录,然后选择 Turn On
。
另请转到此 link 并单击 继续 Allow access to your Google account
我也稍微编辑了一下:
public string sendit(string ReciverMail)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("YourMail@gmail.com");
msg.To.Add(ReciverMail);
msg.Subject = "Hello world! " + DateTime.Now.ToString();
msg.Body = "hi to you ... :)";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("YourMail@gmail.com", "YourPassword");
client.Timeout = 20000;
try
{
client.Send(msg);
return "Mail has been successfully sent!";
}
catch (Exception ex)
{
return "Fail Has error" + ex.Message;
}
finally
{
msg.Dispose();
}
}
如果上面的代码不起作用,尝试像下面的代码那样改变它:
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("YourMail@gmail.com", "YourPassword");