0

I'm getting a little trouble when I try using SMTP and MailMessage in C#:

It's my first time using SMTP and MailMessage, so, it might be wrong some lines, but it is what I've done so far and I need help of you guys.

I have searched a lot of things about it and also here on stackoverflow, but so far, i can't think anymore what I'm doing wrong with it. When I try running (mode debugging and normal run) my Application Console (just tests on console, pretty simple) it gives me this 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

I understood the following error on Visual Studio 2013, the problem is because I don't know very well the properties and methods of the class Smtp and MailMessage, and then it makes me do not understand what I'm doing wrong on code,

Below, the class Email on my Console Application

    /// <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);

    }
}

I just ask around here, on stackoverflow when I can't search an answer of some my doubts anymore, so I need help. As it is my first time using SMTP and MailMessage....!

Why am I having this error ? Is it something wrong on code ? And What it is ?

  • Possible duplicate of [Sending email through Gmail SMTP server with C#](http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp) – Mike Hixson Oct 03 '15 at 06:19

2 Answers2

0

Try setting UseDefaultCredentials to false.

From the 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"

Ahhhhbisto
  • 339
  • 3
  • 13
0

I have just solved this problem. In google login and security, set "allow not safe application" option to true.

Alex M
  • 2,756
  • 7
  • 29
  • 35
haijun
  • 1