SmtpClient Problem

B

BigDave

Hi,

I'm having problems with SmtpClient. I have the following set in my
web.config:

<configuration>
<system.net>
<mailSettings>
<smtp>
<network host="mysmtp" port="25" userName="myuser"
password="mypass" defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
.... lots of other stuff
</configuration>

Now, this code doesn't send the mail (which is the problem)

protected void SendEmail_Click(object sender, EventArgs e)
{

MailMessage message = new MailMessage();
message.To.Add(new MailAddress("toMail"));
message.From = new MailAddress("Frommail");
message.Subject = "This is my subject";
message.Body = "This is the content";

SmtpClient client = new SmtpClient();
try
{
client.Send(message);
Response.Write("Email sent correctly");
}
catch (Exception exc)
{
Response.Write("Send failure: " + exc.ToString());
}
}

But - this does work:

protected void SendEmail_Click(object sender, EventArgs e)
{

MailMessage message = new MailMessage();
message.To.Add(new MailAddress("toMail"));
message.From = new MailAddress("Frommail");
message.Subject = "This is my subject";
message.Body = "This is the content";

SmtpClient client = new SmtpClient("mySMTP", 25);
NetworkCredential SMTPUserInfo = new NetworkCredential
("myUser", "myPass");
client.UseDefaultCredentials = false;
client.Credentials = SMTPUserInfo;
try
{
client.Send(message);
Response.Write("Email sent correctly");
}
catch (Exception exc)
{
Response.Write("Send failure: " + exc.ToString());
}

}

So, it looks as though if I set the server and username etc manually,
it works, but if I use the default from web.config it doesn't.

Now, logic might say that it isn't reading the data from the
web.config, but I think it might be (at least partially) because I can
see the smtp server in the SmtpClient properties.

Any idea?

Thanks

David
 
B

BigDave

<snip>

Just to confirm - if I use MailSettingsSectionGroup to get the config
settings, they are all there.


So this is quite confusing - if I use the default settings it doesn't
work, if I set them manually it does.
 
B

BigDave

Yeah.

Don't rely on the default configuration stuff.

Thanks, that's a good idea - using XML. And I think I'll do that as a
solution.

It's still beyond me why the default stuff isn't working. If anyone
knows I'd be grateful.


David
 
B

BigDave

The config file is using the default credentials while your code doesn't.
What if you use defaultCredentials="false" instead ?

Yes that worked. Thanks.

The documentation (that I have) on this is sparse. What does
defaultCredentials do?

I assumed that if it was set to true, and you set up the email
settings like I have, it would use *those* automatically? Obviously
this isn't the case - if you could point me towards some documentation
that would be great.

Thanks again.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top