Introduction
Sending E-mail Using the Exchange Webs Services API
Step 1

Figure 1: Create a new Console application project
Step 2

Figure 2: Add the Microsoft.Exchange.Webservices NuGet package
Step 3
- ExchangeService myservice = new
- ExchangeService(ExchangeVersion.Exchange2010_SP1);
- myservice.Credentials = new
- WebCredentials(ConfigurationSettings.AppSettings
- .Get("SenderEmailid").ToString(),
- ConfigurationSettings.AppSettings.Get("Password").ToString());
- try
- {
- string serviceUrl =
- ConfigurationSettings.AppSettings
- .Get("Office365WebserivceURL").ToString();
- myservice.Url = new Uri(serviceUrl);
- EmailMessage emailMessage = new EmailMessage(myservice);
- emailMessage.Subject = "Test Subject";
- emailMessage.Body = new MessageBody("Testing Exchange Web
- Service API");
- emailMessage.ToRecipients.Add(ConfigurationSettings
- .AppSettings.Get("Recipients").ToString());
- emailMessage.Send();
- }
- catch (SmtpException exception)
- {
- string msg = "Mail cannot be sent (SmtpException):";
- msg += exception.Message;
- throw new Exception(msg);
- }
- catch (AutodiscoverRemoteException exception)
- {
- string msg = "Mail cannot be
- sent(AutodiscoverRemoteException):";
- msg += exception.Message;
- throw new Exception(msg);
- }
Sending E-mail Using Office 365
- MailMessage = new MailMessage();
- mailMessage.From = new
- MailAddress(ConfigurationSettings.AppSettings
- .Get("SenderEmailid").ToString());
- mailMessage.To.Add(new
- MailAddress(ConfigurationSettings.AppSettings
- .Get("Recipients").ToString()));
- mailMessage.Subject = "Test Subject";
- mailMessage.Body = "Testing Office365 Email";
- mailMessage.IsBodyHtml = true;
- SmtpClient client = new SmtpClient();
- client.Credentials = new NetworkCredential("", "");
- client.Port = 587;
- client.Host = "smtp.office365.com";
- client.EnableSsl = true;
- client.send(mailmessage);
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <appSettings>
- <add key="SenderEmailid" value="user" />
- <add key="Password" value="password" />
- <add key="Office365WebserivceURL"
- value="https://outlook.office365.com/ews
- /exchange.asmx" />
- <add key="Recipients" value="samplemail@gmail.com" />
- </appSettings>
- </configuration>