简体   繁体   English

使用TLS和SmtpAuth在.Net 4 / C#中发送带有System.Net.Mail.SmtpClient的电子邮件,可在本地使用,但不能在生产服务器上运行(win 2008 r2)

[英]Sending email with System.Net.Mail.SmtpClient in .Net 4/C# with TLS and SmtpAuth, works locally but not on production server(win 2008 r2)

I run this code successfully on my development machine (Win 7, VS 2010) 我在我的开发机器上成功运行此代码(Win 7,VS 2010)

SmtpClient client = new SmtpClient("servername.domain.tld");
client.Port = 25;
client.Credentials = new NetworkCredential("username", "password");
client.EnableSsl = true;
client.Send(new MailMessage("destination@domain.tld", "sender@domain.tld", "subject", "body"));

My smtp server is Exim 4.7 running on debian with TLS and SmtpAuth enabled. 我的smtp服务器是在debian上运行的Exim 4.7,启用了TLS和SmtpAuth。 It doesn't support relaying but if a user authenticates he can send mail to anyone. 它不支持中继,但如果用户进行身份验证,他可以向任何人发送邮件。

When I run the above code on our Win 2008 R2 production server I get this error message: 当我在Win 2008 R2生产服务器上运行上面的代码时,我收到以下错误消息:

System.Net.Mail.SmtpException: Server does not support secure connections. System.Net.Mail.SmtpException:服务器不支持安全连接。

When I google this I read allot about connecting to port 465, etc. but this is not legacy SMTP over SSL aka. 当我谷歌这个,我读了关于连接到端口465等的分配,但这不是传统的SMTP over SSL aka。 SSMTP/SMTPS. ssmtp中/ SMTPS。 You can read more here: http://www.exim.org/exim-html-current/doc/html/spec_html/ch39.html 你可以在这里阅读更多内容: http//www.exim.org/exim-html-current/doc/html/spec_html/ch39.html

So what I can understand port 25 is correct also it does work on my dev machine with port 25, TLS, etc. 所以我能理解端口25 正确的它也适用于我的开发机器端口25,TLS等。

The other thing that shows up when I google this is to try with 当我谷歌这个时出现的另一件事是尝试

client.EnableSsl = false; client.EnableSsl = false;

when I do this I get the following error: 当我这样做时,我收到以下错误:

System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. System.Net.Mail.SmtpFailedRecipientException:邮箱不可用。 The server response was: relay not permitted 服务器响应是:不允许中继

Which is correct as because if the client doesn't authenticate we don't relay mail. 这是正确的,因为如果客户端不进行身份验证,我们不会中继邮件。

Here's the smtp server output from a failing session from the server: 这是来自服务器的失败会话的smtp服务器输出:

2011-10-21 11:23:43 [2022] SMTP connection from [source.ip]:51712 I=[smtp.ip]:25 (TCP/IP connection count = 1)
2011-10-21 11:23:48 [2072] ident connection to source.ip timed out
2011-10-21 11:23:48 [2072] SMTP connection from servername.domain.tld (servername) [source.ip]:51712 I=[smtp.ip]:25 lost
2011-10-21 11:23:48 [2072] no MAIL in SMTP connection from servername.domain.tld (servername) [source.ip]:51712 I=[smtp.ip]:25 D=0s C=EHLO

Here's the smtp server output from a working session from my dev machine: 这是我的开发机器的工作会话中的smtp服务器输出:

2011-10-21 12:04:05 [2022] SMTP connection from [source.ip]:63294 I=[smtp.ip]:25 (TCP/IP connection count = 1)
2011-10-21 12:04:10 [2077] ident connection to source.ip timed out
2011-10-21 12:04:11 [2077] 1RHBxT-0000XV-7P  for to@domain.tld
2011-10-21 12:04:11 [2079] cwd=/var/spool/exim4 3 args: /usr/sbin/exim4 -Mc 1RHBxT-0000XV-7P
2011-10-21 12:04:12 [2079] 1RHBxT-0000XV-7P => to@domain.tld F= P= R=dnslookup T=remote_smtp S=1058 H=mx2.hotmail.com [65.55.37.104]:25 C="250   Queued mail for delivery" QT=1s DT=1s
2011-10-21 12:04:12 [2079] 1RHBxT-0000XV-7P Completed QT=1s

Anyone have any insights into what I can do to make this work? 任何人都对我能做些什么来深入了解这项工作?

Thanks, goddang 谢谢,goddang

As far as your Windows server not allowing outbound SSL, you will need to work on your firewall configuration. 对于不允许出站SSL的Windows服务器,您需要处理防火墙配置。 If you disable the firewall temporarily, that error should go away. 如果暂时禁用防火墙,则该错误应该消失。

SSL does not change the way you authenticate, it only encrypts the connection. SSL不会更改您的身份验证方式,它只会加密连接。 After you disabled SSL, you successfully talked to the SMTP server but you were rejected for invalid credentials. 禁用SSL后,您已成功与SMTP服务器通信,但您因无效凭据而被拒绝。

As far as why the server rejected your credentials, that could be caused by any number of issues. 至于服务器拒绝您的凭据的原因,这可能是由许多问题引起的。 It can be as simple as the username/password are incorrect or as complex as the server only allows connections from a subset of machines. 它可以很简单,因为用户名/密码不正确或复杂,因为服务器只允许来自机器子集的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用System.Net.Mail.SmtpClient将电子邮件发送到通讯组列表 - Sending email to a distribution list using System.Net.Mail.SmtpClient 使用System.Net.Mail.SmtpClient发送的电子邮件的字体大小 - Font size for an email sent using System.Net.Mail.SmtpClient System.Net.Mail.SmtpClient 在 4.7 中过时了吗? - Is System.Net.Mail.SmtpClient obsolete in 4.7? 如何使用TLS和System.Net.Mail.SMTPCLient连接到googlemail.com? - How can I connect to googlemail.com using TLS and System.Net.Mail.SMTPCLient? 如何在Xamarin中使用System.Net.Mail.SmtpClient发送邮件 - How to send a mail in Xamarin using System.Net.Mail.SmtpClient C#在Windows Server 2008 R2上使用System.Web.Mail发送认证的邮件时出错 - C# Error sending certified mail with System.Web.Mail on Windows Server 2008 R2 System.Net.Mail.SmtpClient 在发送电子邮件时使用什么类型的身份验证? - What type of authentication is System.Net.Mail.SmtpClient using when sending emails? 使用System.Net.Mail.SmtpClient,更改SMTP FROM电子邮件地址 - Using System.Net.Mail.SmtpClient, change the SMTP FROM email address 无法使用 System.Net.Mail.SmtpClient 将 email 发送到其他域 - Unable to send email to other domains using System.Net.Mail.SmtpClient 发送到 System.Net.Mail.SmtpClient 等文件夹 - Send to folder like System.Net.Mail.SmtpClient
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM