It is one of the major requirements in application to inform support team about the errors occurred during production. Or in most of the trading applications, status of the trades needs to be forwarded to the clients through emails [attaching latest reports].

This blog helps users to send emails using a SMTP server which send Outlook emails from a production id rather than from user’s mailbox as generally done by using Microsoft.Office.Interop.Outlook.dll

Here we will use System.Net.Mail instead of Microsoft.Office.Interop.Outlook to send emails. This is done for below advantages:-

1. Mail will be send using a production id instead of current user id

2. This will not use current user’s mailbox and hence in turn will not prompt user to allow accessing his mailbox

3. No need for user’s outlook to be open at the time of sending mail which is a requirement while using Microsoft.Office.Interop.Outlook

Code Changes

a. Include required namespace in the code

b. Create format for email body

c. Create send email method using MailMessage class provided in System.Net.Mail dll. Determine From, To, CC emails id’s either from app.config as done in below example or they can be passed in method also if needs to be dynamic.

d. Call the SendEmail() method whenever required, most generic place is to call in every catch block of the application so that an email is send to the concerned application support group whenever some exception occurs at application level.

Below are the details of above mentioned steps:-

1. Include Namespace in the class

   1: using System.Net.Mail;

2. Build Message/Email Body

   1: private string BuildMessageBody(Exception ex)
   2: {
   3:     StringBuilder msgBody = new StringBuilder();
   4:     msgBody.Append("<HTML>");
   5:     msgBody.Append("<BODY>");
   6:     msgBody.Append("<FONT face=Arial size=2>");
   7:  
   8:     msgBody.Append("<B>Error Message : </B>");
   9:     msgBody.Append(ex.Message);
  10:     msgBody.Append("<BR>");
  11:  
  12:     msgBody.Append("<B>Error Source : </B>");
  13:     msgBody.Append("<BR>");
  14:     msgBody.Append("<B>StackTrace : </B>");
  15:     msgBody.Append(ex.StackTrace);
  16:     msgBody.Append("<BR>");
  17:  
  18:     if (ex.InnerException != null)
  19:     {
  20:         msgBody.Append(ex.InnerException.Message);
  21:         msgBody.Append("<BR>");
  22:         msgBody.Append(ex.InnerException.Source);
  23:         msgBody.Append("<BR>");
  24:         msgBody.Append("<B>Target Site : </B>");
  25:         msgBody.Append(ex.InnerException.TargetSite);
  26:         msgBody.Append("<BR>");
  27:         msgBody.Append("<B>Exception Details : </B>");
  28:         msgBody.Append("<BR>");
  29:         msgBody.Append(ex.InnerException.InnerException);
  30:         msgBody.Append("<BR>");
  31:         msgBody.Append("<B>Inner Exception : </B>");
  32:         msgBody.Append(ex.InnerException.StackTrace);
  33:         msgBody.Append("<BR>");
  34:     }
  35:     msgBody.Append("</FONT>");
  36:     msgBody.Append("</BODY>");
  37:     msgBody.Append("</HTML>");
  38:     return msgBody.ToString();
  39: }

3. App.Config changes [value needs to be modified as per the project]

These values can be directly passed to the SendEmail() method also as per the project requirements

   1: <appSettings>
   2:     <!-- Email -->
   3:     <add key="EMAIL_FROM" value="productionId@domain.com"/>
   4:     <add key="EMAIL_TO" value="ToId@domain.com"/>
   5:     <add key="EMAIL_COPYDEVTEAM" value="DevTeam@domain.com"/>
   6:     <!—Add available SMTP server name here before using the code -->
   7:     <add key="SMTP_SERVER" value="infy-hub"/>
   8: </appSettings>

4. Method [Send Mail using SMTP server]

   1: public static void SendEmail(string body, string subject)
   2: {
   3:     try
   4:     {
   5:         string emailTo = ConfigurationManager.AppSettings["EMAIL_TO"];
   6:         string emailFrom = ConfigurationManager.AppSettings["EMAIL_FROM"];
   7:         string emailCopy = ConfigurationManager.AppSettings["EMAIL_COPYDEVTEAM"];
   8:         string emailSmtpServer = ConfigurationManager.AppSettings["SMTP_SERVER"];
   9:  
  10:         // Create a message and set up the recipients.
  11:         MailMessage email = new MailMessage(emailFrom, emailTo, subject, body);
  12:         email.IsBodyHtml = true;
  13:  
  14:         email.CC.Add(emailCopy);
  15:         
  16:         // Send the message
  17:         SmtpClient client = new SmtpClient(emailSmtpServer);
  18:  
  19:         // Add credentials if the SMTP server requires them.
  20:         client.Credentials = CredentialCache.DefaultNetworkCredentials;
  21:         //send the mail
  22:         client.Send(email);
  23:     }
  24:     catch (Exception ex)
  25:     {
  26:         LogException(ex, "Error while sending email");
  27:     }
  28: }

5. Method Call [Send mail when some major exception is encountered in application, ex:- in catch blocks]

   1: catch (Exception ex)
   2: {
   3:     string message = BuildMessageBody(ex);
   4:     SendEmail(message, subject);
   5: }

6. Output [Mail Body will look like below]

   1: Error Message : XXXXXXXXXXXXXX
   2: Actual Exception Error Message : XXXXXXXXXXXXX
   3: Error Source : XXXXXXXXXXXXXX
   4: StackTrace : XXXXXXXXXXXXXXX

Summary:

1. Send email in case of some major exception in application has occurred i.e. when main functionality is not working. We can create a method ‘HandleException’ which will call method ‘SendMail’ defined in step 4. ‘HandleException’ method can be called in catch (Exception ex) of try-catch-finally block.

2. BuildMessageBody() method can be modified as per the mail format requirement of the project.

3. Attachments can be added using below code:-

Attachment attach = new Attachment(fileName);

email.Attachments.Add(attach);

4. Above example shows scenario in which an error has occurred, we can also use this functionality in cases where daily status report needs to be send to the business user.

Benefits:

1. Using SMTP server , one can send mail from a production id

2. It is faster as it does not use user’s outlook to send emails which in turn prompts user to allow accessing his outlook before sending emails.

3. Reduces unnecessary step of allowing access.