该文主要介绍了MailBee.NET Objects中SMTP认证的代码示例,欢迎您下载试用版进行运用!
MailBee.NET Objects介绍和试用点击查看>>> 大多数SMTP服务器都要求用户进行身份验证,才能将电子邮件发送到外部电子邮件地址(属于其他域的地址)。登录/密码通常与同一服务器上用户的POP3或IMAP帐户相同。
C#: Smtp mailer = new Smtp(); SmtpServer server = new SmtpServer(“smtp.company.com”, “jdoe”, “secret”); mailer.SmtpServers.Add(server); … mailer.Send(); |
VB.NET: Dim mailer As New Smtp() Dim server As New SmtpServer(“smtp.company.com”,”jdoe”,”secret”) mailer.SmtpServers.Add(server) … mailer.Send() |
在这种情况下,如果要使用Connect方法手动建立连接,过程如下:
C#: Smtp mailer = new Smtp(); SmtpServer server = new SmtpServer(“mail.domain.com”, “user@domain.com”, “pass”); mailer.SmtpServers.Add(server); mailer.Connect(); mailer.Hello(); mailer.Login(); |
VB.NET:Dim mailer As New Smtp()Dim server As New SmtpServer(“mail.domain.com”, “user@domain.com”, “pass”) mailer.SmtpServers.Add(server) mailer.Connect() mailer.Hello() mailer.Login() |
如果启用SMTP认证(由于登录名和密码在SmtpServer函数中已经确定),那么Login方法将尝试使用服务器支持的最佳(最安全)的方法在SMTP服务器上进行身份验证。你也可以强制MailBee使用指定的身份验证方法,或尝试最简单而不是最安全的方法。
以下代码可以强制MailBee使用SASL LOGIN(不安全)、SASL PLAIN(不安全)或SASL NTLM(安全)的方法。只有SASL LOGIN或SASL PLAIN暂不支持SASL NTLM:
C#: Smtp mailer = new Smtp(); SmtpServer server = new SmtpServer(“smtp.company.com”, “jdoe”, “secret”); server.AuthMethods = AuthenticationMethods.SaslLogin | AuthenticationMethods.SaslPlain | AuthenticationMethods.SaslNtlm; server.AuthOptions = AuthenticationOptions.PreferSimpleMethods; mailer.SmtpServers.Add(server); … mailer.Send(); |
VB.NET:Dim mailer As New Smtp() Dim server As New SmtpServer(“smtp.company.com”,”jdoe”,”secret”) server.AuthMethods = AuthenticationMethods.SaslLogin _ Or AuthenticationMethods.SaslPlain Or AuthenticationMethods.SaslNtlm server.AuthOptions = AuthenticationOptions.PreferSimpleMethods mailer.SmtpServers.Add(server) … mailer.Send() |
一些较旧的服务器要求客户端自己进行身份验证,但不支持SMTP身份验证。这是因为SMTP身份验证是SMTP协议的可选扩展项(称为ESMTP身份验证更为正确)。在这种情况下,你可以使用与SMTP服务器共享用户帐户数据库的POP3服务器执行POP3认证。这被称为POP-before-SMTP认证。使用MailBee,你可以采用两种方式使用POP-before-SMTP。如果POP3和SMTP服务器名称相同(例如,两个服务器都是mail.domain.com),并且POP3服务在默认端口110上运行:
C#: Smtp mailer = new Smtp(); mailer.SmtpServers.Add(“mail.domain.com”, “jdoe@domain.com”, “secret”).AuthPopBeforeSmtp = true; … mailer.Send(); |
VB.NET:Dim mailer As New Smtp() mailer.SmtpServers.Add(“mail.domain.com”, “jdoe@domain.com”, “secret”).AuthPopBeforeSmtp = True … mailer.Send() |
如果服务器名称不同(例如POP3的POP.domain.com和SMTP的smtp.domain.com)或POP3端口是非标准的,请在连接到SMTP服务器之前使用AuthPopBeforeSmtp方法。
C#: Smtp mailer = new Smtp(); mailer.SmtpServers.Add(“smtp.domain.com”); mailer.AuthPopBeforeSmtp(“pop.domain.com”, 110, “jdoe@domain.com”, “secret”); … mailer.Send(); |
VB.NET:Dim mailer As New Smtp() mailer.SmtpServers.Add(“smtp.domain.com”) mailer.AuthPopBeforeSmtp(“pop.domain.com”, 110, “jdoe@domain.com”, “secret”) … mailer.Send() |
注意:POP-before-SMTP验证完成后,你无需再调用Login方法。Login方法仅执行ESMTP认证。MailBee还支持Windows集成身份验证(使用当前记录的Windows用户的凭据登录)。例如:
C#: Smtp mailer = new Smtp(); mailer.SmtpServers.Add(“smtp.domain.com”, null, null, AuthenticationMethods.SaslNtlm); |
VB.NET:Dim mailer As New Smtp() mailer.SmtpServers.Add(“smtp.domain.com”, Nothing, Nothing, AuthenticationMethods.SaslNtlm) |
如果你开发的是在匿名IIS或ASP.NET用户文本中运行的ASP.NET应用程序(因为当前记录的用户是IIS用户而不是使用该应用程序的人员),那么你无法使用它。
试用、下载、了解更多产品信息请点击“咨询在线客服”

标签:.NET电子邮件
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!