身份验证错误发送 SMTP 请求
Authentication error sending SMTP Request
我正在编写一个 C# 电子邮件应用程序,该应用程序目前设计用于 Gmail,因此我可以测试身份验证。但是,我收到一个似乎没有多大意义的错误,因为我将 SSL 设置为 true 并使用用户和密码。
错误信息:
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. Learn more at'
using System;
using System.Net;
using System.Net.Mail;
namespace EmailSendingProgram
{
public class EmailSendingClass
{
public static void Main(string[] args)
{
Console.WriteLine("Type your Gmail Address: ");
string from = Console.ReadLine();
Console.WriteLine("Type your Gmail Password: ");
string password = Console.ReadLine();
Console.WriteLine("Type the Email Address you wish to send to: ");
string to = Console.ReadLine();
Console.WriteLine("Type the Subject of the email: ");
string subject = Console.ReadLine();
Console.WriteLine("Type or paste in your email (either text or HTML): ");
string body = Console.ReadLine();
EmailSendingClass email = new EmailSendingClass();
email.Send(from, password, to, subject, body);
}
/// handles sending the email
/// hardcoded to work with gmail just change the CreateSmtpClient from
/// "smtp.gmail.com", 587 to whatever you want to use
public void Send(string from, string password, string to, string subject, string body)
{
var message = CreateMailMessage(from, to, subject, body);
// Host and Port setup for use with Gmail
using (SmtpClient client = CreateSmtpClient("smtp.gmail.com", 587, from, password))
{
client.Send(message);
}
}
/// Defines the SmtpClient for the mail message
/// setups up the network credentials to send the email
private SmtpClient CreateSmtpClient(string host, int port, string from, string password)
{
SmtpClient client = null;
client = new SmtpClient()
{
Host = host,
Port = port,
EnableSsl = true,
Credentials = new NetworkCredential(from, password)
};
return client;
}
/// defines what is contained in the email and where it will be sent
/// also makes all messages send as HTML
private MailMessage CreateMailMessage(string from, string to, string subject, string body)
{
MailMessage message = null;
message = new MailMessage(from, to)
{
Subject = subject,
Body = body
};
message.IsBodyHtml = true;
return message;
}
}
}
我知道身份验证有误,但我似乎无法弄清楚具体是什么。因此,我们将不胜感激。
我遇到了由 TLS 1.2 支持问题引起的类似问题。如果是这种情况,最简单的解决方案可能是面向 .NET 4.6.1 或更高版本。可以找到其他建议 here and here.
问题不是程序,而是 Gmail 拒绝从 "less secure apps"
登录
我只是点击了保护您的帐户按钮并允许此帐户从安全性较低的应用程序登录。
我正在编写一个 C# 电子邮件应用程序,该应用程序目前设计用于 Gmail,因此我可以测试身份验证。但是,我收到一个似乎没有多大意义的错误,因为我将 SSL 设置为 true 并使用用户和密码。
错误信息: 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. Learn more at'
using System;
using System.Net;
using System.Net.Mail;
namespace EmailSendingProgram
{
public class EmailSendingClass
{
public static void Main(string[] args)
{
Console.WriteLine("Type your Gmail Address: ");
string from = Console.ReadLine();
Console.WriteLine("Type your Gmail Password: ");
string password = Console.ReadLine();
Console.WriteLine("Type the Email Address you wish to send to: ");
string to = Console.ReadLine();
Console.WriteLine("Type the Subject of the email: ");
string subject = Console.ReadLine();
Console.WriteLine("Type or paste in your email (either text or HTML): ");
string body = Console.ReadLine();
EmailSendingClass email = new EmailSendingClass();
email.Send(from, password, to, subject, body);
}
/// handles sending the email
/// hardcoded to work with gmail just change the CreateSmtpClient from
/// "smtp.gmail.com", 587 to whatever you want to use
public void Send(string from, string password, string to, string subject, string body)
{
var message = CreateMailMessage(from, to, subject, body);
// Host and Port setup for use with Gmail
using (SmtpClient client = CreateSmtpClient("smtp.gmail.com", 587, from, password))
{
client.Send(message);
}
}
/// Defines the SmtpClient for the mail message
/// setups up the network credentials to send the email
private SmtpClient CreateSmtpClient(string host, int port, string from, string password)
{
SmtpClient client = null;
client = new SmtpClient()
{
Host = host,
Port = port,
EnableSsl = true,
Credentials = new NetworkCredential(from, password)
};
return client;
}
/// defines what is contained in the email and where it will be sent
/// also makes all messages send as HTML
private MailMessage CreateMailMessage(string from, string to, string subject, string body)
{
MailMessage message = null;
message = new MailMessage(from, to)
{
Subject = subject,
Body = body
};
message.IsBodyHtml = true;
return message;
}
}
}
我知道身份验证有误,但我似乎无法弄清楚具体是什么。因此,我们将不胜感激。
我遇到了由 TLS 1.2 支持问题引起的类似问题。如果是这种情况,最简单的解决方案可能是面向 .NET 4.6.1 或更高版本。可以找到其他建议 here and here.
问题不是程序,而是 Gmail 拒绝从 "less secure apps"
登录我只是点击了保护您的帐户按钮并允许此帐户从安全性较低的应用程序登录。