在 .NET 1.1 中发送复杂邮件






4.44/5 (30投票s)
2006年2月28日
2分钟阅读

203917

1151
本文描述了在 .NET 1.1 中发送邮件的复杂问题(例如使用需要身份验证的 SMTP 服务器)。
引言
可以使用 System.Web.Mail
从 .NET 1.1 应用程序发送邮件。发送简单邮件非常容易。当您尝试使用需要身份验证的 SMTP 服务器发送邮件,或者仅仅需要格式化要发送的邮件的 发件人 名称时,就会变得更加复杂。
背景
这是使用 C# 发送邮件的最简单代码示例:
// build the email message
MailMessage msg = new MailMessage();
msg.From = "from.email@domain.com";
msg.To = "to.email@domain.com";
msg.Subject = "Subject";
msg.Body = "Body";
// send the message
SmtpMail.SmtpServer = "smtp.server.com";
SmtpMail.Send(msg);
问题和直接解决方案
如果不是显示 发件人 电子邮件地址,而是希望显示收件人看到的名称,则事情需要变得更加复杂。为此,需要添加一个自定义标头。
string sFromName = "From display name";
string sFromAddress = "from.email@domain.com";
msg.Headers.Add("From", string.Format("{0} <{1}>",
sFromName, sFromAddress));
使用需要身份验证的 SMTP 服务器发送邮件会更加复杂。为此,需要使用 MailMessage
对象的 Fields
集合。以下代码示例可以帮助您解决问题:
// set SMTP server name
msg.Fields["http://schemas.microsoft.com" +
"/cdo/configuration/smtpserver"] = "smtp.server.com";
// set SMTP server port
msg.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/smtpserverport"] = 25;
msg.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/sendusing"] = 2;
msg.Fields["http://schemas.microsoft.com/cdo/" +
"configuration/smtpauthenticate"] = 1;
// set SMTP username
msg.Fields["http://schemas.microsoft.com/cdo" +
"/configuration/sendusername"] = "username";
// set SMTP user password
msg.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/sendpassword"] = "password";
更好的解决方案
对于上述增强功能,更好的解决方案是创建一个从 MailMessage
继承的新类,并具有额外的功能。以下是新类的内容:
/// <summary>
/// EnhancedMailMessage is a class that provides
/// more features for email sending in .NET
/// </summary>
public class EnhancedMailMessage : MailMessage
{
private string fromName;
private string smtpServerName;
private string smtpUserName;
private string smtpUserPassword;
private int smtpServerPort;
private bool smtpSSL;
public EnhancedMailMessage()
{
fromName = string.Empty;
smtpServerName = string.Empty;
smtpUserName = string.Empty;
smtpUserPassword = string.Empty;
smtpServerPort = 25;
smtpSSL = false;
}
/// <summary>
/// The display name that will appear
/// in the recipient mail client
/// </summary>
public string FromName
{
set
{
fromName = value;
}
get
{
return fromName;
}
}
/// <summary>
/// SMTP server (name or IP address)
/// </summary>
public string SMTPServerName
{
set
{
smtpServerName = value;
}
get
{
return smtpServerName;
}
}
/// <summary>
/// Username needed for a SMTP server
/// that requires authentication
/// </summary>
public string SMTPUserName
{
set
{
smtpUserName = value;
}
get
{
return smtpUserName;
}
}
/// <summary>
/// Password needed for a SMTP server
/// that requires authentication
/// </summary>
public string SMTPUserPassword
{
set
{
smtpUserPassword = value;
}
get
{
return smtpUserPassword;
}
}
/// <summary>
/// SMTP server port (default 25)
/// </summary>
public int SMTPServerPort
{
set
{
smtpServerPort = value;
}
get
{
return smtpServerPort;
}
}
/// <summary>
/// If SMTP server requires SSL
/// </summary>
public bool SMTPSSL
{
set
{
smtpSSL = value;
}
get
{
return smtpSSL;
}
}
public void Send()
{
if (smtpServerName.Length == 0)
{
throw new Exception("SMTP Server not specified");
}
if (fromName.Length > 0)
{
this.Headers.Add("From",
string.Format("{0} <{1}>",
FromName, From));
}
// set SMTP server name
this.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/smtpserver"] = smtpServerName;
// set SMTP server port
this.Fields["http://schemas.microsoft.com/cdo" +
"/configuration/smtpserverport"] = smtpServerPort;
this.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/sendusing"] = 2;
if (smtpUserName.Length >0 && smtpUserPassword.Length > 0)
{
this.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/smtpauthenticate"] = 1;
// set SMTP username
this.Fields["http://schemas.microsoft.com" +
"/cdo/configuration/sendusername"] = smtpUserName;
// set SMTP user password
this.Fields["http://schemas.microsoft.com/" +
"cdo/configuration/sendpassword"] = smtpUserPassword;
}
// ssl if needed
if (smtpSSL)
{
this.Fields.Add("http://schemas.microsoft" +
".com/cdo/configuration/smtpusessl", "true");
}
SmtpMail.SmtpServer = smtpServerName;
SmtpMail.Send(this);
}
public static void QuickSend(
string SMTPServerName,
string ToEmail,
string FromEmail,
string Subject,
string Body,
MailFormat BodyFormat)
{
EnhancedMailMessage msg = new EnhancedMailMessage();
msg.From = FromEmail;
msg.To = ToEmail;
msg.Subject = Subject;
msg.Body = Body;
msg.BodyFormat = BodyFormat;
msg.SMTPServerName = SMTPServerName;
msg.Send();
}
}
如上代码所示,您可以使用需要身份验证的 SMTP 服务器发送邮件。以下是示例用法代码:
EnhancedMailMessage msg = new EnhancedMailMessage();
msg.From = "from.email@domain.com";
msg.FromName = "From display name";
msg.To = "to.email@domain.com";
msg.Subject = "Subject";
msg.Body = "Body";
msg.SMTPServerName = "smtp.server.com";
msg.SMTPUserName = "username";
msg.SMTPUserPassword = "password";
msg.Send();
有些 SMTP 服务器需要 SSL。一个非常著名的例子是 Gmail。为了使用 Gmail SMTP 服务器发送邮件,您需要指定该服务器并设置正确的端口。以下示例可用于使用 Gmail SMTP 发送邮件:
EnhancedMailMessage msg = new EnhancedMailMessage();
msg.From = "your.address@gmail.com";
msg.FromName = "Your name";
msg.To = "to.email@domain.com";
msg.Subject = "Test email from gmail";
msg.Body = "Gmail is great";
msg.SMTPServerName = "smtp.gmail.com";
msg.SMTPUserName = "your.address@gmail.com";
msg.SMTPUserPassword = "yourpassword";
msg.SMTPServerPort = 465;
msg.SMTPSSL = true;
msg.Send();
此外,您可以使用一行代码发送邮件:
EnhancedMailMessage.QuickSend("smtp.server.com",
"to.email@domain.com",
"from.email@domain.com",
"Subject",
"Body",
MailFormat.Html);
结论
可以使用简单的 .NET Framework 类完成复杂的事情。我将在我下一篇文章中解释更多关于发送邮件的内容。
历史
- 2006 年 2 月 27 日 - 文章的初稿。
- 2006 年 2 月 28 日 - 为所有代码创建了一个包装类,并创建了属性。
- 2006 年 3 月 1 日 - 在类中添加了更多注释,并改进了性能和编码风格。
- 2006 年 3 月 16 日 - 添加了对需要 SSL 的 SMTP 服务器的支持。