0

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
        }
    
Train
  • 3,420
  • 2
  • 29
  • 59
  • 1
    That code compiles?? `if (!SendSmtpEmail) return;` that's not valid C#, you can't compare a method group with a boolean. – Gusman Oct 21 '16 at 17:22
  • 1
    Not working, favorite description of a problem ! – mybirthname Oct 21 '16 at 17:23
  • 1
    Dude, didn't you got your cristal ball from SO? that's the basic tool to answer questions here. ;) – Gusman Oct 21 '16 at 17:24
  • @Gusman It is valid c# and it compiles just fine. Your comments could be a little more productive.,, – Train Oct 21 '16 at 17:26
  • @Gusman You are confusing `SendSmtpEmail` with `SendSmtpMail` because his naming is very similar. The initialization or assignment of `SendSmtpEmail` is not shown in the snippet. – Hank Oct 21 '16 at 17:27
  • 2
    it was a question to clarify it, not an answer. @Hank yes, you're right, holly molly, that's the best naming ever... ¬¬ – Gusman Oct 21 '16 at 17:29
  • @AKADER You need to debug and narrow down the code that is actually messing up and tell us where it's tripping up. We can't be expected to just eyeball the code and know the exact error. – Hank Oct 21 '16 at 17:29
  • @Hank Is the update sufficient? It's happening at `await smtpClient.SendMailAsync(mailMessage);` Why won't that work but a simple `smtpClient.Send(mailMessage);` works fine? – Train Oct 21 '16 at 17:33
  • @AKADER Can you post the `SendMailAsync` method? If it's waiting on that method, you need to dive into that one and see what it's waiting on. And because you're awaiting two different methods. The `Send` method is working fine apparently. You just need to follow the bread crumbs. – Hank Oct 21 '16 at 17:36
  • @Hank It's all Microsoft :) https://msdn.microsoft.com/en-us/library/hh193922(v=vs.110).aspx – Train Oct 21 '16 at 17:38
  • @AKADER The only thing I can suggest is to increase the `SmtpClient.Timeout` property but it's not necessarily the best fix for it. Just keep in mind it is in milliseconds – Hank Oct 21 '16 at 19:42
  • @Hank I've already tried it, it won't even send with a single recipient. Thanks for all of your input. – Train Oct 21 '16 at 21:50

1 Answers1

3

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.
Community
  • 1
  • 1
AlinG
  • 451
  • 3
  • 12
  • That's the thing, It's working fine With GMAIL, but not on the client's network. We looked at some real time logs and it's not even hitting the server when I call sendEmailAsync. It won't work no matter how simple I make the code. But I just wanted it to work before I make it do it's thing in the background. I think I will just have to go with .send for now. Thanks a lot! – Train Oct 21 '16 at 22:13