yaapelsinko yaapelsinko - 1 year ago 90
ASP.NET (C#) Question

web.config contents doesn't affects SmtpClient() configuration

I have a problem trying to configure mail sending via SmtpClient.

I configure it trough web.config file with code like:

<smtp from="mail@gmail.com" deliveryMethod="Network">
<network enableSsl="true" host="smtp.gmail.com" port="587" userName="mail@gmail.com" password="pass" />

Then sending a message:

mailMessage = new MailMessage("mail@gmail.com", "mail@gmail.com", Subj, message.ToString());
using (var smtpClient = new SmtpClient()) {

But at this moment I'm receiving an exception saying that SMTP-host isn't specified.
I tried to initialize it this way: new SmtpClient("smtp.gmail.com"), then I received another exception about SSL required but not configured.

So, obviously, web.config content is just ignored. How do I specify that all necessary setting are actually there?

Dai Dai
Answer Source

Your web.config's contents don't look like the application-root web.config, but something stored in a subdirectory (e.g. mywebsite.com/foo/web.config instead of mywebsite.com/web.config)

Move your <system.net><mailSettings> configuration elements to the web.config file in the root of your application and then it should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download