yaapelsinko yaapelsinko - 9 days ago 6
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:

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


Then sending a message:

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


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

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.