473,836 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SmtpClient Problem

Hi,

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

<configuratio n>
<system.net>
<mailSettings >
<smtp>
<network host="mysmtp" port="25" userName="myuse r"
password="mypas s" defaultCredenti als="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("to Mail"));
message.From = new MailAddress("Fr ommail");
message.Subject = "This is my subject";
message.Body = "This is the content";

SmtpClient client = new SmtpClient();
try
{
client.Send(mes sage);
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("to Mail"));
message.From = new MailAddress("Fr ommail");
message.Subject = "This is my subject";
message.Body = "This is the content";

SmtpClient client = new SmtpClient("myS MTP", 25);
NetworkCredenti al SMTPUserInfo = new NetworkCredenti al
("myUser", "myPass");
client.UseDefau ltCredentials = false;
client.Credenti als = SMTPUserInfo;
try
{
client.Send(mes sage);
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 3920
<snip>

Just to confirm - if I use MailSettingsSec tionGroup 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.space s.live.com/Blog/cns!A68482B9628 A842A!138.entry

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

............... ...
'You can probably find a way to get it working.......b ut 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******** *************** ***********@h23 g2000prf.google groups.com...
Hi,

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

<configuratio n>
<system.net>
<mailSettings >
<smtp>
<network host="mysmtp" port="25" userName="myuse r"
password="mypas s" defaultCredenti als="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("to Mail"));
message.From = new MailAddress("Fr ommail");
message.Subject = "This is my subject";
message.Body = "This is the content";

SmtpClient client = new SmtpClient();
try
{
client.Send(mes sage);
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("to Mail"));
message.From = new MailAddress("Fr ommail");
message.Subject = "This is my subject";
message.Body = "This is the content";

SmtpClient client = new SmtpClient("myS MTP", 25);
NetworkCredenti al SMTPUserInfo = new NetworkCredenti al
("myUser", "myPass");
client.UseDefau ltCredentials = false;
client.Credenti als = SMTPUserInfo;
try
{
client.Send(mes sage);
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.ne twrote:
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 defaultCredenti als="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************* *************** **...legroup s.com...
Hi,

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

<configuratio n>
<system.net>
<mailSettings >
<smtp>
<network host="mysmtp" port="25" userName="myuse r"
password="mypas s" defaultCredenti als="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("to Mail"));
message.From = new MailAddress("Fr ommail");
message.Subject = "This is my subject";
message.Body = "This is the content";

SmtpClient client = new SmtpClient();
try
{
client.Send(mes sage);
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("to Mail"));
message.From = new MailAddress("Fr ommail");
message.Subject = "This is my subject";
message.Body = "This is the content";

SmtpClient client = new SmtpClient("myS MTP", 25);
NetworkCredenti al SMTPUserInfo = new NetworkCredenti al
("myUser", "myPass");
client.UseDefau ltCredentials = false;
client.Credenti als = SMTPUserInfo;
try
{
client.Send(mes sage);
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 defaultCredenti als="false" instead ?
Yes that worked. Thanks.

The documentation (that I have) on this is sparse. What does
defaultCredenti als 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
8297
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. ----------------------------------------------------------- The code (I changed the server, user, and pass for obvious reasons): SmtpClient client = new SmtpClient("mail.server.com"); client.Credentials = new NetworkCredential("user", "pass");
11
7739
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 the app, even the first time. The application will be required to be sending out repeated emails, about one every second or two. Must this be done asynchronously? Thank you. -Greg I get the generic error messages;
6
10498
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 we.config, and it works. However, I would like to place it in my web.config file, and then just get the value from the web.config? Can someone tell me what I need to add to the web.config
2
4059
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", "to@xxxxxx", "TEST", ""); msg.Headers.Add("Content-Type", "application/pkcs7-mime; smime-type=signed-data; name=\"smime.p7m\"");
10
3019
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 for their version in the book to the best of my abiltiies, and came across issues, mostly that a type was expected when trying to compile in MS Visual Studio 2002. When my stuff wasn't working at all, I decided to try just using his code almost...
0
2416
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 when the SMTPClient attaches to the server and the server sends it greeting message, if anything other than +OK is received by the client it throws an exception and falls over.
6
5490
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 it sometimes works too, but often does not. If it does not, he will never - until the next reboot. Then the "game" starts again. I get the exception "'No connection could be made because the target machine actively refused it.". Telnet to port 25...
7
22886
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 outlook or outlook express it works fine.. System.Net.Mail.MailMessage msgMail = new System.Net.Mail.MailMessage("myemail@mydomain.com", "testemail@mydomain.com", "subject", "message body");
2
3838
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 hosting company can't find what's wrong with it so I'm here hoping somebody can help. The hosting company tried sending email from the server and it was okay and found no restrictions that prevents my page to access the smtp server. What could cause...
0
9810
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9656
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10819
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10526
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10240
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5641
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3100
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.