tags:

views:

75

answers:

2

I use SendCompletedEventHandler of SmtpClient when sending a list of emails.

The SendCompletedEventHandler is only called when have already sent all emails in the list.

I expexted, that SendCompletedEventHandler is called when an email is sent.

Is there something wrong in my code?

    public void SendAllNewsletters(List<string> recipients)
    {
        string mailText  = "My Text";
        foreach(string recipient in recipients)
        {
            //if this loop takes 10min then the first call to
            //SendCompletedCallback is after 10min
            SendNewsletter(mailText,recipient);
        }
    }

    public bool SendNewsletter(string mailText , string emailaddress)
    {

            SmtpClient sc = new SmtpClient(_smtpServer, _smtpPort);
            System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(_smtpuser, _smtppassword);
            sc.Credentials = SMTPUserInfo;
            sc.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

            MailMessage mm = null;
            mm = new MailMessage(_senderemail, emailaddress );
            mm.IsBodyHtml = true;
            mm.Priority = MailPriority.Normal;
            mm.Subject = "Something";
            mm.Body = mailText ;
            mm.SubjectEncoding = Encoding.UTF8;
            mm.BodyEncoding = Encoding.UTF8;

            //Mail 
            string userState = emailaddress;
            sc.SendAsync(mm, userState);

            return true;
    }


    public void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
    {
        // Get the unique identifier for this asynchronous operation.
        String token = (string)e.UserState;
        if (e.Error != null)
        {
            _news.SetNewsletterEmailsisSent(e.UserState.ToString(), _newslettername, false, e.Error.Message); 
        }
        else
        {
            _news.SetNewsletterEmailsisSent(e.UserState.ToString(), _newslettername, true, string.Empty);
        }            
    }
+1  A: 

You are creating a new instance of SmtpClient each and every time (and then re-assigning the handler). Use a static variable with a bigger scope.

Josh Stodola
It sounds like you're assuming that the handler can only be associated with one SmtpClient which isn't correct. Besides, he's creating a new handler each time so there is a 1:1 relationship.
Damian Powell
A: 

It works as expected on my machine except that the constructor of MailMessage throw a format exception because "My Tex" is not a valid email address. The first argument is the email address of the sender.

As Josh Stodola points out, you should cache the SmtpClient for the life of this class rather than creating another one for each call. If you don't cache the SmtpClient, then you should add the following line to the end of your SendCompletedCallback (preferably in a finally block):

((SmtpClient)sender).SendCompleted -= SendCompletedCallback;

If this doesn't help you, perhaps you could post more details - such as what is the data in the event args that do get called?

Damian Powell
Thnx, I updated the post to use the right syntax for MailMessage
Malcolm Frexner
Did it fix your problem though?
Damian Powell