收录日期:2020/07/07 00:24:16 时间:2009-04-20 10:27:57 标签:asp.net

I am doing a Flight booking system and I want to send an E-Mail to the user which wiil contain the E-Ticket of his travel. The E-Ticket is generated dynamically with the booking ID fetched from the database and the other details from the previous pages like Name of the passenger and all. So how can I send him the dynamically generated E-Ticket to his E-Mail ID?

WARNING: My original answer to this question suggest using System.Web.Mail for sending e-mails. However, this API has been replaced by System.Net.Mail back in .NET 2.0, and the classes of System.Web.Mail are now all marked obsolete/deprecated.


You can use the System.Web.Mail.Mailmessage class:

System.Web.Mail.MailMessage mailMessage = new System.Web.Mail.MailMessage();
mailMessage.To = "recipient@repipient.com";
mailMessage.From = "sender@sender.com";
mailMessage.Subject = "Email subject";
mailMessage.BodyFormat = System.Web.Mail.MailFormat.Html;
mailMessage.Body = "Email body";

System.Web.Mail.SmtpMail.SmtpServer = System.Configuration.ConfigurationSettings.AppSettings["mailserver"];
System.Web.Mail.SmtpMail.Send(mailMessage);

WARNING: The accepted answer to this question suggest using System.Web.Mail for sending e-mails. However, this API has been replaced by System.Net.Mail back in .NET 2.0, and the classes of System.Web.Mail are now all marked obsolete/deprecated.


Here is the a real simple example of how to construct and send a mail message in .NET:

using (MailMessage message = new MailMessage())
{
    message.From = new MailAddress("boss@example.com");
    message.To.Add(new MailAddress("you@example.com"));
    message.Subject = "Get back to work!";
    message.Body = "Stop hanging around SO.";

    SmtpClient smtp = new SmtpClient();
    smtp.Send(message);
}

Actually, the code above can be written much shorter, using the SmtpClient.Send() method:

SmtpClient smtp = new SmtpClient();
smtp.Send("boss@example.com", "you@example.com",
          "Get back to work!", "Stop hanging around SO.");

However, you will often need to use the "verbose" style, in order to be able to set display names on the addresses, change the message content type or add attachments to the message.

Regardless of which you choose, you will need to configure your application with respect to SMTP settings, telling it how to deliver email. In you application configuration file (that would be web.config in an ASP.NET application), you will need to add something like this:

<system.net>
    <mailSettings>
        <smtp from="default@example.com">
            <network host="smtp.example.com" />
        </smtp>
    </mailSettings>
</system.net>

The exact settings depends on how you want your application to deliver mail messages. With the above sample configuration, messages will be relayed through a specified SMTP server. Other solutions includes having the application write messages to a pickup folder, from where the local IIS virtual mail server will process them. See the official documentation for details.

Emails can be sent using the System.Net.Mail namespace. I'd consider using a StringBuilder or String.Format to place the details into the body of the email.

System.web.mail is (rightfully) deprecated in asp.net v2.0; you should ignore that answer and listen to the folks who are pointing you to use system.net.mail instead.

Have a look at the MailMessage class. That MSDN page contains a complete sample including how to handle attachments.

Or look at this short tutorial in Scott Guthries blog - it also explains the required entries in the configuration file (web.config).

It is pretty simple to send an email in ASP.NET. Jsut generate the content of the message in your code, pulling in the fixed and dynamic elements to create the full string for the body, then simple send it, either as plain text or HTML.

Here is the basic structure for the call to do a send:

Imports System.Net.Mail

        Dim eMessage As New MailMessage(senderAddress, receiverAddress)

        eMessage.Subject = emailSubject
        eMessage.Body = emailBody
        eMessage.IsBodyHtml = emailIsHTML
        eMessage.Attachments.Add(emailAttachment)

        ' send email object
        Dim smtp As New SmtpClient
        smtp.Send(eMessage)

The dynamic part of the content is merely a stringbuilder or similar approach to build up the string from the fixed and dynamic elements you need, adding HTML formatting if you want to use HTML email, etc.