TOC

This article is currently in the process of being translated into Vietnamese (~35% done).

Misc:

Sending e-mails

Gửi e-mail với ASP.NET khá dễ dàng. .NET framework có một namespace cho việc xử lý e-mail, đó là System.Net.Mail. Trong các ví dụ sau, chúng ta dùng hai lớp trong namespace này: Đó là MailMessage cho e-mail hiện tại và SmtpClient cho việc gửi e-mail.

Như bạn thấy, mail được gửi qua server SMTP, để gửi mail với .NET framework, bạn cần truy cập vào server SMTP. Nếu bạn kiểm tra nội bộ thì công ty cung cấp cho bạn truy nhập Internet với server SMTP mà bạn có thể dùng. Để tìm địa chỉ thực sự thì dùng smtp.your-isp.com.

Một khi bạn đã truy nhập vào server SMTP thì chúng ta đã sẵn sàng gửi email đầu tiên. Trong ví dụ đầu tiên, tất cả những gì bạn cần là một trang trống với đoạn code sau trong CodeBehind.

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        MailMessage mailMessage = new MailMessage();
        mailMessage.To.Add("your.own@mail-address.com");
        mailMessage.From = new MailAddress("another@mail-address.com");
        mailMessage.Subject = "ASP.NET e-mail test";
        mailMessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
        SmtpClient smtpClient = new SmtpClient("smtp.your-isp.com");
        smtpClient.Send(mailMessage);
        Response.Write("E-mail sent!");
    }
    catch(Exception ex)
    {
        Response.Write("Could not send the e-mail - error: " + ex.Message);
    }
}

That's actually all you need to send an e-mail. We create a new MailMessage instance, add a new receiver, set the "From" address and the subject, and then we write a simple test message for the body of the e-mail. After that, we create a new instance of the SmtpClient, with the host address of the SMTP server that you may use as a parameter, and then we use the SmtpClient instance to shoot the e-mail out into cyberspace. The entire thing is surrounded by a try..catch block, just in case something goes wrong.

This was just a very basic example, but there are a lot of other options. Here is a short list with interesting ideas:

You can attach one or several files, simply by adding them to the Attachments collection. In this example, we attach a file called "image.jpg", located in the root of the ASP.NET website:

mailMessage.Attachments.Add(new Attachment(Server.MapPath("~/image.jpg")));

You can send to more than one person at the same time, simply by adding another e-mail address to the "To" collection, like this:

mailMessage.To.Add("your.own@mail-address.com");
mailMessage.To.Add("another@mail-address.com");

You can set a name for the sender - otherwise, only the e-mail address will be shown in the "From" column of the receivers e-mail client. For instance, like this:

mailMessage.From = new MailAddress("me@mail-address.com", "My Name");

You can send HTML e-mails, instead of the default plaintext mails, to use more complicated layouts. Here is a simple example:

mailMessage.IsBodyHtml = true;
mailMessage.Body = "Hello <b>world!</b>";

You can use the CC and BCC fields, just like in regular e-mail messages, like this:

mailMessage.CC.Add("me@mail-address.com");
mailMessage.Bcc.Add("me2@mail-address.com");

You can set the priority of an e-mail, like this:

mailMessage.Priority = MailPriority.High;
This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!