Not getting status event from SmtpClient

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Barnett

    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.addre ss", "My Name");
    private SmtpClient smtp = new SmtpClient("my. isp.smtp.addres s");
    private String token;

    public Emails()
    {
    smtp.UseDefault Credentials = true;
    // Hook in to the send completed event. We want to know when
    this finishes
    smtp.SendComple ted += new
    SendCompletedEv entHandler(smtp _SendCompleted) ;
    }


    public void SendEmail(strin g emailTitle, string emailText, string
    recipient)
    {
    MailMessage msg = new MailMessage(fro mAddress, new
    MailAddress(rec ipient));
    msg.IsBodyHtml = true;
    msg.Body = emailText;
    msg.BodyEncodin g = System.Text.Enc oding.UTF8;

    msg.Subject = emailTitle;
    msg.SubjectEnco ding = System.Text.Enc oding.UTF8;

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

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

    void smtp_SendComple ted(object sender,
    System.Componen tModel.AsyncCom pletedEventArgs 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.ToSt ring());
    }
    else
    {
    resultsText = String.Format("[{0}] Message sent.",
    token);
    }

    MailResults(res ultsText);
    }
    }
    }
    }

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

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

    Thanks
    Steve


  • Steve Barnett

    #2
    Re: Not getting status event from SmtpClient

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

    Steve


    "Steve Barnett" <noname@nodomai n.comwrote in message
    news:uD0Ig8epIH A.1772@TK2MSFTN GP03.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.addre ss", "My Name");
    private SmtpClient smtp = new SmtpClient("my. isp.smtp.addres s");
    private String token;
    >
    public Emails()
    {
    smtp.UseDefault Credentials = true;
    // Hook in to the send completed event. We want to know when
    this finishes
    smtp.SendComple ted += new
    SendCompletedEv entHandler(smtp _SendCompleted) ;
    }
    >
    >
    public void SendEmail(strin g emailTitle, string emailText, string
    recipient)
    {
    MailMessage msg = new MailMessage(fro mAddress, new
    MailAddress(rec ipient));
    msg.IsBodyHtml = true;
    msg.Body = emailText;
    msg.BodyEncodin g = System.Text.Enc oding.UTF8;
    >
    msg.Subject = emailTitle;
    msg.SubjectEnco ding = System.Text.Enc oding.UTF8;
    >
    msg.From = fromAddress;
    msg.ReplyTo = fromAddress;
    msg.Sender = fromAddress;
    >
    token = recipient;
    smtp.Send(msg);
    }
    >
    void smtp_SendComple ted(object sender,
    System.Componen tModel.AsyncCom pletedEventArgs 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.ToSt ring());
    }
    else
    {
    resultsText = String.Format("[{0}] Message sent.",
    token);
    }
    >
    MailResults(res ultsText);
    }
    }
    }
    }
    >
    I've put a breal point in the smtp_SendComple ted event code but it never
    gets called.
    >
    I also tried using SendAsync() but that didn't fire the SendCompleted
    event either.
    >
    Thanks
    Steve
    >
    >

    Comment

    Working...