Sending E-Mails from ASP.NET 2.0 page using System.Net.Mail

G

Guest

Greetings,

I have been searching the web like mad for a solution to my SMTP problem. I
am using Windows Server 2003 and ASP.NET 2.0 w/ C# to send out e-mails from a
web site I have created to the members of my organization. I think my problem
is incorrectly setting the settings on my server or an authentication
problem. Here is the code I have written to send a test message:

-----Code Begins: Sensitive Information Replaced by [SOMETHING]-----
MailAddress from = new MailAddress([ADDRESS], [NAME]);
MailAddress to = new MailAddress([ADDRESS], [NAME]);
MailAddress copy = new MailAddress([ADDRESS], [NAME]);

MailMessage message = new MailMessage(from, to);
message.Subject = "Using the SmtpClient class.";
message.IsBodyHtml = true;
message.Body = @"Using this feature, you can send an e-mail message from an
application very easily.";
message.CC.Add(copy);

NetworkCredential authInfo = new NetworkCredential([USERNAME], [PASSWORD]);
SmtpClient client = new SmtpClient([DOMAIN NAME], 25);
client.UseDefaultCredentials = false;
client.Credentials = authInfo;
client.Send(message);
-----Code Ends-----

On Windows Server 2003, I have created an account and placed it in the SMTP
Users group and granted this group access to the SMTP Service. Under
Authentication, I checked Anonymous Access and Integrated Windows
Authentication. In the Outbound Security, I selected Integrated Windows
Authentication and provided the username and password for the account I
created previously.

When click submit to send the e-mail, this is the error I receive (sorry for
copying and pasting this long error report, but I don't know what is wrong):
-----Error Report Begins-----
Server Error in '/HfHv3' Application
--------------------------------------------------------------------------------
An established connection was aborted by the software in your host machine
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.Net.Sockets.SocketException: An established
connection was aborted by the software in your host machine

Source Error:
Line 88: client.UseDefaultCredentials = false;
Line 89: client.Credentials = authInfo;
Line 90: client.Send(message);
Line 91:
Line 92: //try

Source File:
c:\Inetpub\wwwroot\HfHv3\intranet\board\mailinglists\sendemail.aspx.cs
Line: 90

Stack Trace:
[SocketException (0x2745): An established connection was aborted by the
software in your host machine]
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,
SocketAddress socketAddress) +1002146
System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) +33
System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,
Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState
state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) +431

[WebException: Unable to connect to the remote server]
System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object
owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket&
abortSocket6, Int32 timeout) +1447736
System.Net.PooledStream.Activate(Object owningObject, Boolean async,
Int32 timeout, GeneralAsyncDelegate asyncCallback) +190
System.Net.PooledStream.Activate(Object owningObject,
GeneralAsyncDelegate asyncCallback) +21
System.Net.ConnectionPool.GetConnection(Object owningObject,
GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +318
System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) +227
System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) +316
System.Net.Mail.SmtpClient.GetConnection() +42
System.Net.Mail.SmtpClient.Send(MailMessage message) +1485

[SmtpException: Failure sending mail.]
System.Net.Mail.SmtpClient.Send(MailMessage message) +2074
sendemail.btnSend_Click(Object sender, EventArgs e) in
c:\Inetpub\wwwroot\HfHv3\intranet\board\mailinglists\sendemail.aspx.cs:90
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
+107

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
-----Error Report Ends-----

Any help would be greatly appreciated!! Thanks a bunch!

Eric
 
S

sloan

First thing:

You need to know if your external smtp server uses:
none
basic
or
ssl
authentication.

Here is my send an email code (2.0)



System.Net.Mail.SmtpClient email = new System.Net.Mail.SmtpClient();

email.Host = "smtp.myisp.com";

email.Port = 25; // Or 465 for smtp.gmail.com ... a SSL example

switch (serv.AuthenicationMethod) // this is MY enum...telling me which type
of authentication to use. this code won't compile, but you can see what i'm
doing // serv.AuthenicationMethod is My code... and will give you a compile
error

{

case AuthenticationType.None:

break;

case AuthenticationType.Basic:

email.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

email.UseDefaultCredentials = false;



email.Credentials = new NetworkCredential(serv.SmtpUserName,
serv.SmtpUserPassword);

break;

case AuthenticationType.SSL:

email.EnableSsl = true;

email.Credentials = new NetworkCredential(serv.SmtpUserName,
serv.SmtpUserPassword);

//email.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network ;

break;

}





