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("[email protected]"));

    message.From = new MailAddress("[email protected]", "Ultraman");
            message.Subject = "Hello xixifusi";
            smtpClient.Credentials = new NetworkCredential("[email protected]", "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("[email protected]"));

    message.From = new MailAddress("[email protected]", "Ultraman");
            message.Subject = "Hello xixifusi";
            smtpClient.Credentials = new NetworkCredential("[email protected]", "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);

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。