Monday, November 14, 2011

SMTP mail via ASP.net

  private bool SendMail(string mailto, string username, string pasword)
    {
        bool chkstatus = false;
        try
        {
            string verifyurl;
            verifyurl = "http://www.demo.com/demos1/SignIn.aspx";
            string boardurl = "http://Shera.com";

            MailMessage mailmsg = new MailMessage();
            mailmsg.From =  new System.Net.Mail.MailAddress(ConfigurationManager.AppSettings["FromAdminAddress"].ToString());
            mailmsg.To.Add(mailto);

            mailmsg.Subject = "Account activation mail: " + ' ' + " " + '"' + "Demo....." + '"' + "";

            string msgBody = "Hi,<BR><BR>";
            msgBody += "Welcome to my Demo..... <BR><BR>";
            msgBody += "Please keep this e-mail for your records. Your account information is as follows:<BR><BR>";
            msgBody += "--------------------------------------------------------";
            msgBody += "<table  border='0'>      " +
                          "  <tr>                           " +
                          "  <td>                           " +
                          "      Username:</td>             " +
                          "  <td align='left'>      " +
                          "     " + username + "                " +
                          "  </td>                          " +
                          " </tr>                           " +
                          "<tr>                             " +
                          " <td>                            " +
                          "      Board URL:</td>         " +
                          "   <td  align='left'>     " +
                          "  <A HREF=" + pasword + " >" + pasword + "</A>  " +
                          "        </td>                    " +
                          " </tr>                           " +
                          "<tr>                             " +
                          " <td>                            " +
                          "      Board URL:</td>         " +
                          "   <td  align='left'>     " +
                          "  <A HREF=" + boardurl + " >" + boardurl + "</A>  " +
                          "        </td>                    " +
                          " </tr>                           " +
                          "</table>                         ";
            msgBody += "--------------------------------------------------------<BR>";
            msgBody += "Please visit the following link in order to activate your account:<BR><BR>";
            msgBody += "<p><a href=" + verifyurl + " >" + verifyurl + "</a></p>";
            msgBody += "<BR>Thank you for registering.";
            msgBody += "<BR><BR>The Demo..... Team";
            msgBody += "<BR>-------------------------------";
            mailmsg.Body = msgBody;
            mailmsg.IsBodyHtml = true;


            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = ConfigurationManager.AppSettings["SmtpHost"].ToString();
            smtpClient.Port = Convert.ToInt32(ConfigurationManager.AppSettings["SmtpPort"].ToString());
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["UserName"].ToString(), ConfigurationManager.AppSettings["Password"].ToString());
            smtpClient.EnableSsl = false;

            smtpClient.Send(mailmsg);

            mailmsg.Dispose();
            chkstatus = true;

        }

        catch (Exception ex)
        {
           
            chkstatus = false;
        }

        return chkstatus;
    }

---------------------------
web.config
<add key="FromAdminAddress" value="shera@vedangsoftware.com"/>
    <add key="SmtpHost" value="60.5.523.212"/>
    <add key="SmtpPort" value="15"/>
    <add key="UserName" value="Shera@demos.com"/>
    <add key="Password" value="123456"/>
    




No comments:

Post a Comment