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?
我正在做一个航班预订系统,我想向用户发送一封电子邮件,其中包含他旅行的电子机票。电子机票是动态生成的,其中包含从数据库中提取的预订ID以及之前页面中的其他详细信息,例如乘客姓名和所有内容。那么如何将动态生成的电子机票发送给他的电子邮件ID呢?
6 个解决方案
#1
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.
警告:我对此问题的原始答案建议使用System.Web.Mail发送电子邮件。但是,这个API已经被.NET 2.0中的System.Net.Mail所取代,而System.Web.Mail的类现在都被标记为过时/弃用。
You can use the System.Web.Mail.Mailmessage class:
您可以使用System.Web.Mail.Mailmessage类:
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);
#2
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.
警告:此问题的已接受答案建议使用System.Web.Mail发送电子邮件。但是,这个API已经被.NET 2.0中的System.Net.Mail所取代,而System.Web.Mail的类现在都被标记为过时/弃用。
Here is the a real simple example of how to construct and send a mail message in .NET:
以下是如何在.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.Send()方法可以将上面的代码写得更短:
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:
无论您选择哪种方式,都需要根据SMTP设置配置应用程序,告诉它如何发送电子邮件。在您的应用程序配置文件(即ASP.NET应用程序中的web.config)中,您需要添加如下内容:
<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.
确切的设置取决于您希望应用程序如何传递邮件。使用上面的示例配置,邮件将通过指定的SMTP服务器进行中继。其他解决方案包括让应用程序将消息写入拾取文件夹,本地IIS虚拟邮件服务器将从该文件夹处理它们。有关详细信息,请参阅官方文档
#3
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.Net.Mail命名空间发送电子邮件。我会考虑使用StringBuilder或String.Format将详细信息放入电子邮件正文中。
#4
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.
在asp.net v2.0中(系统地)弃用了System.web.mail;你应该忽略那个答案,然后倾听那些指向你使用system.net.mail的人。
#5
Have a look at the MailMessage class. That MSDN page contains a complete sample including how to handle attachments.
看看MailMessage类。该MSDN页面包含完整示例,包括如何处理附件。
Or look at this short tutorial in Scott Guthries blog - it also explains the required entries in the configuration file (web.config).
或者查看Scott Guthries博客中的这个简短教程 - 它还解释了配置文件(web.config)中的必需条目。
#6
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.
在ASP.NET中发送电子邮件非常简单。 Jsut在代码中生成消息的内容,拉入固定和动态元素以创建正文的完整字符串,然后简单地将其发送为纯文本或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.
内容的动态部分仅仅是一个字符串构建器或类似方法,用于从您需要的固定和动态元素构建字符串,如果您想使用HTML电子邮件等,则添加HTML格式。
#1
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.
警告:我对此问题的原始答案建议使用System.Web.Mail发送电子邮件。但是,这个API已经被.NET 2.0中的System.Net.Mail所取代,而System.Web.Mail的类现在都被标记为过时/弃用。
You can use the System.Web.Mail.Mailmessage class:
您可以使用System.Web.Mail.Mailmessage类:
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);
#2
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.
警告:此问题的已接受答案建议使用System.Web.Mail发送电子邮件。但是,这个API已经被.NET 2.0中的System.Net.Mail所取代,而System.Web.Mail的类现在都被标记为过时/弃用。
Here is the a real simple example of how to construct and send a mail message in .NET:
以下是如何在.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.Send()方法可以将上面的代码写得更短:
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:
无论您选择哪种方式,都需要根据SMTP设置配置应用程序,告诉它如何发送电子邮件。在您的应用程序配置文件(即ASP.NET应用程序中的web.config)中,您需要添加如下内容:
<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.
确切的设置取决于您希望应用程序如何传递邮件。使用上面的示例配置,邮件将通过指定的SMTP服务器进行中继。其他解决方案包括让应用程序将消息写入拾取文件夹,本地IIS虚拟邮件服务器将从该文件夹处理它们。有关详细信息,请参阅官方文档
#3
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.Net.Mail命名空间发送电子邮件。我会考虑使用StringBuilder或String.Format将详细信息放入电子邮件正文中。
#4
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.
在asp.net v2.0中(系统地)弃用了System.web.mail;你应该忽略那个答案,然后倾听那些指向你使用system.net.mail的人。
#5
Have a look at the MailMessage class. That MSDN page contains a complete sample including how to handle attachments.
看看MailMessage类。该MSDN页面包含完整示例,包括如何处理附件。
Or look at this short tutorial in Scott Guthries blog - it also explains the required entries in the configuration file (web.config).
或者查看Scott Guthries博客中的这个简短教程 - 它还解释了配置文件(web.config)中的必需条目。
#6
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.
在ASP.NET中发送电子邮件非常简单。 Jsut在代码中生成消息的内容,拉入固定和动态元素以创建正文的完整字符串,然后简单地将其发送为纯文本或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.
内容的动态部分仅仅是一个字符串构建器或类似方法,用于从您需要的固定和动态元素构建字符串,如果您想使用HTML电子邮件等,则添加HTML格式。