How To

How to Send Email using ASP.NET 2.0

Member
By AminTaheri
User-Submitted Article
(6 Ratings)

Ever wondered how to send email on a web page? Well here is a small function that you can use.

Difficulty: Easy
Instructions

Things You'll Need:

  • Visual Studio 2005
  • using System.Net.Mail
  1. Step 1

    First thing we do is create the Mail Message object.

    MailMessage oMsg = new MailMessage();

  2. Step 2

    Once we have that, we can start making email addresses. This is the email address that the email will be both sent from, and sent to.

    MailAddress oAddress = new MailAddress("Email@Domain.com");

  3. Step 3

    Now we will set the To and From addresses. Notice that Setting the From address and adding a TO address have different ways of specifying the same thing. For each additional email address to send to, you would just .Add another.

    To specify a CC or BCC you would use the Syntax oMsg.CC.Add(oAddress) and oMsg.Bcc.Add(oAddress) respectively.

    oMsg.From = oAddress;
    oMsg.To.Add(oAddress);

    Note: If you want the email to be sent from a different address then just create a new MailAddress object for it.

  4. Step 4

    Next we will set the Subject, and the Priority of the mail message. For the purposes of this article we will assume that the message is important.

    oMsg.Subject = "Email Sent";
    oMsg.Priority = MailPriority.High;

  5. Step 5

    Now that we have the basics set, we can insert our Email body, and whether it is an HTML body or TEXT. This is up to your preference. You could even have the user choose or use their saved preferences at this point.

    oMsg.Body = "A String Builder Text, or Typed Input";
    oMsg.IsBodyHtml = false;

  6. Step 6

    Now our email is completely built. All we have left to do is to send the email. So we will have to connect to a SMTP server to do so.

    If your provider requires SMTP Authentication (and all of them should be now. Shame on you if you dont!!) you will need to tell it not to use the defaults, and specify the user to connect with.

    Also specify the Mail server, by IP or hostname.

    SmtpClient smtp = new SmtpClient();
    smtp.UseDefaultCredentials = false;
    smtp.Host = "MailServer.Domain.com"
    smtp.Credentials = new NetworkCredential("userID", "password", "domainName");

  7. Step 7

    Lastly, Send the message and clear out the objects since you no longer need them.

    smtp.Send(oMsg);
    oMsg = null;
    smtp = null;

Post a Comment

Post a Comment
  • Have you done this? Click here to let us know.
I Did This

Related Ads

Internet
Virginia DeBolt,

Meet Virginia DeBolt eHow's Internet Expert.

Get Free Internet Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US

Demand Media
eHow_eHow Technology and Electronics