2boolORNOT2bool
2boolORNOT2bool

Reputation: 567

C# smtp configuration in system.net.mail

I have a small WinForms application that sends notification emails. It works fine with outlook clients but bombs if a gmail/yahoo/windowslive address is entered. I've seen many posts using system.web.mail and schemas but Visual Studio 2010 gives me an error when trying to use this (I'm assuming because of .Net 2.0). Is it possible to configure my smtp client code to relay to all of these email providers?

Im using system.net.mail with MailMessage. My configuration code is below...

SmtpClient mailSender = new SmtpClient("smtp.myclient.com");
            mailSender.EnableSsl = true;
            mailSender.Send(message);

Upvotes: 1

Views: 2314

Answers (3)

Csaba Toth
Csaba Toth

Reputation: 10697

You can get an exception message even if your credentials supplied to the mail servers would be OK by themselves, but the "From" field doesn't match that particular user. (This is a feature, a restriction enforced by the SMTP servers to prevent spoofing a sender. The SMTP protocol itself is so liberal, that in theory you can write any e-mail address into the "From" field. Preventing spoofing is very important to fight against e-mail spam.)

In general if you do something wrong, you'll receive an SmtpException, which is unexpectedly descriptive. Sometimes the embedded inner exception distinguish more cases. You just have to debug the various cases, I sorted out some:

  1. If the "from" address of the e-mail doesn't match the e-mail address of the authenticated user what he/she is entitled to, you'll get an SmtpException saying "Mailbox unavailable." and "Client does not have permissions to send as this sender".
  2. If you specify a wrong host or port, the SmtpException message will start with "Mailbox unavailable.", it'll have an inner exception of type WebException saying "Unable to connect to the remote server", which will have an inner exception itself of a type SocketException, saying "No connection could be made because the target machine actively refused it".
  3. If you turn on SSL (mailSender.EnableSsl = true; // for your code), but the SMTP server doesn't support it, or the other way around, then you'll get an SmtpException saying "Server does not support secure connections".
  4. If the credentials (username/password) are wrong or some other SSL problem cases you'll get an SmtpExeption saying "The SMTP server requires a secure connection or the client was not authenticated".

Another thing to pay attention is how exactly you create your NetworkCredential object.

System.Net.Mail is very convenient!

Upvotes: 0

Wesley Long
Wesley Long

Reputation: 1708

It works fine for Outlook clients because the workstation is (likely) logged into the Active Directory Domain and the Exchange server "trusts" the connection because of it.

You need to add the user's credentials on the mail service, and you'll have to get them from the user:

 mailsender.Credentials = new NetworkCredential(username, password);

Also, don't forget to set the Port property for SSL. Some providers don't use the standard ones.

Upvotes: 3

Ignacio Soler Garcia
Ignacio Soler Garcia

Reputation: 21855

If your application works with outlook clients then you application sends email correctly. The only difference with web address is the way the emails are shown so maybe the problem is with the encoding. Can you detail what means that the application bombs?

Upvotes: 0

Related Questions