0

I'm trying to understand why I am not being able to send async mails (maybe it is related that I'm running it from NUnit?

When I do simple smtp.Send(mail);is it working well

// Nunit test
{
    [TestFixture]
    public class Tester
    {

        [Test]
        public void TestSendTestMail()
        {
            // Arrange

            // Act
            await EbayProxy.Instance.SendTestMail();

            // Assert
        }
    }

    public async Task SendTestMail()
    {
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress(_mailFrom);
        mail.To.Add(_mailTo);

        mail.Subject = "Test Mail Subject Async";

        mail.Body = "Test Mail Body";

        mail.IsBodyHtml = true;

        SmtpClient smtp = new SmtpClient(_smtpClient, Convert.ToInt32(_smtpPort));
        smtp.EnableSsl = true;
        smtp.Credentials = new NetworkCredential(_mailFrom, _mailFromPassword);

        await smtp.SendMailAsync(mail); // Not sending mail :(
    }
user829174
  • 6,132
  • 24
  • 75
  • 125

2 Answers2

1

Actually before you learn about Async/Await (which is a great topic!), I would question what it is that you are trying to achieve, full stop.

Why would you want to test the email facility itself, you should give the email client some element of trust but furthermore as mail isn't guaranteed you can only hope that the mail arrives at is destination. Actual acknowledgement would involve the recipient.

The email client seems to be a singleton but could potentially be passed in as a collaborator to another orchestrating class and you could use a mock and verify that the send mail method is called, trusting that the mail client does what it supposed to. I'm pretty certain that the mailing facility is part of some other process....

Furthermore, I'd use an integration type test for this external type command, to verify that when everything is plumbed in properly an actual email gets sent. But I suppose It could be argued what you have already is an integration test, and maybe that's your intention.

brumScouse
  • 3,166
  • 1
  • 24
  • 38
  • So a year later looking at the question, you are just knocking up a test just to see that it works, this wouldnt actually be part of your test suite. Thats got to be it. – brumScouse Oct 25 '17 at 21:22
0

As explained many times before...

How to call asynchronous method from synchronous method in C#?

Calling async method synchronously

http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

I think you'll find this code is causing a dead lock. Try...

[TestFixture]
public class Tester
{

    [Test]
    public async Task TestSendTestMail()
    {
        // Arrange

        // Act
        await EbayProxy.Instance.SendTestMail();

        // Assert
    }
}

public async Task SendTestMail()
{
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(_mailFrom);
    mail.To.Add(_mailTo);

    mail.Subject = "Test Mail Subject Async";

    mail.Body = "Test Mail Body";

    mail.IsBodyHtml = true;

    SmtpClient smtp = new SmtpClient(_smtpClient, Convert.ToInt32(_smtpPort));
    smtp.EnableSsl = true;
    smtp.Credentials = new NetworkCredential(_mailFrom, _mailFromPassword);

    await smtp.SendMailAsync(mail); // Not sending mail :(
}

As discussed here...

How do I test an async method with NUnit, eventually with another framework?

Community
  • 1
  • 1
Mick
  • 6,527
  • 4
  • 52
  • 67