Tuesday, July 5, 2016

How to send email in ASP.NET C#

To send email in ASP.NET application, please do below steps

1. Configure Web.config file for MailSettings as below


<system.net>
    <mailSettings>
      <smtp from="no-repy@test.com" deliveryMethod="Network">
        <network host="mail.test.com" userName="user@test.com" password="password" port="25"/>
      </smtp>
    </mailSettings>
  </system.net>

2. Create below function for SendEmail


public static bool SendEmail(string FromEmail, string ToEmails,string BccEmails, string subject, string MailBody, Boolean IsBodyHtml)
    {
        MailMessage mm = new MailMessage();

        if (!String.IsNullOrEmpty(FromEmail))
            mm.From = new MailAddress(FromEmail);

        foreach (string toemail in ToEmails.Split(';'))
            if (!String.IsNullOrEmpty(toemail))
                mm.To.Add(new MailAddress(toemail));


        foreach (string BccEmail in BccEmails.Split(';'))
            if (!String.IsNullOrEmpty(BccEmail))
                mm.Bcc.Add(new MailAddress(BccEmail));

        mm.Subject = subject;
        mm.IsBodyHtml = IsBodyHtml;
        mm.Body = MailBody;

        SmtpClient smtp = new SmtpClient();
        smtp.Send(mm);
        return true;
    }

No comments:

Post a Comment