在现代编程中,发送电子邮件是一项常见的任务。使用C#发送电子邮件看似简单,但当涉及到需要认证的SMTP服务器,或者需要自定义邮件的显示名称时,事情就变得复杂起来。本文将介绍如何使用C#发送电子邮件,并处理一些常见的复杂情况。
发送一个简单的电子邮件是非常容易的。下面是一个使用C#发送电子邮件的基础代码示例:
MailMessage msg = new MailMessage();
msg.From = "from.email@domain.com";
msg.To = "to.email@domain.com";
msg.Subject = "Subject";
msg.Body = "Body";
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服务器发送邮件时,需要设置一些额外的字段:
msg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "smtp.server.com";
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;
msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "username";
msg.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "password";
这段代码展示了如何设置SMTP服务器的名称、端口、认证方式、用户名和密码。
为了简化上述操作,可以创建一个继承自MailMessage的类,该类提供了额外的功能。以下是新类的代码:
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;
}
public string FromName
{
set { fromName = value; }
get { return fromName; }
}
public string SMTPServerName
{
set { smtpServerName = value; }
get { return smtpServerName; }
}
public string SMTPUserName
{
set { smtpUserName = value; }
get { return smtpUserName; }
}
public string SMTPUserPassword
{
set { smtpUserPassword = value; }
get { return smtpUserPassword; }
}
public int SMTPServerPort
{
set { smtpServerPort = value; }
get { return smtpServerPort; }
}
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));
}
this.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = smtpServerName;
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;
this.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = smtpUserName;
this.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = smtpUserPassword;
}
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服务器和SSL支持。
以下是如何使用EnhancedMailMessage类发送电子邮件的示例:
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();
此外,还可以使用QuickSend方法简化发送过程:
EnhancedMailMessage.QuickSend("smtp.server.com", "to.email@domain.com", "from.email@domain.com", "Subject", "Body", MailFormat.Html);
对于需要SSL的SMTP服务器,如Gmail,可以设置SSL和正确的端口:
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();
通过这些示例,可以看到使用.NET Framework发送电子邮件是一件相对简单的事情。在后续的文章中,将介绍更多关于电子邮件发送的内容。