Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7445809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:07:42+00:00 2026-05-29T12:07:42+00:00

I was using SMTPClient.Send(mail) method to send emails, but then I saw, if the

  • 0

I was using SMTPClient.Send(mail) method to send emails, but then I saw, if the email id is not present (does not exists), my application waits till it receives the exception and then allows user to perform further tasks.

So I thought of using SMTPClient.SendAsync Method.

My doubt!! Where can this userToken Object be used which is passed as the parameter to the method? I searched many things online but din’t find a good example. Even in MSDN they use it like this

string userState = "test message1";
client.SendAsync(message, userState);

But then what can it be really used for?

Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:07:46+00:00Added an answer on May 29, 2026 at 12:07 pm

    You may use it in the following case: imagine that you have application for batch email sending. You compose message(different messages\attachments for every recipient, so you can’t combine it into a single message), select for example 20 recipients and press button “Send All”. For sending you use SendAsync and several SmtpClient instances from the “pool”(as SmtpClient doesn’t allow to call SendAsync twice on one instance before previous call isn’t completed).

    You have a single SmtpClientSendCompleted handler for all SendAsync calls in which you should perform advanced logging: log result of the sending, names (addresses or even attachments) of the recipients of failed messages, but AsyncCompletedEventArgs can provide this information only with help of UserState. So the basic pattern for this purpose is to use custom user state object. So see the simplified example:

    Interface which contains fields you will need in the handler:

    public interface IEmailMessageInfo{
       string RecipientName {get;set;}
    }
    

    Async state class:

    /// <summary>
    /// User defined async state for SendEmailAsync method
    /// </summary>
    public class SendAsyncState {
    
        /// <summary>
        /// Contains all info that you need while handling message result
        /// </summary>
        public IEmailMessageInfo EmailMessageInfo { get; private set; }
    
    
        public SendAsyncState(IEmailMessageInfo emailMessageInfo) {
            EmailMessageInfo = emailMessageInfo;
        }
    }
    

    Here the code for sending email:

    SmtpClient smtpClient = GetSmtpClient(smtpServerAddress);
    smtpClient.SendCompleted += SmtpClientSendCompleted;
    smtpClient.SendAsync(
       GetMailMessage()
       new SendAsyncState(new EmailMessageInfo{RecipientName = "Blah-blah"})
    );
    

    And the handler code example:

    private void SmtpClientSendCompleted(object sender, AsyncCompletedEventArgs e){
        var smtpClient = (SmtpClient) sender;
        var userAsyncState = (SendAsyncState) e.UserState;
        smtpClient.SendCompleted -= SmtpClientSendCompleted;
    
        if(e.Error != null) {
           tracer.ErrorEx(
              e.Error, 
              string.Format("Message sending for \"{0}\" failed.",userAsyncState.EmailMessageInfo.RecipientName)
           );
        }
    
        // Cleaning up resources
        .....
    }
    

    Please, let me know if you need more details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sending mail from my C# Application, using the SmtpClient. Works great, but I
I have an application that uses SmtpClient to send E-Mail, but the E-Mails are
am using system.net.mail to send email as shown below, but its too slow. it
We are using SmtpClient.Send to send mail. But unfortunately the mail body which is
I'm sending a simple mail with attachment using SmtpClient but I get this error:
I'm using System.Net.Mail.MailMessage to send emails from my C# Windows app. I originally had
I am sending emails using c# using SmtpClient. I have to send aproximately one
I'm currently looping through a datareader and calling the System.Net.Mail.SmtpClient's Send() method. The problem
I need to send mail using Gmail SMTP, but it's showing Failure sending mail.
I'm using the following code to send an email from my vb.net web application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.