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

Author: Sridhar Pasham

I am a web developer works mostly with Microsoft technologies. I like to blog about code, web and technology. My blog is at sridharpasham.com. You can find me in twitter at @pashsridhar . You can know more about my professional background at linkedin.com/in/sridharpasham

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s