是一款为创建、发送、接收以及处理电子邮件而设计的健壮、功能丰富的.NET控件。几行代码便可为应用程序添加E-Mail支持,简单高效。具备“必需”以及独特的功能,这些控件帮助开发人员简单快速地将复杂的电子邮件功能添加到他们的应用程序中。
本文主要介绍了如何使用使用多个SMTP服务器发送邮件的代码示例。目前MailBee.NET Objects在线订购享75折优惠正在进行中,欢迎您下载试用版进行运用!
当开发人员不确定他将用于发送电子邮件的SMTP服务器是否可靠(例如,有时服务器崩溃)时,使用多个SMTP服务器则非常有用。例如,当使用单个SMTP服务器发送消息时,如果此SMTP服务器崩溃,则MailBee无法发送消息。但是,在使用多个SMTP服务器时,如果一个SMTP服务器崩溃,MailBee会根据这些服务器的优先级别尝试使用其他SMTP服务器。
因此,使用多个SMTP服务器增加了邮件发送成功的可靠性。MailBee.NET Objects允许开发人员管理SMTPSERVER对象的集合,存储在SMTP.SmtpServers的SMTP对象的属性。开发人员应调用SMTP.SmtpServers.Add方法向集合添加一个新的SmtpServer对象实例。这个方法有五个重载。第一个重载允许添加直接SmtpServer对象,如下所示:
C#: SmtpServer smtp_srv = new SmtpServer(); smtp_srv.Name = "mail.domain.com"; smtp_srv.AccountName = "john_doe"; smtp_srv.Password = "secret";oMailer.SmtpServers.Add(smtp_srv); |
VB.NET: Dim smtp_srv As SmtpServer = New SmtpServer() smtp_srv.Name = "mail.domain.com" smtp_srv.AccountName = "john_doe" smtp_srv.Password = "secret" oMailer.SmtpServers.Add(smtp_srv) |
SMTP.SmtpServers.Add方法的下一次重载允许通过指定的服务器名称或相应的IP地址添加新的SMTP服务器,如下所示:
C#: oMailer.SmtpServers.Add("127.0.0.1"); oMailer.SmtpServers.Add("smtp.company.com"); |
VB.NET: oMailer.SmtpServers.Add("127.0.0.1") oMailer.SmtpServers.Add("smtp.company.com") |
此外,开发人员可以指定SMTP服务器的端口号和优先级。默认优先级为0(即最高),默认端口号为25。以下代码将两个具有不同优先级的SMTP服务器添加到集合中:
C#: oMailer.SmtpServers.Add("127.0.0.1", 33, 1); oMailer.SmtpServers.Add("smtp.company.com", 37, 2); |
VB.NET: oMailer.SmtpServers.Add("127.0.0.1", 33, 1) oMailer.SmtpServers.Add("smtp.company.com", 37, 2) |
C#: oMailer.SmtpServers.Add("127.0.0.1", "dan_brown", "password"); oMailer.SmtpServers.Add("smtp.company.com ", "john_doe", "secret"); |
VB.NET: oMailer.SmtpServers.Add("127.0.0.1", "dan_brown", "password") oMailer.SmtpServers.Add("smtp.company.com ", "john_doe", "secret") |
以上就是本次教程的全部内容,接下来会有更多相关教程,敬请关注!您也可以在评论者留下你的经验和建议。
试用、下载、了解更多产品信息请点击""