blog

Home / DeveloperSection / Blogs / How To Send Email Using System.Web.Mail

How To Send Email Using System.Web.Mail

Uttam Misra4501 05-Nov-2010

Here in this blog I am trying to explain you about sending emails in

ASP.NET by using namespace System.Web.Mail.

using System.Web.Mail; 
try
            {
                string smtpServer = "relay-hosting.secureserver.net";
                string userName = "contact@yourdomain.com";
                int cdoBasic = 1;
                int cdoSendUsingPort = 2;
                MailMessage msg = new MailMessage();
                if (userName.Length > 0)
                {
                    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
                    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
                    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
                    msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
                    //msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
                    // msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
                }
                msg.To = "contact@yourdomain.com";
                msg.From = "contact@yourdomain.com";
                msg.BodyFormat = MailFormat.Html;
                msg.Subject = txtSubject.Text;          
                msg.Body = "Name: " + txtName.Text + "<br /><br />Company: " + txtCompany.Text + "<br /><br />Email: " + txtEmail.Text + "<br />" + txtDetails.Text;
                SmtpMail.SmtpServer = smtpServer;
                SmtpMail.Send(msg);
 
                msg.To = txtEmail.Text.Trim();
                msg.From = "donotreply@yourdomain.com";
                msg.BodyFormat = MailFormat.Html;
                msg.Subject = txtSubject.Text;
                msg.Body = @"Hi,<br/>
                Thanks for contacting , we will review your information and try to get back to you ASAP.<br/>
                Thanks;
 
                SmtpMail.SmtpServer = smtpServer;
                SmtpMail.Send(msg);
                ValidationSummary1.Visible = false;
                lblStatus.Visible = true;
                lblStatus.Text = "Message Sent Sucessfully.";
 
            }
            catch
            {
 
            }


Updated 18-Sep-2014
More than 18 years of working experience in IT sector. We are here to serve you best.

Leave Comment

Comments

Liked By