Wednesday, 3 April 2013

How to send mail using Asp .Net

Write the Code in The Event of any Server control to Send mail using Asp .Net
------------------------------------------------------------------------------------------------------------


void SendMailToUser()
        {
            SendFromMail = "codeuser@swashconvergence.us";
            SendToMail = txtEmail.Text; //"snehasish@gmail.com";
            subject = "Thank you for evaluating Product Demo ";
            Body += "<br /> <br />Dear " + txtFirstName.Text + "<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thank you for registering SYSTFIX. We hope you&#39;ll like the features we have created ";
            Body += "to help your System to step to an advanced Optimize level. <br /><br />Please use the following key to register your app upon the first run of the application:<br /><br /><div align=\"center\">";
            Body += activationCode + " </div> <br /> <strong>Please note</strong> that the initial installation and automatic setup process can take several minutes ";
            Body += " (some prerequisite software components need to be installed during the setup) – <br /> <br /> If you have difficulties installing or starting SYSTFIX on your machine ,or have ";
            Body += "questions regarding to the Product please call us on 123-456-789 or email us on:<a href=\"mailto:support@Product.com\">support@product.com</a><br /><br /><br />Thank You again for evaluating Product v1.0<br />";
            Body += "-------------------------------------------------------------------------------------------------------------------------<br />    The Product Sales Team";
            //Body = "Hi<br/> This is your activation code:" + activationCode + " <br/> <br/> <br/> Thanks <br/> Sankarshan Parida";
            SendMail(SendFromMail, SendToMail, subject, Body);
        }


Call the Send Mail Method
.............................................


public void SendMail(string SendToMail, string SendFromMail, string subject, string Body)
    {
        try
        {
            MailAddress SendFrom = new MailAddress(SendFromMail);
            MailAddress SendTo = new MailAddress(SendToMail);
            MailMessage MyMessage = new MailMessage(SendFrom, SendTo);

            MyMessage.Subject = subject;
            MyMessage.Body = Body;
            MyMessage.IsBodyHtml = true;
            string mSMTPServer = "swashtest.swashconvergence.us";// ConfigurationSettings.AppSettings["smtpServer"].ToString();
            SmtpClient SmtpMail = new SmtpClient(mSMTPServer);
            System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("codeuser@swashconvergence.us", "test@1234");
            SmtpMail.UseDefaultCredentials = false;
            SmtpMail.Credentials = SMTPUserInfo;
            SmtpMail.Send(MyMessage);
        }
        catch (Exception ex) { }
    }

No comments:

Post a Comment