MailBee.NET Objects发送电子邮件(SMTP)教程二:SMTP认证
来源:互联网 发布:韶关市网络问政门户 编辑:程序博客网 时间:2024/06/06 04:03
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();
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()
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();
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()
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:
以下代码可以强制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();
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()
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();
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()
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();
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()
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);
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)
mailer.SmtpServers.Add("smtp.domain.com", Nothing, Nothing, AuthenticationMethods.SaslNtlm)
如果你开发的是在匿名IIS或ASP.NET用户文本中运行的ASP.NET应用程序(因为当前记录的用户是IIS用户而不是使用该应用程序的人员),那么你无法使用它。
0 0
- MailBee.NET Objects发送电子邮件(SMTP)教程二:SMTP认证
- MailBee.NET Objects发送电子邮件(SMTP)教程一:使用SMTP服务器发送消息
- MailBee.NET Objects发送电子邮件(SMTP)教程八:使用多个SMTP服务器发送邮件
- MailBee.NET Objects发送电子邮件(SMTP)教程六:创建并发送带有附件的邮件
- MailBee.NET Objects发送电子邮件(SMTP)教程七:添加多个附件
- MailBee.NET Objects发送电子邮件(SMTP)教程三:没有SMTP服务器的情况下发送消息
- MailBee.NET Objects发送电子邮件(SMTP)教程五:发送带有嵌入图片的网页和HTML邮件
- MailBee.NET Objects退回邮件教程(一):SMTP部分
- MailBee.NET Objects接收电子邮件(POP3)教程二:接收和显示HTML电子邮件
- MailBee.NET Objects接收电子邮件(POP3)教程三:POP3认证
- ASP.NET 发送电子邮件 smtp
- MailBee.NET Objects撰写邮件教程(二):发送一个网页
- MailBee.NET Objects接收电子邮件(POP3)教程一:接收简单的电子邮件
- MailBee.NET Objects退回邮件教程(二):POP3部分
- MailBee.NET Objects接收电子邮件(POP3)教程四:访问电子邮件附件
- 在.NET中用SMTP发送电子邮件
- 使用 SMTP 发送电子邮件
- MailBee.NET Objects撰写邮件教程(三):请求阅读/发送状态提示
- bitset
- Intellij IDEA svn的使用
- python—命名规范
- CodeForces 731 A.Night at the Museum(水~)
- 正则表达式
- MailBee.NET Objects发送电子邮件(SMTP)教程二:SMTP认证
- ConcurrentHashMap详解
- SecureCRT中改变背景色和文字颜色
- react-native-vector-icons 图标库的使用
- 简明扼要的反射入门教程
- JavaEE学习笔记--JavaScript(1)
- iOS动画篇:UIView动画
- 关于Android6.0以上调用intent隐式意图拨打电话问题
- 域名关联nodejs项目