Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send SMTP email testing Microsoft Office 365 in .net

i have a mail account on the Exchange Online service. Now i'm trying to test if i am able to send mails to customers ( on varoius domains and on Microsoft Office 365) through c# application

I tried implementing the below code but i am getting the error

"The remote certificate is invalid according to the validation procedure."

MailMessage mail = null;                
mail = new MailMessage();

string[] strToList = "[email protected]"              
foreach (string strID in strToList)
{
    if (strID != null)
    {
        mail.To.Add(new MailAddress(strID));
    }
}

mail.From = "[email protected]";
mail.Subject = "testing"
mail.IsBodyHtml = true;
mail.Body = "mail body";

SmtpClient client = new SmtpClient("smtp.outlook.office365.com");
client.Port = 587;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
NetworkCredential cred = new System.Net.NetworkCredential("[email protected]", "mypassword");
client.Credentials = cred;
client.Send(mail);

Please advice if i am doing anything wrong. Thanks a lot in advance.

like image 849
user166013 Avatar asked Apr 09 '13 10:04

user166013


People also ask

Can I use Office 365 as an SMTP server?

Microsoft 365 or Office 365 SMTP relay uses a connector to authenticate the mail sent from your device or application. This authentication method allows Microsoft 365 or Office 365 to relay those messages to your own mailboxes and external recipients.


1 Answers

this works for me ( edited from source )


   ThreadPool.QueueUserWorkItem(t =>
            {
                SmtpClient client = new SmtpClient("smtp.office365.com",587);
                client.EnableSsl = true;
                client.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
                MailAddress from = new MailAddress("[email protected]", String.Empty, System.Text.Encoding.UTF8);
                MailAddress to = new MailAddress("[email protected]");
                MailMessage message = new MailMessage(from, to);
                message.Body = "The message I want to send.";
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.Subject = "The subject of the email";
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                // Set the method that is called back when the send operation ends.
                client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
                // The userState can be any object that allows your callback 
                // method to identify this send operation.
                // For this example, I am passing the message itself
                client.SendAsync(message, message);
            });

        private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // Get the message we sent
            MailMessage msg = (MailMessage)e.UserState;

            if (e.Cancelled)
            {
                // prompt user with "send cancelled" message 
            }
            if (e.Error != null)
            {
                // prompt user with error message 
            }
            else
            {
                // prompt user with message sent!
                // as we have the message object we can also display who the message
                // was sent to etc 
            }

            // finally dispose of the message
            if (msg != null)
                msg.Dispose();
        }
like image 160
Zakos Avatar answered Oct 06 '22 19:10

Zakos