使用 GMAIL 的 SMTP 身份验证,错误 5.5.1
SMTP authentication using GMAIL, error 5.5.1
当我尝试在 C#:
我是第一次使用SMTP
和MailMessage
,所以有些行可能有误,但这是我到目前为止所做的,我需要你们的帮助。
我已经搜索了很多关于它的东西,也在 Whosebug 上搜索过,但到目前为止,我再也想不出我做错了什么。当我尝试 运行ning (模式调试和正常 运行) 我的 Application Console
(只是在控制台上测试,非常简单) 它给了我这个 error
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
我在Visual Studio 2013
上理解了下面的错误,问题是因为我不太了解class的属性和方法Smtp
和 MailMessage
,然后这让我不明白我在代码上做错了什么,
下面,我的控制台应用程序
上的 class Email
/// <summary>
/// Email Server
/// </summary>
protected SmtpClient SmtpClient { get; set; }
/// <summary>
/// Message Content
/// </summary>
protected MailMessage MailMessage { get; set; }
#endregion
/// <summary>
/// Método enviar e-mail
/// </summary>
/// <param name="smtp"></param>
/// <param name="from"></param>
/// <param name="to"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <param name="priority"></param>
public string EnviarEmail(string smtp, string from, string to, string subject, string body, bool priority)
{
try
{
SmtpClient = new SmtpClient();
SmtpClient.Host = "smtp.gmail.com";
SmtpClient.Port = 587;
SmtpClient.EnableSsl = true;
SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpClient.Credentials = new NetworkCredential("raffa.ferreiira@gmail.com","mypassword");
SmtpClient.UseDefaultCredentials = true;
MailMessage = new MailMessage();
MailMessage.From = new MailAddress(from, "Raffa Ferreira", Encoding.UTF8);
MailMessage.To.Add(new MailAddress(to, "Fulano teste", Encoding.UTF8));
MailMessage.Subject = subject;
MailMessage.Body = body;
MailMessage.BodyEncoding = Encoding.UTF8;
MailMessage.BodyEncoding = Encoding.GetEncoding("ISO-8859-1");
if (priority == false)
{
MailMessage.Priority = MailPriority.Normal;
}
else
{
MailMessage.Priority = MailPriority.High;
}
SmtpClient.Send(MailMessage);
}
catch(SmtpFailedRecipientException ex)
{
Console.WriteLine("Mensagem : {0} " + ex.Message);
}
catch(SmtpException ex)
{
Console.WriteLine("Mensagem SMPT Fail : {0} " + ex.Message);
}
catch(Exception ex)
{
Console.WriteLine("Mensagem Exception : {0} " + ex.Message);
}
string mensagem = "E-mail enviado";
return mensagem;
}
And on Program
I'm calling this class Email
passing some parameters:
class Program
{
static void Main(string[] args)
{
string smtp = "smtp.live.com";
string from = "raffa.ferreiira@gmail.com";
string to = "raffa.ferreiira@live.com";
string subject = "COMMUNICATE";
string body = "Hello, this is a text of a test message for you"
Email email1 = new Email();
email1.EnviarEmail(smtp, from, to, subject, body, true);
}
}
当我无法再搜索一些疑惑的答案时,我只是在这里问问,在 Whosebug
,所以我需要帮助。因为这是我第一次使用 SMTP
和 MailMessage
.....!
为什么我会遇到这个错误?代码有问题吗?它是什么?
尝试将 UseDefaultCredentials
设置为 false
。
来自MSDN article:
"Set this property to true when this SmtpClientobject should, if requested by the server, authenticate using the default credentials of the currently logged on user."
...
"If the UseDefaultCredentials property is set to false,then the value set in the Credentials property will be used"
我刚刚解决了这个问题。在 google 登录和安全中,将 "allow not safe application" 选项设置为 true。
当我尝试在 C#:
我是第一次使用SMTP
和MailMessage
,所以有些行可能有误,但这是我到目前为止所做的,我需要你们的帮助。
我已经搜索了很多关于它的东西,也在 Whosebug 上搜索过,但到目前为止,我再也想不出我做错了什么。当我尝试 运行ning (模式调试和正常 运行) 我的 Application Console
(只是在控制台上测试,非常简单) 它给了我这个 error
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
我在Visual Studio 2013
上理解了下面的错误,问题是因为我不太了解class的属性和方法Smtp
和 MailMessage
,然后这让我不明白我在代码上做错了什么,
下面,我的控制台应用程序
上的class Email
/// <summary>
/// Email Server
/// </summary>
protected SmtpClient SmtpClient { get; set; }
/// <summary>
/// Message Content
/// </summary>
protected MailMessage MailMessage { get; set; }
#endregion
/// <summary>
/// Método enviar e-mail
/// </summary>
/// <param name="smtp"></param>
/// <param name="from"></param>
/// <param name="to"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <param name="priority"></param>
public string EnviarEmail(string smtp, string from, string to, string subject, string body, bool priority)
{
try
{
SmtpClient = new SmtpClient();
SmtpClient.Host = "smtp.gmail.com";
SmtpClient.Port = 587;
SmtpClient.EnableSsl = true;
SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpClient.Credentials = new NetworkCredential("raffa.ferreiira@gmail.com","mypassword");
SmtpClient.UseDefaultCredentials = true;
MailMessage = new MailMessage();
MailMessage.From = new MailAddress(from, "Raffa Ferreira", Encoding.UTF8);
MailMessage.To.Add(new MailAddress(to, "Fulano teste", Encoding.UTF8));
MailMessage.Subject = subject;
MailMessage.Body = body;
MailMessage.BodyEncoding = Encoding.UTF8;
MailMessage.BodyEncoding = Encoding.GetEncoding("ISO-8859-1");
if (priority == false)
{
MailMessage.Priority = MailPriority.Normal;
}
else
{
MailMessage.Priority = MailPriority.High;
}
SmtpClient.Send(MailMessage);
}
catch(SmtpFailedRecipientException ex)
{
Console.WriteLine("Mensagem : {0} " + ex.Message);
}
catch(SmtpException ex)
{
Console.WriteLine("Mensagem SMPT Fail : {0} " + ex.Message);
}
catch(Exception ex)
{
Console.WriteLine("Mensagem Exception : {0} " + ex.Message);
}
string mensagem = "E-mail enviado";
return mensagem;
}
And on
Program
I'm calling thisclass Email
passing some parameters:
class Program
{
static void Main(string[] args)
{
string smtp = "smtp.live.com";
string from = "raffa.ferreiira@gmail.com";
string to = "raffa.ferreiira@live.com";
string subject = "COMMUNICATE";
string body = "Hello, this is a text of a test message for you"
Email email1 = new Email();
email1.EnviarEmail(smtp, from, to, subject, body, true);
}
}
当我无法再搜索一些疑惑的答案时,我只是在这里问问,在 Whosebug
,所以我需要帮助。因为这是我第一次使用 SMTP
和 MailMessage
.....!
为什么我会遇到这个错误?代码有问题吗?它是什么?
尝试将 UseDefaultCredentials
设置为 false
。
来自MSDN article:
"Set this property to true when this SmtpClientobject should, if requested by the server, authenticate using the default credentials of the currently logged on user."
...
"If the UseDefaultCredentials property is set to false,then the value set in the Credentials property will be used"
我刚刚解决了这个问题。在 google 登录和安全中,将 "allow not safe application" 选项设置为 true。