0% found this document useful (0 votes)
51 views1 page

Office 365 Code For Mail Sending

This document shows code for asynchronously sending an email using SmtpClient in .NET. It creates a SmtpClient connected to Office 365, sets the credentials and enables SSL. A MailMessage is constructed with sender, recipient, subject and body. SendAsync is called, passing the message as user state. A callback is defined to handle completion, checking for errors or cancellation and disposing the message.

Uploaded by

pavan4india8381
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views1 page

Office 365 Code For Mail Sending

This document shows code for asynchronously sending an email using SmtpClient in .NET. It creates a SmtpClient connected to Office 365, sets the credentials and enables SSL. A MailMessage is constructed with sender, recipient, subject and body. SendAsync is called, passing the message as user state. A callback is defined to handle completion, checking for errors or cancellation and disposing the message.

Uploaded by

pavan4india8381
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

ThreadPool.QueueUserWorkItem(t => { SmtpClient client = new SmtpClient("smtp.office365.com",587); client.EnableSsl = true; client.Credentials = new System.Net.NetworkCredential("xxx@yyy.

c om", "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 en ds. client.SendCompleted += new SendCompletedEventHandler(SendComple tedCallback); // 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, AsyncCompletedE ventArgs 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 mes sage // was sent to etc } // finally dispose of the message if (msg != null) msg.Dispose(); }

You might also like