Monday, March 1, 2021

Send Email Using Gmail SMTP / Send Mails from asp.net Application / How to send Emails form Code Behind file in asp.net

C# Class to send mail
=======================

 
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Web;
using System.Web.UI.WebControls;
namespace VSGCapital.EMailer
{
    public class Mailer
    {
        public int  SendEmail(eMail request)
        {
            int isMailSent = 0;
            try
            {
                SmtpSection smtpSection = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
                string receiverEmailAddress = ConfigurationSettings.AppSettings["ReceiverEmailAddress"];
                string smtpAddress = smtpSection.Network.Host;
                int portNumber = smtpSection.Network.Port;
                bool enableSSL = smtpSection.Network.EnableSsl;
                string emailFromAddress = smtpSection.From; //Sender Email Address  
                string password = smtpSection.Network.Password; //Sender Password  
                string emailToAddress = receiverEmailAddress; //Receiver Email Address  
                string subject = request.Subject;//mail subject line
                string body = request.Body;//mail body
                using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress(emailFromAddress);
                    mail.To.Add(emailToAddress);
                    mail.Subject = subject;
                    mail.Body = body;
                    mail.IsBodyHtml = true;
                    mail.IsBodyHtml = true;
                    //mail.Attachments.Add(new Attachment("D:\\TestFile.txt"));//--Uncomment this to send any attachment  
                    using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
                    {
                        smtp.Credentials = new NetworkCredential(emailFromAddress, password);
                        smtp.EnableSsl = enableSSL;
                        smtp.Send(mail);
                        isMailSent = 1;
                    }
                }
            }
            catch (Exception ex)
            {
                isMailSent= 0;
            }
            return isMailSent;
        }
    }

   public class eMail
    {
        public string SenderEmailAddress { get; set; }

        public string ReceiverEmailAddress { get; set; }

        public string Body { get; set; }

        public string Subject { get; set; }
    }

}




Note- Add Below Given settings into Web.Config File.
=====================================

<appSettings>
    <add key="ReceiverEmailAddress" value="receiverEmailAccount@gmail.com" />
  </appSettings>

 <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="senderEmailAccount@gmail.com">
        <network
            host="smtp.gmail.com"
            port="587"
            enableSsl="false"
            userName="senderEmailAccount@gmail.com"
            password="abdc@123"
            defaultCredentials="true"/>
      </smtp>
    </mailSettings>
  </system.net>

No comments:

Post a Comment

What is C# ("See Sharp") | Introduction of C#

  C# (pronounced " See Sharp ") is a modern, object-oriented, and type-safe programming language.  C# is a simple programming lan...