简体   繁体   中英

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)

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. 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:

System.Net.Mail.SmtpException: Server does not support secure connections.

When I google this I read allot about connecting to port 465, etc. but this is not legacy SMTP over SSL aka. SSMTP/SMTPS. You can read more here: 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.

The other thing that shows up when I google this is to try with

client.EnableSsl = false;

when I do this I get the following error:

System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. 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:

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:

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

As far as your Windows server not allowing outbound SSL, you will need to work on your firewall configuration. If you disable the firewall temporarily, that error should go away.

SSL does not change the way you authenticate, it only encrypts the connection. After you disabled SSL, you successfully talked to the SMTP server but you were rejected for invalid credentials.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM