zoukankan      html  css  js  c++  java
  • postal邮件发送(三):以附件形式显示图片

    前言

    上篇提到如果邮件中有图片的话,可以使用 @Html.EmbedImage("~/Content/postal.png") 这种方式,但是经过测试发现,在outlook中如果有该行代码,则会导致乱码问题,原因未知(有兴趣可研究下)。。。

    后台以附件形式添加图片

    下面提供另一种方式,解决outlook乱码问题,,,

     后台代码,以附件的形式

            public ActionResult SendImageEmail()
            {
    
                var emailService = new EmailService(ViewEngines.Engines, () => CreateMySmtpClient());
    
                dynamic email = new Email("SimpleImage");
    
                var img = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin", "Content", "postal.png");//"bin",
                var imageAttachment = new Attachment(img);
                email.Image = imageAttachment.ContentId;
                email.Attach(imageAttachment);
    
                emailService.Send(email);
    
                return RedirectToAction("Sent", "Home");
    
            }
    
            private SmtpClient CreateMySmtpClient()
            {
                SmtpClient mailClient = new SmtpClient("localhost");
                return mailClient;
    
            }
    

      

      

     邮件模板代码

    To: test@example.org
    From: test@example.org
    CC:cc@example.org
    BCC:BCC@example.org
    Subject: Simple email example
    
    <html>
    <body>
        <p>你好,Postal</p>
        <p>This is an <code>HTML</code> message</p>
        <p>Generated by <a href="http://aboutcode.net/postal">Postal</a> on @ViewBag.Date</p>
        <p>
            <img src="cid:@Model.Image" />
        </p>
        <p>
            The date is: @ViewBag.Date
        </p>
    </body>
    </html>
    

     记着图片设置为 如果较新则复制

    效果

  • 相关阅读:
    sql总结
    2018年6月10日笔记
    Docker入门之zabbix-agent篇
    2018年6月7日笔记
    2018年6月5日笔记
    Docker入门之container篇
    Docker入门之image篇
    Docker 入门
    2018年5月31日笔记
    2018年5月29日笔记
  • 原文地址:https://www.cnblogs.com/mybky/p/5978124.html
Copyright © 2011-2022 走看看