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

Trying out a swiss knife like email function in

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 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 constants.

say -


    public const string _SMTP_Server = ""; // Our SMTP server provided by the host
    public const string _SMTP_User =;
    public const string _SMTP_Pass = "<pass>";
    public const string _Message_Receiving_Email =; // 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 with
        System.Net.Mail.MailAddress From = new System.Net.Mail.MailAddress(_SMTP_User, name);
        msgMail.From = From;
        //person to send mail
        msgMail.Subject = subject;
        msgMail.Body = body;
        msgMail.IsBodyHtml = true;
        msgMail.Priority = System.Net.Mail.MailPriority.Normal;
        //for gmail replace with 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 = "";
            client.Port = 587;
            client.EnableSsl = true;
            //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

            return true;
        catch (Exception ep)
            Response.Write(ep.Message + ep.Data + ep.InnerException);
            return false;




Invoking our Function


bool deliveryReport = false;

deliveryReport = SendMail (“”, “Sample User Name”, “User Enquiry Subject”, “Body of the Email”, false) ;



Response.Write("Email Sent Successfully");








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