Sending email using Smtp.mail.microsoftonline.com

you can try this sample

      private void Button1_Click(System.Object sender, System.EventArgs e)
    {
        try
        {
            MailMessage myMessage = new MailMessage();
            SmtpClient myClient = new SmtpClient("yourserver");
            myClient.Port = "587";
            myClient.Host = "your server";
            myClient.UseDefaultCredentials = false;
            myClient.Credentials = new System.Net.NetworkCredential("username", "password");


            myMessage.From = new MailAddress("sender");
            myMessage.To.Add("recipient");
            myMessage.Subject = "Subject email";
            myMessage.Body = "body email";
            myClient.EnableSsl = true;
            myClient.Send(myMessage);
        }

        catch (Exepiton ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }

Bye


looking in Reflector on UseDefaultCredentials property, you can see that it also changes the trasnport.Credentials value, so when you called this property with a false value, it changed the transport credentials to null. the problem is that you called this property after setting the credentials in the line before that, it nullified the credentials.

so bottom line, you shouldn't set the credentials and call this property afterwise.

Tags:

C#

Email