• C#定制并发送HTML邮件


    HTML格式的邮件能够使用所有html/css使得邮件更丰富,比如现在很多newsletter 都是使用的html邮件. 今天试了一下,如何把图片嵌入到html中呢?

    方法一,你的图片host到了internet上

         SmtpClient smtpClient = new SmtpClient();
                MailMessage message = new MailMessage();
                message.IsBodyHtml = true;
                message.Body = "<div style='border:thin solid #00FFFF'> <p>Dear xixifusi,</p> <p>We wanted to take this opportunity to say thank you for your business and to wish you a wonderful holiday season and a very happy New Year.</p> <img alt='' src='http://i.microsoft.com/global/en-us/homepage/PublishingImages/Header/IELogo.png' /></div>";
                MailAddressCollection tos = new MailAddressCollection();
                message.To.Add(new MailAddress("xixifusi@gmail.com"));

        message.From = new MailAddress("xixifusi@gmail.com", "Ultraman");
                message.Subject = "Hello xixifusi";
                smtpClient.Credentials = new NetworkCredential("xixifusi@gmail.com", "123456", "");
                smtpClient.Host = "smtp.gmail.com";
                smtpClient.Port = 587;
                smtpClient.EnableSsl = true;
                smtpClient.Send(message);

    方法二, 把本地图片嵌入到html邮件中

        SmtpClient smtpClient = new SmtpClient();
                MailMessage message = new MailMessage();
                message.IsBodyHtml = true;
                MailAddressCollection tos = new MailAddressCollection();
                message.To.Add(new MailAddress("xixifusi@gmail.com"));

        message.From = new MailAddress("xixifusi@gmail.com", "Ultraman");
                message.Subject = "Hello xixifusi";
                smtpClient.Credentials = new NetworkCredential("xixifusi@gmail.com", "123456", "");
                smtpClient.Host = "smtp.gmail.com";
                smtpClient.Port = 587;
                smtpClient.EnableSsl = true;
                AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<div style='border:thin solid #00FFFF'> <p>Dear xixifusi,</p> <p>We wanted to take this opportunity to say thank you for your business and to wish you a wonderful holiday season and a very happy New Year.</p> <img alt='' src='cid:logo' /></div>", null, "text/html");
                LinkedResource logo = new LinkedResource(@"D:IElogo.png");
                logo.ContentId = "logo";
                htmlView.LinkedResources.Add(logo);
                message.AlternateViews.Add(htmlView);
                smtpClient.Send(message);

  • 相关阅读:
    ListCtrl中垂直滚动条自动滚动
    auth组件
    母版的继承
    mysql数据备份和恢复
    部署MHA
    安装mysql
    yum源优化
    存储过程
    redis哨兵、集群
    redis主从同步
  • 原文地址:https://www.cnblogs.com/Kingly/p/3555925.html
Copyright © 2020-2023  润新知