Sending Email in asp.net or mvc using gmail or other smpt.


. how to configure email's setting to sending email by user account.

I have sent a email to user when his/her registration time. In this exam. ,I have sent email address, username, password and user verify link..
I have used a snippet code of email sending.

First of all, I have add the emailId,password and smpt details in web.config file.
  <configSections>
 <appSettings>
<add key="EmailHost" value="smtp.gmail.com" />
    <add key="EmailPort" value="587" />
    <add key="NoReplyEmail" value="adc@gmail.com" />
    <add key="NoReplyPassword" value="password" />
    <add key="NoReplyEmailDisplayName" value="display name" />
    <add key="Sitename" value="site name"/>
    <add key="StationeryPath" value="Content\Stationery\" />
  </appSettings>
  <connectionStrings>
 

After that I will create a seprate class for sending emall functionality and this class use in several place.
Create a class MailSent and inherit to MAilMessage class. show the snippet of code.
using System.Net.Mail;
using System.Net;
public class MailSend : MailMessage
{
}
I will make a common function to sending welcome message and pass the argument.
public void SendWelcomeMessage(string aEmailAddress, string aUserName, string aPassword, string aUrl, string aPhysicalAppPath) {
            StringBuilder builder = new StringBuilder(GetStationery("WelcomeMessage.htm", aPhysicalAppPath));
            builder.Replace("$%EmailAddress%$", aEmailAddress);
            builder.Replace("$%Url%$", aUrl);
            builder.Replace("$%UserName%$", aUserName);
            builder.Replace("$%Password%$", aPassword);
            mailweb.To.Add(aEmailAddress);
            mailweb.Subject = "Welcome to " + ConfigurationManager.AppSettings["Sitename"];
            mailweb.Body = builder.ToString();
            this.SendFromDomain(mailweb);      
    }
Open the html file and read txt for sending mail body ..............
 private static String GetStationery(String aStationeryFileName, String aPhysicalAppPath)    {
        String templateFileName = aPhysicalAppPath + ConfigurationManager.AppSettings["StationeryPath"] + aStationeryFileName;
        String template = String.Empty;
        if (System.IO.File.Exists(templateFileName))
        {
            using (System.IO.StreamReader rdr = System.IO.File.OpenText(templateFileName))
            {
                template = rdr.ReadToEnd();
            }
        }
        return template;
    }

Open the html file and read txt for sending mail body ..............
    private static String GetStationery(String aPhysicalAppPath, String stationeryFolder, String aStationeryFileName)  {
        String path = aPhysicalAppPath + stationeryFolder + @"\" + aStationeryFileName;
        String str2 = String.Empty;
        if (!File.Exists(path))
        {
            return str2;
        }
        using (StreamReader reader = File.OpenText(path))
        {
            return reader.ReadToEnd();
        }
    }

Reading email address and password whose email send mail.....
    private void SendFromDomain() {
        From = new MailAddress(ConfigurationManager.AppSettings["NoReplyEmail"], ConfigurationManager.AppSettings["NoReplyEmailDisplayName"]);
        IsBodyHtml = true;
        Send();
    }

Sending Email function read host and port or other credentials....
    private void Send()  {
        SmtpClient client = new SmtpClient();
        client.Host = ConfigurationManager.AppSettings["EmailHost"];
        client.Port = Convert.ToInt32(ConfigurationManager.AppSettings["EmailPort"]);
        client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["NoReplyEmail"], ConfigurationManager.AppSettings["NoReplyPassword"]);
        try
        {
            client.EnableSsl = true;
            client.Send(this);
        }
        catch (System.Net.Mail.SmtpException ex)
        {
            throw (ex);
        }
    }

 

I have used a html file  "WelcomeMessage.htm" like containt in side html txt....
replace variable by value..
<table width="300" bgcolor="#ffffff" border="0" cellpadding="2" cellspacing="0">                                        <tbody>
                                            <tr><td style="padding-left: 10px;"> <b>E-Mail :</b> $%EmailAddress%$ </td></tr>
                                            <tr><td style="padding-left: 10px;"> <b>UserName :</b> $%UserName%$ </td></tr>
                                            <tr><td style="padding-left: 10px;"> <b>Password :</b>$%Password%$ </td></tr>
                                            <tr><td style="padding-left: 10px;"> <b><a href="http://$%Url%$">Verify your account</a> </td></tr>
                                        </tbody></table>


Comments

Popular posts from this blog

Form submit resulting in “InvalidDataException: Form value count limit 1024 exceeded.” In ASP.NET Core

Calculate Dynamic Current Calendar / Financial Year In C#

Repository Design Pattern in C#