Failure sending mail Exception

  • Thread starter Thread starter Ramon
  • Start date Start date
R

Ramon

Hi,

I'm trying to send an HTML e-mail (using ASP.NET 3.5) by initialising a
SmtpClient object and calling the method SendAsync(). For some reason,
it throws System.Net.Mail.SmtpException -- "Failure sending mail.".

Strangely enough, e-mails sent using Send() method (i.e. synchronous)
were all delivery successfully!

Here is my code:

[MethodImpl(MethodImplOptions.Synchronized)]
public static void SendHtmlEmail(
String emailAddress,
String name,
String subject,
String message,
SendCompletedEventHandler emailEventHandler)
{
MailMessage mailMessage = new MailMessage();

mailMessage.To.Add(new MailAddress(emailAddress));
mailMessage.Subject = subject;
mailMessage.IsBodyHtml = true;
mailMessage.Body = message;

SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.SendCompleted += emailEventHandler;
smtpClient.SendAsync(mailMessage, "e-mail");
}

Can somebody figure out what am I doing wrong?

Thanks.
 
Mark said:
Presumably you have actually created the SendCompleted method? It's not
enough just to wire it up:
http://www.systemnetmail.com/faq/4.6.aspx

Also, if you set EnableSsl to false just as a test, does it work then?

Hi I copied and pasted the code in a C# application and it worked great.
For some reason, the same code does not work in ASP... very strange
indeed!

Is there perhaps a configuration that I need to perform to the server??
 
Mark said:
Firstly, when you say "ASP", presumably you mean ASP.NET, not ASP.
Obviously, this won't work at all in ASP...
Yes I'm using ASP.NET 3.5
Assuming you do mean ASP.NET, it's almost certainly to do with
authentication. Ordinarily, the account under which ASP.NET apps run has
very limited permissions.
I've run the ASP.NET application as both admin and limited user, and it
did not work in both cases. I've run the same code on a C#.net app and
it worked -- so presumably it is not an authentication issue.
Try sending from ASP.NET firstly with your own credentials:
http://www.systemnetmail.com/faq/4.2.aspx

If that works, then maybe think about creating a dedicated account for
ASP.NET to use.

However, be aware that this may not work with SSL:
http://www.systemnetmail.com/faq/5.2.aspx
I've literally copied and pasted the 4.6 example (of systemnetmail.com)
on a C#.NET application (using SSL, server authentication) and it worked
great. HOWEVER, the same code did not work on ASP.NET.

Maybe this is a .NET framework bug... ??
 
Asp.net code is running under the security account that's in the asp.net
process on the server, not the security account of the user running the
application. That would explain why it worked on a desktop application but
not asp.net. Maybe that's what you meant, but in case it's not, you could
try changing the account for the asp.net process.
 
Back
Top