joomla visitors
Mithila Software Blog | Trying out a swiss knife like asp.net email function in asp.net

Trying out a swiss knife like asp.net email function in asp.net

July 05, 2012 Posted by Mithila Software

Whether you are a seasoned web developer or a newbie you might agree to the point that writing email sending code for clients is one of the most frequently used actions in your life.There may be many applications of the email code , ranging from that very simple contact/query page to the fancy scheduler enabled e-mailers programs.In addition , you may have found email codes interacting with database for record keeping and maintenance.

 

The purpose of this post is to find a way so that we can design an asp.net email sending code which can handle gmail email accounts as well with minimum coding or such that we can put some credentials and we are able to send an email.

 

First Step

Defining email sending credentials in asp.net constants.

say -

 

    public const string _SMTP_Server = "xx.domain.com"; // Our SMTP server provided by the host
    public const string _SMTP_User = xx@domain.com;
    public const string _SMTP_Pass = "<pass>";
    public const string _Message_Receiving_Email = xx@domain.com; // The address which will receive our email

 

 

Constructing Our Generic Email Sending Function

 

public bool SendMail (string from, string name, string subject, string body, bool IsGmail)
    {

        System.Net.Mail.MailMessage msgMail = new System.Net.Mail.MailMessage();
        //for gmail servers change mail@domain.com with your-gmail-id@gmail.com
        System.Net.Mail.MailAddress From = new System.Net.Mail.MailAddress(_SMTP_User, name);
        msgMail.From = From;
        //person to send mail
        msgMail.To.Add(_Message_Receiving_Email);
        msgMail.Subject = subject;
        msgMail.Body = body;
        msgMail.IsBodyHtml = true;
        msgMail.Priority = System.Net.Mail.MailPriority.Normal;
        //for gmail replace mail@domain.com with gmailID@gmail.com and gmail password
        System.Net.NetworkCredential logininfo = new System.Net.NetworkCredential(_SMTP_User, _SMTP_Pass);
        System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
        if (IsGmail)
        {
            client.Host = "smtp.gmail.com";
            client.Port = 587;
            client.EnableSsl = true;
        }
        else
        {
            //if not using gmail input your smtp host name here
            client.Host = _SMTP_Server;
        }
        client.UseDefaultCredentials = false;
        client.Credentials = logininfo;
        //client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory

        try
        {
            client.Send(msgMail);
            return true;
        }
        catch (Exception ep)
        {
            Response.Write(ep.Message + ep.Data + ep.InnerException);
            return false;
        }

    }

 

 

Invoking our Function

 

bool deliveryReport = false;

deliveryReport = SendMail (“userXX@some_email.com”, “Sample User Name”, “User Enquiry Subject”, “Body of the Email”, false) ;


if(deliveryReport)

{

Response.Write("Email Sent Successfully");

}

else

{

Response.Write("failed");

}

Tags:

Asp.Net

blog comments powered by Disqus
Address ::
Mithila Software Solutions,
Green House , 3 GM Road ,
Near Radio Station , Darbhanga
PIN:: 846004
Phone :: # 099-397-49350