System.Net.Mail.MailMessage mailMsg = new
System.Net.Mail.MailMessage(fromAddress, toAddress, emailSubject, sBody);



mailMsg.IsBodyHtml = true;



email.Send(mailMsg);



I don't see email.EnableSsl = true; in your code.

...


Now, the second thing .... is that there is a difference between the Code
failing, and the server not Sending.

In Win 2000, sometimes C# code would work, but would get "stuck"

In win2000, the location was
C:\Inetpub\mailroot\Queue

I don't know about 2003 unforunately. But if there is an equivalent folder,
where emails are "dropped in" for sending, you can look there, and see if
there are entries in there.

The way I fixed this in win2000, was to set up a "smart host" under IIS,
under the email settings.


HTH a little.

Also check:
http://www.systemnetmail.com/default.aspx





Eric Sheu said:
Greetings,

I have been searching the web like mad for a solution to my SMTP problem. I
am using Windows Server 2003 and ASP.NET 2.0 w/ C# to send out e-mails from a
web site I have created to the members of my organization. I think my problem
is incorrectly setting the settings on my server or an authentication
problem. Here is the code I have written to send a test message:

-----Code Begins: Sensitive Information Replaced by [SOMETHING]-----
MailAddress from = new MailAddress([ADDRESS], [NAME]);
MailAddress to = new MailAddress([ADDRESS], [NAME]);
MailAddress copy = new MailAddress([ADDRESS], [NAME]);

MailMessage message = new MailMessage(from, to);
message.Subject = "Using the SmtpClient class.";
message.IsBodyHtml = true;
message.Body = @"Using this feature, you can send an e-mail message from an
application very easily.";
message.CC.Add(copy);

NetworkCredential authInfo = new NetworkCredential([USERNAME], [PASSWORD]);
SmtpClient client = new SmtpClient([DOMAIN NAME], 25);
client.UseDefaultCredentials = false;
client.Credentials = authInfo;
client.Send(message);
-----Code Ends-----

On Windows Server 2003, I have created an account and placed it in the SMTP
Users group and granted this group access to the SMTP Service. Under
Authentication, I checked Anonymous Access and Integrated Windows
Authentication. In the Outbound Security, I selected Integrated Windows
Authentication and provided the username and password for the account I
created previously.

When click submit to send the e-mail, this is the error I receive (sorry for
copying and pasting this long error report, but I don't know what is wrong):
-----Error Report Begins-----
Server Error in '/HfHv3' Application.
-------------------------------------------------------------------------- ------
An established connection was aborted by the software in your host machine
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.Net.Sockets.SocketException: An established
connection was aborted by the software in your host machine

Source Error:
Line 88: client.UseDefaultCredentials = false;
Line 89: client.Credentials = authInfo;
Line 90: client.Send(message);
Line 91:
Line 92: //try

Source File:
c:\Inetpub\wwwroot\HfHv3\intranet\board\mailinglists\sendemail.aspx.cs
Line: 90

Stack Trace:
[SocketException (0x2745): An established connection was aborted by the
software in your host machine]
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,
SocketAddress socketAddress) +1002146
System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) +33
System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,
Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState
state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) +431

[WebException: Unable to connect to the remote server]
System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object
owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket&
abortSocket6, Int32 timeout) +1447736
System.Net.PooledStream.Activate(Object owningObject, Boolean async,
Int32 timeout, GeneralAsyncDelegate asyncCallback) +190
System.Net.PooledStream.Activate(Object owningObject,
GeneralAsyncDelegate asyncCallback) +21
System.Net.ConnectionPool.GetConnection(Object owningObject,
GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +318
System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) +227
System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) +316
System.Net.Mail.SmtpClient.GetConnection() +42
System.Net.Mail.SmtpClient.Send(MailMessage message) +1485

[SmtpException: Failure sending mail.]
System.Net.Mail.SmtpClient.Send(MailMessage message) +2074
sendemail.btnSend_Click(Object sender, EventArgs e) in
c:\Inetpub\wwwroot\HfHv3\intranet\board\mailinglists\sendemail.aspx.cs:90
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
+107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePo
stBackEvent(String eventArgument) +7
 

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,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top