Tuesday, September 22, 2009

How to send email in Asp.net c#



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;
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 bodyMessage= new StringBuilder();

bodyMessage.Append("<br>");
bodyMessage.AppendFormat("Response From your domain name");
bodyMessage.Append("<br>");
bodyMessage.AppendFormat("Your Account has been deleted if you are not activate\n\n");
bodyMessage.Append("<br>");
bodyMessage.AppendFormat("So Please follow the link To Activate Your Account with Us!\n\n");
bodyMessage.Append("Click Here To Activate Your Account");
bodyMessage.AppendFormat("Registered Email:", "yourtoemail@domain.com");
bodyMessage.AppendFormat("Note:- If U Already Activate your account means Please ignore this message");

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

the mail will be send to the to address for activating the account 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.

Happy to send mail.

9 comments :

  1. great artikel n tutorial

    i will try later

    ReplyDelete
  2. Great...so you're masterring ASP.net ?

    ReplyDelete
  3. Great tutorial thanks for sharing

    We can also submit our .net related links on http://www.dotnettechy.com to increase traffics of your website / blog

    Its kind of social networking for .Net professionals only

    ReplyDelete
  4. I love your blog.. very nice colors & theme. Did you design this website
    yourself or did you hire someone to do it for you? Plz respond as I'm looking to design my own blog
    and would like to know where u got this from. appreciate it

    Feel free to visit my page; animals

    ReplyDelete
  5. I am really glad to read this weblog posts which consists of tons of helpful facts,
    thanks for providing these kinds of information.


    my homepage: blackbox rdp

    ReplyDelete
  6. Nice weblog right here! Additionally your web site loads up fast!
    What web host are you the use of? Can I get your affiliate hyperlink on your
    host? I want my site loaded up as quickly as
    yours lol

    my site: Free ITunes Gift Card Code Generator, Http://Www.Youtube.Com/,

    ReplyDelete