Failure sending mail Exception




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:

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?





Mark said:
Presumably you have actually created the SendCompleted method? It's not
enough just to wire it up:

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

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 app and
it worked -- so presumably it is not an authentication issue.
Try sending from ASP.NET firstly with your own credentials:

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:
I've literally copied and pasted the 4.6 example (of
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... ??



Paul Shapiro code is running under the security account that's in the
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 Maybe that's what you meant, but in case it's not, you could
try changing the account for the process.

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