您的位置:首页 > 大数据 > 人工智能

System.Web.Mail - 2.0 快速入门示例

2007-07-19 09:59 471 查看
原文出自:http://www.systemwebmail.com/default.aspx

翻译:lzumcj

2 快速入门程序示例

System.Web.Mail 一系列示例,它支持设计来快速通知开发者了解在 .NET 中发送
email 的注释。快速入门示例被设计成简短的, 易于理解的 System.Web.Mail 示例。

重要的: 当测试这些示例时,总是确保:
1. 有 System.Web.dll 的引用集(reference set
2. 如果你用 C#, 确保 将"using System.Web.Mail;" 语句放在代码的顶端。或者如果你
用 VB.NET, 确保将"Imports System.Web.Mail"语句放在代码的顶端。
3. 设置正确的 FROMTO 地址。
4. 设置 SmtpMail.SmtpServer 为有效的服务器, 它允许中继(relaying)你的
FROM email address 或者你发送 email 的 IP 地址。

2.1 如何发送简单的 email?

下例演示(demonstrates)了发送一个简单的文本(text) email。

[ C# ]

MailMessage mail = new MailMessage();
mail.To = "me@mycompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
//your real server goes here SmtpMail.Send( mail );
SmtpMail.SmtpServer = "localhost";

[ VB.NET ]


Dim mail As New MailMessage()
mail.To = "me@mycompany.com"
mail.From = "you@yourcompany.com"
mail.Subject = "this is a test email."
mail.Body = "this is my test email body"
'your real server goes here SmtpMail.Send(mail)
SmtpMail.SmtpServer = "localhost"


2.2 如何发送简单的 Html email?


默认情况下, 用 System.Web.Mail 发送的 email 是纯文本(plain text)
格式。为格式化为 Html, 设置 MailMessage.BodyFormat 属性为
MailFormat.Html。

[ C# ]

MailMessage mail = new MailMessage();
mail.To = "me@mycompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "this is a test email.";
mail.BodyFormat = MailFormat.Html;
// 邮件格式 mail.Body = "this is my test email body.";
//your real server goes here SmtpMail.Send( mail );
SmtpMail.SmtpServer = "localhost";

[ VB.NET ]

Dim mail As New MailMessage()
mail.To = "me@mycompany.com"
mail.From = "you@yourcompany.com"
mail.Subject = "this is a test email."
mail.BodyFormat = MailFormat.Html
mail.Body = "this is my test email body."
'your real server goes here SmtpMail.Send(mail)
SmtpMail.SmtpServer = "localhost"
2.3 如何发送带附件(attachments)的 email?

为了发送带附件(attachments)的 email, ASP.NET 进程(process)
(或者 ASP.NET 模拟(impersonated)帐号)需要被允许读文件,
并且附加到 MailMessage 类(class)。

注意: 附件(Attachments)仅能从文件系统创建。System.Web.Mail
不支持直接从 strings, byte arrays, streams, 或者已上传(uploaded)
文件创建附件。 为了直接从这些类型创建附件(attachments), 可以利用
aspNetEmail, 否则, 附件(attachment)内容必须首先保存于文件系统。

下例演示了添加文件 "test.txt" 作为一个 MailMessage 的附件。

[ C# ]

MailMessage mail = new MailMessage();
mail.To = "me@mycompany.com";
mail.From = "you@yourcompany.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body.";
//创建附件
MailAttachment attachment = new MailAttachment( Server.MapPath( "test.txt" ) );
//添加附件
mail.Attachments.Add( attachment );
//your real server goes here SmtpMail.Send( mail );
SmtpMail.SmtpServer = "localhost";


[ VB.NET ]

Dim mail As New MailMessage()
mail.To = "me@mycompany.com"
mail.From = "you@yourcompany.com"
mail.Subject = "this is a test email."
mail.Body = "this is my test email body."
'create the attachment
Dim attachment As New MailAttachment(Server.MapPath("test.txt"))
'add the attachment
mail.Attachments.Add(attachment)
'your real server goes here SmtpMail.Send(mail)
SmtpMail.SmtpServer = "localhost"

译者注:在实际操作中,发送的附件必须位于服务器端,发送附件才会成功。
因此,在客户端发送带附件的邮件时,必须先将附件上传至服务器端。

2.4 如何将 FROM 地址改成友好的名字?

通常, 许多人将 MailMessage 类(class)的 FROM 属性设置成这样: 
mail.From = "me@mycompny.com"
这样做的话,email 地址将会显示在大多数 email阅读器,比如 Outlook, Outlook Express, 或者 Eudora 的 FROM 行。为了显示 友好的名字(而不是 email 地址), 可以利用下面的语法。
mail.From = "\"John Smith\" "
下面的代码片断(snippet)演示了这样技术。 [ C# ] MailMessage mail = new MailMessage(); mail.To = "me@mycompany.com"; mail.From = "\"John Smith\" "; // 友好的名字 mail.Subject = "this is a test email."; mail.Body = "this is my test email body."; //your real server goes here SmtpMail.Send( mail ); SmtpMail.SmtpServer = "localhost";


[ VB.NET ]
Dim mail As New MailMessage()
mail.To = "me@mycompany.com"
mail.From = """John Smith"" "
mail.Subject = "this is a test email."
mail.Body = "this is my test email body."
'your real server goes here SmtpMail.Send(mail)
SmtpMail.SmtpServer = "localhost"
2.5 如何将 TO 地址改成友好的名字?

用来显示友好的 FROM 名字的技术, 和用来显示友好的 TO 与 CC 字段
的技术一样。通常典型的 .To 代码像这样: 
mail.To = "me@mycompny.com"
这样做的话,email 地址将会显示在大多数 email阅读器,比如 Outlook, Outlook Express, 或者 Eudora 的 TO 行。为了显示 友好的名字(而不是 email 地址), 可以利用下面的语法。
mail.To = "\"Jane Doe\" ""
下面的代码片断(snippet)演示了这样技术。 [ C# ] MailMessage mail = new MailMessage(); mail.To = "\"Jane Doe\" "; // 友好的名字 mail.From = "\"John Smith\" "; mail.Subject = "this is a test email."; mail.Body = "this is my test email body."; //your real server goes here SmtpMail.Send( mail ); SmtpMail.SmtpServer = "localhost"; [ VB.NET ]

Dim mail As New MailMessage()
mail.To = """Jane Doe"" "
mail.From = """John Smith"" "
mail.Subject = "this is a test email."
mail.Body = "this is my test email body."
'your real server goes here SmtpMail.Send(mail)

SmtpMail.SmtpServer = "localhost"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: