How To Send Mail Using Exchange Server In ASP.Net

In this blog I am explaining how to send mail on any domain using exchange server. You can implement exchange server following these step.
 
Step 1 - Create a web application.
 
Step 2 - Add "Microsoft.Exchange" library  reference in your application.
 
PM>Install-Package Microsoft.Exchange.WebServices
 
See this image and add  "Microsoft.Exchange" library in your application.
 
 
 
Thereafter create a method to create mailing concept using exchange server.
  1. //send mail to admin via outlook credintial.  
  2.     public class SendmailByOutLook  
  3.     {  
  4.         string MailUser = WebConfigurationManager.AppSettings["MailUser"].ToString();  
  5.         string MailPass = WebConfigurationManager.AppSettings["mailPass"].ToString();  
  6.         string MailTo = WebConfigurationManager.AppSettings["MailTo"].ToString();  
  7.         public bool SendMail(RequestInsights info)  
  8.         {  
  9.             try  
  10.             {  
  11.                 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);  
  12.                 service.Credentials = new NetworkCredential(MailUser,MailPass);  
  13.                 service.AutodiscoverUrl(MailUser);  
  14.                 EmailMessage emailMessage = new EmailMessage(service);  
  15.                 emailMessage.Subject =info.Subject;  
  16.                 emailMessage.Body = new MessageBody("<table><tr><td>Hi Admin,</td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr><tr><td>Name</td><td>:</td><td>" +info.Name+ "</td></tr><tr><td></td><td></td><td></td></tr><tr><td>Email</td><td>:</td><td>" + info.EmailId + "</td></tr><tr><td></td><td></td><td></td></tr><tr><td>Details</td><td>:</td><td>" + info.Details+ "</td></tr><tr><td></td><td></td><td></td></tr><tr><td>Date</td><td>:</td><td>" + DateTime.Now.ToString() + "</td></tr></table>");  
  17.                 emailMessage.ToRecipients.Add(MailTo);  
  18.                 emailMessage.SendAndSaveCopy();  
  19.                 return true;  
  20.             }  
  21.             catch (Exception ex)  
  22.             {  
  23.                 return false;  
  24.             }          
  25.              
  26.         }  
  27.       }  
In this example you can customize these thing according your requirement.
  1.         string MailUser = WebConfigurationManager.AppSettings["MailUser"].ToString();    
  2.         string MailPass = WebConfigurationManager.AppSettings["mailPass"].ToString();    
  3.         string MailTo = WebConfigurationManager.AppSettings["MailTo"].ToString();    
And also message body.
  1. emailMessage.Body = new MessageBody("<table><tr><td>Hi Admin,</td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr><tr><td>Name</td><td>:</td><td>" +info.Name+ "</td></tr><tr><td></td><td></td><td></td></tr><tr><td>Email</td><td>:</td><td>" + info.EmailId + "</td></tr><tr><td></td><td></td><td></td></tr><tr><td>Details</td><td>:</td><td>" + info.Details+ "</td></tr><tr><td></td><td></td><td></td></tr><tr><td>Date</td><td>:</td><td>" + DateTime.Now.ToString() + "</td></tr></table>");   
Thanks...
Ebook Download
View all
Learn
View all