在MVC控制器中发送大型电子邮件无声地失败

时间:2016-06-21 17:00:36

标签: c# email asp.net-mvc-5 smtp async-await

我的MVC Controller中有一个方法可以通过SMTP发送html格式的电子邮件。除非我尝试发送长电子邮件,否则Haven没有任何问题:

    public async Task<string> TestLargeEmail()
    {
        var recipes = await _context.RecipesNoIngredients.ToListAsync();
        string longBody = this.RenderRazorViewToString("RecipesNoIngredients", recipes.Take(500).ToList()); //~300k characters
        string shortBody = this.RenderRazorViewToString("RecipesNoIngredients", recipes.Take(300).ToList()); //~150k characters

        var longMsg = new MailMessage("TestFrom@email.com", "TestTo@email.com", "Long Message", longBody) { IsBodyHtml = true };
        var shortMsg = new MailMessage("TestFrom@email.com", "TestTo@email.com", "Short Message", shortBody) { IsBodyHtml = true };

        using (var client = new SmtpClient())
        {
            await client.SendMailAsync(longMsg); //fails silently over the network, but works when delivering to local file system
            await client.SendMailAsync(shortMsg); //works perfectly
        }

        return "done";
    }

Web.Config文件:

<smtp deliveryMethod="Network" from="test@test.com">
    <network host="smtp.some.address.com" />
</smtp> // Settings when delivering over network
<smtp deliveryMethod="SpecifiedPickupDirectory" from="test@test.com">    
    <network host="smtp.some.address.com" />
    <specifiedPickupDirectory pickupDirectoryLocation="C:\\TestEmails" />
</smtp> // Settings when delivering to local file system

也许值得注意的是,同步发送也没有任何区别...

0 个答案:

没有答案