Not getting status event from SmtpClient

S

Steve Barnett

I'm trying to send emails using the SmtpClient class and, while the messages
get sent, I can't seem to get any status information to that effect. I've
hooked in to the SendCompleted event, but it never fires. Can anyone suggest
why or tell me how to find out whether the email was sent or not?

The code I'm using is as follows (credentials removed to protect the
guilty):

using System;
using System.Text;
using System.Net.Mail;

namespace SendStuffBySmtp
{
public delegate void MailSendResults(String resultsMessage);

class Emails
{
public event MailSendResults MailResults;

private MailAddress fromAddress = new
MailAddress("(e-mail address removed)", "My Name");
private SmtpClient smtp = new SmtpClient("my.isp.smtp.address");
private String token;

public Emails()
{
smtp.UseDefaultCredentials = true;
// Hook in to the send completed event. We want to know when
this finishes
smtp.SendCompleted += new
SendCompletedEventHandler(smtp_SendCompleted);
}


public void SendEmail(string emailTitle, string emailText, string
recipient)
{
MailMessage msg = new MailMessage(fromAddress, new
MailAddress(recipient));
msg.IsBodyHtml = true;
msg.Body = emailText;
msg.BodyEncoding = System.Text.Encoding.UTF8;

msg.Subject = emailTitle;
msg.SubjectEncoding = System.Text.Encoding.UTF8;

msg.From = fromAddress;
msg.ReplyTo = fromAddress;
msg.Sender = fromAddress;

token = recipient;
smtp.Send(msg);
}

void smtp_SendCompleted(object sender,
System.ComponentModel.AsyncCompletedEventArgs args)
{
if (MailResults != null)
{
// Get the unique identifier for this asynchronous
operation.
string resultsText = null;

if (args.Cancelled)
{
resultsText = String.Format("[{0}] Send canceled.",
token);
}
if (args.Error != null)
{
resultsText = String.Format("[{0}] {1}", token,
args.Error.ToString());
}
else
{
resultsText = String.Format("[{0}] Message sent.",
token);
}

MailResults(resultsText);
}
}
}
}

I've put a breal point in the smtp_SendCompleted event code but it never
gets called.

I also tried using SendAsync() but that didn't fire the SendCompleted event
either.

Thanks
Steve
 
S

Steve Barnett

Don't worry about it, I restructured the code to use exceptions instead.

Steve
 

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

Top