I'm having the following error when working with SMTP
and MailMessage
in C #:
The SMTP server requires a secure connection or the client was not authenticated. The response from the server was: 5.5.1 Requested Authentication Learn more at
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 understood the error message.
I've tried changing e-mail
and senha
, this is my first time using SMTP . I know it's not difficult, just a lack of attention from me.
I've been looking for some helps elsewhere, but I did not find anything to help me, I hope you can help me.