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 8085191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:02:19+00:00 2026-06-05T18:02:19+00:00

I am using SmtpClient to sent out email. I create some function in Mail

  • 0

I am using SmtpClient to sent out email. I create some function in Mail class:

private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
    // Get the unique identifier for this asynchronous operation.
    String token = (string)e.UserState;

    if (e.Cancelled)
    {
        MailStatus = Status.CANCEL;
        MailStatusMessage = token + " Send canceled.";
    }
    else if (e.Error != null)
    {
        MailStatus = Status.ERROR;
        MailStatusMessage = token + " " + e.Error.ToString();
    }
    else
    {
        MailStatus = Status.SENT;
        MailStatusMessage = "Mail sent.";
    }

    mailSent = true;
}

public void SentEmail()
{
    client = new SmtpClient(Host, Port);
    client.Credentials = new NetworkCredential(UserName, Password);
    MailAddress from = new MailAddress(MerchantEmail, MerchantName);
    MailAddress to = new MailAddress(CustomerEmail);
    MailMessage message = new MailMessage(from, to);
    message.Body = EmailSubjectTemplate();
    message.BodyEncoding = System.Text.Encoding.UTF8;
    message.Subject = EmailSubjectTemplate();
    message.SubjectEncoding = System.Text.Encoding.UTF8;

    client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

    client.SendAsync(message, "Sending message.");

    message.Dispose();
}

In a form I call the function, before closing the form, but when waiting for the response from the SendCompletedCallback, the this.Close() will be executed:

Mail mail = new Mail();
mail.SentEmail();
this.Close();

How can I stop the form from closing before I get a response from SendCompletedCallback?

  • 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-06-05T18:02:21+00:00Added an answer on June 5, 2026 at 6:02 pm

    Option1

    public class Mail 
    {
        public delegate void MailSendComplete();
        public event MailSendComplete OnMailSendComplete;
        private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // your code 
            // finally call the complete event
            OnMailSendComplete();
        }
    
        public void SentEmail()
        {
            // your code 
        }
    }
    

    subscribe to this event from calling form as:

    Mail m = new Mail();
    m.OnMailSendComplete += new Mail.MailSendComplete(m_OnMailSendComplete);
    m.SentEmail();
    

    When you receive the complete event you can close the form
    void m_OnMailSendComplete()
    {
    this.Close();
    }

    Option 2

    when you create Mail object you can pass current form reference to it

    Mail mail = new Mail(this);
    

    then at the end of SendCompletedCallback you can close the form

    public class Mail 
    {
        public Form form { get; set; }
        public Mail(Form  f)
        {
            form = f;
        }
    
        private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            // your code 
            // finally close the form
            form.Close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When sending email using the SMTPClient class in ASP.NET C#, how can I add
I'm using System.Net.Mail to send out a few emails. The emails are being sent
I'm using MailMessage class and then sent mail to many recipients. My code is
I am using a simple mail sender class that uses the System.Net.Mail . I
When I send plain text emails using SmtpClient class, transfer encoding is set to
I am trying to send mail using SmtpClient() within my Winforms VB.Net program Here
I'm sending a simple mail with attachment using SmtpClient but I get this error:
I'm using System.Net.Mail to send email, like so : MailMessage message = new MailMessage();
I'm using an online form at one of my web sites. Every mail sent
I have written some simple code, to send en auto generated email, using the

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.