Over a million developers have joined DZone.

How to Send Email Messages using Exchange Server & Exchange Web Services Using C# & VB.NET

· Cloud Zone

Download the Essential Cloud Buyer’s Guide to learn important factors to consider before selecting a provider as well as buying criteria to help you make the best decision for your infrastructure needs, brought to you in partnership with Internap.

This technical tip explains how to send email messages using exchange server & exchange web services inside .NET applications. You can send email messages using Exchange Server with the help of the tools in the Aspose.Email.Exchange. The ExchangeClient.Send() method accepts a MailMessage instance as a parameter and sends the email. Please follow the following steps to send emails using Exchange Server:

  • Create an instance of the ExchangeClient class.
  • Specify server name, username, password and domain.
  • Create an instance of the MailMessage class.
  • Specify the from, to, subject and other MailMessage properties.
  • Call the ExchangeClient.Send() method to send the email.
//The sample code below sends email messages using Exchange Server.

//[C# Code Sample]

/ Create instance of ExchangeClient class by giving credentials
ExchangeClient client = new ExchangeClient("http://MachineName/exchange/username",
    "username", "password", "domain");

// Create instance of type MailMessage
MailMessage msg = new MailMessage();
msg.From = "sender@domain.com";
msg.To = "recipient@ domain.com ";
msg.Subject = "Sending message from exchange server";
msg.HtmlBody = "  

sending message from exchange server

"; // Send the message client.Send(msg); //[VB.NET Code Sample] ‘Create instance of ExchangeClient class by giving credentials Dim client As ExchangeClient = New ExchangeClient("http://MachineName/exchange/username", "username", "password", "domain") ' Create instance of type MailMessage Dim msg As MailMessage = New MailMessage() msg.From = "sender@domain.com" msg.To = "recipient@ domain.com " msg.Subject = "Sending message from exchange server" msg.HtmlBody = "

sending message from exchange server

" ' Send the message client.Send(msg) ///Send Email using Exchange Web Services //[C# Code Sample] // Create instance of IEWSClient class by giving credentials IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); // Create instance of type MailMessage MailMessage msg = new MailMessage(); msg.From = "sender@domain.com"; msg.To = "recipient@ domain.com "; msg.Subject = "Sending message from exchange server"; msg.HtmlBody = "

sending message from exchange server

"; // Send the message client.Send(msg); //[VB.NET Code Sample] ' Create instance of EWSClient class by giving credentials Dim client As IEWSClient = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain") ' Create instance of type MailMessage Dim msg As MailMessage = New MailMessage() msg.From = "sender@domain.com" msg.To = "recipient@ domain.com " msg.Subject = "Sending message from exchange server" msg.HtmlBody = "

sending message from exchange server

" ' Send the message client.Send(msg)

The Cloud Zone is brought to you in partnership with Internap. Read Bare-Metal Cloud 101 to learn about bare-metal cloud and how it has emerged as a way to complement virtualized services.

Topics:
c#

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}