Send Email From ASP.NET site

Many times we want to send auto email to our website viewer or website owner. As we require after the viewer of website submits any enquiry then it should be come immediate in email Inbox Or In E-Commerce website after successfully purchasing any item a auto email notification will be send to customer (billing detail) and as well as website administrator also.
So that I use two variation of code for sending email as shown below. For this we require a email account and password for authentication.

First one is require your any one email account, password and mail server detail. The second one is for using gmail account.


using System.Web.Mail;
class abc
{
    public bool mailsend(string sSubject, string sBody, string sMailFrom, string sToMail, string sIsAdmin)
    {
        try
        {
            MailMessage sMail = new System.Web.Mail.MailMessage();
            sMail.To = sToMail.ToString();
            sMail.From = sMailFrom.ToString();
            sMail.Subject = sSubject.ToString();
            sMail.Body = sBody.ToString();
            sMail.Priority = MailPriority.High;
            sMail.BodyFormat = MailFormat.Html;
            SmtpMail.SmtpServer = "mail.website.com"// your mail server name
 
            sMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate""1");  //basic authentication
            sMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername""emailid");  //set your username here
            sMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword""password");  //set your password here
            SmtpMail.Send(sMail);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
 
    // using by gmail id for authentication
    public bool mailsend(string to, string sub, string msg, string replyto)
    {
        string from = "yourmailid@gmail.com"//Replace this with your own correct Gmail Address
        string pwd = "password"// Replace this with your above email id password.
        bool status = false;
        System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
        email.To.Add(to);
        email.From = new System.Net.Mail.MailAddress(to, "Client", System.Text.Encoding.UTF8);
        string mailbody = msg;
        email.Subject = sub;
 
        email.ReplyTo = new System.Net.Mail.MailAddress(replyto, "", System.Text.Encoding.UTF8);
 
        email.SubjectEncoding = System.Text.Encoding.UTF8;
        email.Body = mailbody;
        email.BodyEncoding = System.Text.Encoding.UTF8;
        email.IsBodyHtml = true;
        System.Net.Mail.SmtpClient emailclient = new System.Net.Mail.SmtpClient();
        client.Credentials = new System.Net.NetworkCredential(from, pwd);
        client.Port = 587; // Gmail works on this port
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true//Gmail works on Server Secured Layer
        try
        {
            emailclient.Send(email);
            status = true;
            return (status);
        }
        catch (Exception ex)
        {
            status = false;
            return (status);
        }
    }
}

Post a Comment