473,395 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

SmtpClient Problem

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

Nov 13 '08 #1
5 3900
<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.
Nov 13 '08 #2

Yeah.

Don't rely on the default configuration stuff.

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!138.entry

This is part of the reason I developed the "smarter" setup.

..................
'You can probably find a way to get it working.......but its so
not-clear-cut...that the alternate method I developed saves time in the long
run.


"BigDave" <bo************@gmail.comwrote in message
news:f6**********************************@h23g2000 prf.googlegroups.com...
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

Nov 13 '08 #3
On 13 Nov, 17:39, "sloan" <sl...@ipass.netwrote:
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

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

Also check the exception to see what i have to offers (it can help to sort
beetween not being able to connect to a smtp server or being able to connect
but having an authentication issue etc...)

--
Patrice
"BigDave" <bo************@gmail.coma écrit dans le message de groupe de
discussion :
f6**********************************...oglegroups.com...
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
Nov 13 '08 #5
On 13 Nov, 17:59, "Patrice" <http://www.chez.com/scribe/wrote:
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

Nov 13 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nick Z. | last post by:
I am trying to connect to my SMTP server using the SmtpClient class. For some reason its refusing to work. Here is the code and the error I get. ...
11
by: hazz | last post by:
smtpClient.Send(message) is causing me problems as per specifics in the trace below. Email is sent but not without this error typically upon sending the second email, but sometimes when running...
6
by: theWizard1 | last post by:
Using VisualStudio 2005, asp.net 2.0: What do I put in my Web.Config to avoid hard coding the: myserveraddress.com? The following shows how I have hard coded it without having the value in the...
2
by: fhtino | last post by:
Hello, I'm trying to create an email message with particular headers. A piece of code: SmtpClient smtp = new SmtpClient("192.168.x.y"); MailMessage msg = new MailMessage("from@xxxxxxx",...
10
by: David Thole | last post by:
Hey all, I'm still very new at all this, but am going through the ASP.net 2.0 unleashed book, first chapter and trying to program my own little form emailer for fun. I tried following the code...
0
by: howardr101 | last post by:
Hi, Have hunted around on the groups and can't find anything, hence. I've tried this against 2 mail servers (mailtraq and hmailserver) and it occus with both. The problems seems to be that...
6
by: =?Utf-8?B?VG9yc3Rlbg==?= | last post by:
Hi, I have a problem sending mails with the SmtpClient class. It's strange - after I boot the pc and start the program I can send mails many times. After I close the program and start it again...
7
by: Rob Dob | last post by:
The following code is giving me a timeout problem., no matter what I do I can't send a piece of mail using .net2.0 System.Net.Mail.SmtpClient via port 465 and using ssl, if however I try using...
2
by: btcoder | last post by:
Hi, my jsp page uses sun.net.smtp.SmtpClient to send email. It worked fine until the hosted location was moved to another server. Now it generates the sun.net.smtp.SmtpProtocolException and the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.