Reputation: 567
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
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:
Another thing to pay attention is how exactly you create your NetworkCredential object.
System.Net.Mail is very convenient!
Upvotes: 0
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
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