5
I’m having the following mistake, working with SMTP
and MailMessage
in the C#:
The SMTP server requires a secure connection or the client has not been authenticated. The server response was: 5.5.1 Requested authentication Learn more on
Or in English
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
/// <summary>
/// Servidor de E-mail
/// </summary>
protected SmtpClient SmtpClient { get; set; }
/// <summary>
/// Conteudo da Mensagem
/// </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("[email protected]","senha");
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;
}
What I’m doing wrong, I got the message of the mistake.
I tried to change the e-mail
and the senha
, is the first time I’ve used SMTP I’m new at this. I know it’s not hard, just a lack of attention from me.
For now I’ve looked for some helps in other places, but I couldn’t find anything to help me, I hope you can help me.
You can do log in on Gmail on the same machine where you’re running this? I hope you haven’t posted your password for everyone to see.
– Maniero
I didn’t post no, where is "password" is something fictitious. I just logged in with the same email and password, and logged in normally. But during the execution of the project, he brings me this failure.
– Raffa Ferreira
When you loga on Gmail, does it ask for access confirmation? Does it indicate unusual usage? See this? https://security.google.com/settings/security/activity Have you tried to change this? https://www.google.com/settings/security/lesssecureapps
– Maniero
It shows me some access quests, Devices and PC’s !
– Raffa Ferreira