Sending email from an application

             In this post I will show you how to send an email from your application with a simple example. In the below example I created a web application and I created a class which has methods for sending email from your application. I also created some key values in appSettings section of the web.config file. I hope the code in the example is self explanatory.

Web.config:

<?xml version="1.0"?>

<configuration>

…………………………………

…………………………………

<appSettings>

      <add key="FromEmail" value="xxxx@domain.com"/>

      <add key="SMTPServerIP" value="smtp.domain.com"/>

      <add key="SMTPServerPort" value="25"/>

</appSettings>

………………………………….

………………………………….

</configuration>

This is the class which has methods for sending emails.

EmailService.cs:


using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Net.Mail;
/// <summary>
/// Summary description for EmailService
/// </summary>
public class EmailService
{
    MailMessage mailMessage = null;
    //Constructor instantiating the mail object
      public EmailService()
      {
        mailMessage = new MailMessage();
      }
    //This method takes string list as a parameter which contains email addresses.
    public void SendEmail(List<string> mailaddrs)
    {
        //Assign From mail from web.config
        mailMessage.From = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["FromEmail"].ToString());
        foreach (string adrs in mailaddrs)
            mailMessage.To.Add(new MailAddress(adrs));

        mailMessage.Subject = "Write the Subject Here";
        mailMessage.Body = "Write the body of the mail here";
        //Get the notification whether email is successfully delivered or not
        mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
        //Assign SMTP Server name and Port from web.config file
        string strSmtpServer = System.Configuration.ConfigurationManager.AppSettings["SMTPServerIP"];
        int strSmtpServerPort = System.Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SMTPServerPort"]);
        SmtpClient smtpClient = new SmtpClient(strSmtpServer, strSmtpServerPort);
        smtpClient.Send(mailMessage);

    }
}

Now pass email address list to the “SendMail” method. That is it you can send mail from your application. Hope this post helps you.
 
Developed Environment:
Visual Studio 2008
.NET3.5
 



Advertisements

Subject Reminder in Outlook

Have you ever sent a mail from outlook without subject and feel bad after that? then here is the solution. 

Follow below steps and you can get rid of it. 

  • Open Outlook.
  • Navigate Tools->Macro->Visual Basic Editor (Alt+11)
  • In the left pane, expand Project1->Microsoft Outlook Objects
  • Double click on “ThisOutlookSession”
  • In the right pane copy below code and save it (see the image).      

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim strSubject As String
strSubject = Item.Subject
If Len(Trim(strSubject)) = 0 Then
Prompt$ = "Subject is Empty. Are you sure you want to send the Mail?"
If MsgBox(Prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground + vbDefaultButton2, "Check for Subject") = vbNo Then
Cancel = True
End If
End If
End Sub

     subject reminder  

  • Navigate Tools->Macro->Security
  • Select either “Warnings for all macros” or “No Security Check for macros” (1st option will ask enable macro while starting Outlook where 2nd option will directly enable without your inputs)
  • Click on OK. 

Note: For Outlook 2010 users no need to follow above steps. Subject reminder is inbuilt in 2010 version.

If you dont want to lower your macro sucurity level, create certificate for your macro. It enables you to keep your macro security high. Read below link to create your own certificate for your macro.

http://www.howto-outlook.com/howto/selfcert.htm

Now test sending a mail without subject. You will get a warning message. 🙂