Train
Train

Reputation: 3506

C# SMTP SendAsync not working

I am having an issue using the SendMailAsync When I change it out for a normal Send it works fine. When I use SendMailAsync I get a connection has timed out and no mail is sent. Am I setting it up the wrong way?

edit: 1. I changed the variable name!!! 2.The exact error happens here await smtpClient.SendMailAsync(mailMessage); It just hangs until the connection times out, and I get an error description of "Connection has timed out" The status code returned is General Error

  1. I'm calling the sendEmail from form the onClick event of a button for now. await unitOfWork.Instance.SendEmail(...) It's a single line of code in the onclick event

    public async Task SendEmail(List<string> notificationList, string emailSubject, string emailMessage)
            {
                var message = new MailMessage()
                {
                    IsBodyHtml = true,
                    BodyEncoding = System.Text.Encoding.UTF8,
                    Subject = emailSubject,
                    From = new MailAddress(FromEmailAddress),
                    Body = emailMessage
                };
    
                 try
                {
                    foreach (var emailAddress in notificationList)
                    {
                        message.To.Add(emailAddress);
                    }
                    var people = string.Join(Environment.NewLine, message.To);
                    MessageBox.Show(@"from: " + unitOfWork.CurrentSystemUser.Person.Email + "To: " + people);
    
                    await SendMessage(message);
    
                }
                catch (Exception E)
                {
                 ...
                }
                finally
                {
                  ...
                }
    
            }
    
        private async Task SendMessage(MailMessage mailMessage)
        {
            if (!CanSendSmtpEmail) return;
            await SendSmtpMail(mailMessage);
        }
    
        public async Task SendSmtpMail(MailMessage mailMessage)
        {
            SmtpClient smtpClient = new SmtpClient(SmtpClientHost, int.Parse(SmtpClientPort));
            smtpClient.SendCompleted += (s, e) => {
                smtpClient.Dispose();
                mailMessage.Dispose();
            };
            await smtpClient.SendMailAsync(mailMessage);
            //smtpClient.Send(mailMessage) works fine
        }
    

Upvotes: 0

Views: 1556

Answers (1)

AlinG
AlinG

Reputation: 491

Increasing SmtpClient.Timeout won't work because it only applies to the synchronous Send method (see here).

Your code is working fine for me when I test it using GMail (like explained here) Can you give it a try with GMail or a different SMTP server? I think maybe your SMTP server is "acting funny". Also maybe you can show us how are you calling SendEmail method ?

Side notes:

  1. Your question is really interesting (at least to me) but because of the way you wrote your code (property has the same name as method) people have down-voted it (you should fix that).
  2. Also, can I see the full exception you get? (btw: catch (Exception E) - not good, use lowercase "e" for the exception argument name).
  3. Make sure you understand exactly how asynchronous it really is.

Upvotes: 3

Related Questions