Thursday, January 6, 2011

Send Email in Asp.net

How to send email in Asp.net using c#. In Asp.net 2.0,email sending is very easy.In our websites we send a feedback,contact us,registration welcome,Forgot password, news letter etc. to the users mail.By using the Asp.net classes.
we can access to send the email.The Simple Mail Transfer Protocol(SMTP) server is used for deliver the mail
using the System.Net.Mail namespace.

In System.Net.Mail namespace having two sub classes MailMessage and SmptClient.

MailMessage contains the properties From,To,Subject,Body etc.

SmtpClient send the Mailmessage data to the SMTP server.

The SMTP server Store the emails in a queue.It sends the mail one after another.

The steps to send email:-
1.use the namespace.
using System.Net.Mail;
using System.Text;

2.use the MailMessage to send the mail with SMTP Connection Settings.

MailMessage mailmsg = new MailMessage("yourfromemail@yourdomain.in", "yourtoemail@domain.com");
SmtpClient smtpmail = new SmtpClient();

mailmsg.Subject = "Account information Verified";
StringBuilder bodyMsg = new StringBuilder();

bodyMsg.Append("br");
bodyMsg.Append("br");
bodyMsg.AppendFormat("Response From your domain name");
bodyMsg.Append("br");
bodyMsg.AppendFormat("sample Text Message for the Email ----- 1\n\n");
bodyMsg.Append("br");
bodyMsg.AppendFormat("sample Text Message for the Email----------- 2 !\n\n");
bodyMsg.Append("br");
bodyMsg.Append("sample Text Message for the Email --- 3");
bodyMsg.Append("br");
bodyMsg.AppendFormat("Registered Email:", "yourtoemail@domain.com");
bodyMsg.Append("br");
bodyMsg.Append("br");
bodyMsg.Append("br");
bodyMsg.Append("br");


mailmsg.Body = bodyMsg.ToString();
smtpmail.Host = "smtp.yourdomain.in";
smtpmail.Port = 587;
smtpmail.Send(mailmsg);

the mail will be send to the to address with some sample text information.

mailmsg.Subject is the subject of our mail.

mailmsg.Body contains the bodymsg string values.it attach the bodymsg with subject.

smtp.Host is your smtp server name for eg.

gmail smtp server is "smtp.gmail.com".

smtpmail.port is the port by sending the mail.

smtpmail.Send function is used to concatenate your message,subject and send the mail through the port with the smtp host address.

No comments:

Post a Comment