Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to send mail email asynchronously from c#, but there is not giving error and also not sending email to the given mail id. Here what I am using code :-

In this code when I send simple mail it works fine.

C#
public void SendMailAccro(int SlNo, string EmailId, string Body)
      {

          MailMessage mail = new MailMessage();
          mail.From = new MailAddress("_____________", "___________", System.Text.Encoding.UTF8);
          mail.To.Add(EmailId);
          mail.Subject = "VOTING DETAILS";
          mail.Body = Body;
          mail.Priority = MailPriority.Normal;
          mail.ReplyTo = new MailAddress("___________", "___________", System.Text.Encoding.UTF8);

          slno = SlNo;
          mail.IsBodyHtml = true;
          SmtpClient smtp = new SmtpClient();

          smtp.Host = "__________________";
          //smtp.EnableSsl = true;
          System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
          NetworkCred.UserName = "_______";
          NetworkCred.Password = "_______";
          //smtp.UseDefaultCredentials = true;
          smtp.Credentials = NetworkCred;
          smtp.Port = "___"
          Object state = mail;

          smtp.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
          try
          {
              smtp.SendAsync(mail, "Name");
          }
          catch (SmtpException ex)
          {
              Console.WriteLine(ex.Message);
          }
          finally
          {
              mail.Dispose();
          }
      }
      void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
      {
          MailMessage mail = e.UserState as MailMessage;
          if (!e.Cancelled && e.Error != null)
          {

              //message.Text = "Mail sent successfully";
          }
      }
Posted
Updated 6-Mar-17 1:13am
v2
Comments
Pascal-78 3-Dec-12 5:27am    
Have you tried to dispose the mail object only in the SendCompleted event ? (or not disposed it)
you may exit the SendAsync function and dispose the mail object before it is used by the smtp object.

I developed a email sender for my company that can send both async and as i say fire and forget method. We send it through the exchange server. checking the lines of your code, the only difference i can find is that i define:

C#
Properties.Settings settings = new DBPROG.Properties.Settings(); 
//to get our server settings


SmtpClient client = new SmtpClient(settings.smtp_server,settings.smtp_port);
client.DeliveryMethod = SmtpDeliveryMethod.Network; 

.....


if (securesent)
{
    client.SendCompleted += new SendCompletedEventHandler(MailDeliveryComplete);
    client.SendAsync(mail, mail.To[0].ToString());
}
else
{
    client.Send(mail);
    CreateLogFiles SendLog = new CreateLogFiles();
    SendLog.ErrorLog("C:\\","\t|Mail sended (Subject): "+mail.Subject.ToString()
                                    + " \t|TO: " + mail.To.ToString()
                                    + " \t|FROM: " + mail.From.ToString()
                                    + " \t|REPLYTO: " + mail.ReplyToList[0].ToString()                                   
                                    , "UNSECURE_SENDLOG", true);
}
 
Share this answer
 
v2
This code is not giving error. But email not received given email id.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900