使用LinkedResource属性通过http发送电子邮件

时间:2017-08-11 16:33:09

标签: c#

我正在尝试使用LinkedResource撰写一个包含嵌入图像的正文的电子邮件。当我尝试将它发布到连接到smtp的api时,我得到了这个例外:

System.AggregateException:InvalidOperationException:此流不支持超时。 JsonSerializationException:获取值时出错' ReadTimeout' on' System.IO.FileStream'。

这是我的代码,我需要更改以使其工作。

class Program
{

    static void Main(string[] args)
    {
        Mailer();            
    }

    public static void Mailer()
    {
        var imageOne = new LinkedResource("PumpkinSpice.jpg", "image/jpeg");
        imageOne.ContentId = "psl";
        var imageLink = $"<img src=\"cid:{imageOne.ContentId}\"/>"; 
        var listOfResources = new List<LinkedResource> {imageOne};

        var email = new MailInfo()
        {
            ToAddresses = "mcjibbles@foo.com",
            CcAddresses = "jibbles@bar.com",
            BccAddresses = null,
            Subject = "test email",
            Body = "Email body" + imageLink,
            Resources = listOfResources,
            IsHtml = true

        };

        using (var client = CreateClient())
        {
            client.PostAsJsonAsync($"api/email", email).Wait();
        }       
    }

    private static HttpClient CreateClient()
    {
        return new HttpClient(new HttpClientHandler { UseDefaultCredentials = true })
        {
            BaseAddress = new Uri("http://fakesite.com/")
        };
    }
}
class MailInfo
{

    public string ToAddresses { get; set; }
    public string CcAddresses { get; set; }
    public string BccAddresses { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
    public bool IsHtml { get; set; }
    public List<LinkedResource> Resources { get; set; }

}

Api代码

public class EmailController : ApiController
{

 [HttpPost]
 public void SendEmail(EmailInformation information)
    {

        using (var message = new MailMessage())
        {
            message.Subject = information.Subject;

            var toAddresses = SplitAddresses(information.ToAddresses);
            foreach (var address in toAddresses)
            {
                message.To.Add(new MailAddress(address));
            }

            message.IsBodyHtml = information.IsHtml;
            message.Body = information.Body;    


            using (var htmlView = AlternateView.CreateAlternateViewFromString(message.Body, null, "text/html"))
            {
                foreach (var resource in information.Resources)
                {
                    htmlView.LinkedResources.Add(resource);
                }
                using (var client = new SmtpClient(WebConfigurationManager.AppSettings["Smtp"]))
                {
                    message.From = information.FromAddress;

                    client.Send(message);
                }
            }              
        }
    }

    public class EmailInformation
    {       
        public string ToAddresses { get; set; }
        public string FromAddress { get; set; }
        public string CcAddresses { get; set; }
        public string BccAddresses { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public bool IsHtml { get; set; }
        public List<LinkedResource> Resources { get; set; }
    }

}

0 个答案:

没有答案