471,348 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Not getting status event from SmtpClient

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("me@my.email.address", "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
Jun 27 '08 #1
1 3731
Don't worry about it, I restructured the code to use exceptions instead.

Steve
"Steve Barnett" <no****@nodomain.comwrote in message
news:uD**************@TK2MSFTNGP03.phx.gbl...
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("me@my.email.address", "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


Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by hazz | last post: by
6 posts views Thread by theWizard1 | last post: by
10 posts views Thread by David Thole | last post: by
6 posts views Thread by =?Utf-8?B?VG9yc3Rlbg==?= | last post: by
1 post views Thread by Ronak mishra | last post: by

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.