Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Mail Helper for Sending Emails in ASP.NET MVC using C#

0.00/5 (No votes)
9 Feb 2015 2  
In this post, we will create a simple mail helper class for sending emails in ASP.NET MVC using C#.

In this post, we will create a simple mail helper class for sending emails in ASP.NET MVC using C#.

Implementation

Create a class named MailHelper and then add the following code.

Mail Helper

public class MailHelper
    {
        private const int Timeout = 180000;
        private readonly string _host;
        private readonly int _port;
        private readonly string _user;
        private readonly string _pass;
        private readonly bool _ssl;

        public string Sender { get; set; }
        public string Recipient { get; set; }
        public string RecipientCC { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public string AttachmentFile { get; set; }

        public MailHelper()
        {
            //MailServer - Represents the SMTP Server
            _host = ConfigurationManager.AppSettings["MailServer"];
            //Port- Represents the port number
            _port = int.Parse(ConfigurationManager.AppSettings["Port"]);
            //MailAuthUser and MailAuthPass - Used for Authentication for sending email
            _user = ConfigurationManager.AppSettings["MailAuthUser"];
            _pass = ConfigurationManager.AppSettings["MailAuthPass"];
            _ssl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSSL"]);
        }

        public void Send()
        {
            try
            {
                // We do not catch the error here... let it pass direct to the caller
                Attachment att = null;
                var message = new MailMessage(Sender, Recipient, Subject, Body) { IsBodyHtml = true };
                if (RecipientCC != null)
                {
                    message.Bcc.Add(RecipientCC);
                }
                var smtp = new SmtpClient(_host, _port);

                if (!String.IsNullOrEmpty(AttachmentFile))
                {
                    if (File.Exists(AttachmentFile))
                    {
                        att = new Attachment(AttachmentFile);
                        message.Attachments.Add(att);
                    }
                }

                if (_user.Length > 0 && _pass.Length > 0)
                {
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new NetworkCredential(_user, _pass);
                    smtp.EnableSsl = _ssl;
                }

                smtp.Send(message);

                if (att != null)
                    att.Dispose();
                message.Dispose();
                smtp.Dispose();
            }

            catch (Exception ex)
            {

            }
        }
    }

Place the following code in the app settings of your application:

<appSettings>
<add key="MailServer" value="smtp.gmail.com"/>
<add key="Port" value="587&Prime;/>
<add key="EnableSSL" value="true"/>
<add key="EmailFromAddress" value="xxxx@gmail.com"/>
<add key="MailAuthUser" value="xxxx@gmail.com"/>
<add key="MailAuthPass" value="xxxxxxxx"/>
</appSettings>

If you don’t have authentication for sending emails, you can pass the empty string in MailAuthUser and MailAuthPass.

<appSettings>
<add key="MailServer" value="smtp.gmail.com"/>
<add key="Port" value="587"/>
<add key="EnableSSL" value="true"/>
<add key="EmailFromAddress" value="xxxx@gmail.com"/>
<add key="MailAuthUser" value=""/>
<add key="MailAuthPass" value=""/>
</appSettings>

Usage

Add the following code snippet in your controller to call MailHelper class for sending emails.

var MailHelper = new MailHelper
   {
      Sender = sender, //email.Sender,
      Recipient = useremail,
      RecipientCC = null,
      Subject = emailSubject,
      Body = messageBody
   };
 MailHelper.Send();

The post Mail Helper for sending emails in ASP.NET MVC using C# appeared first on Venkat Baggu Blog.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here