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
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