微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在C#中附加邮件正文中的图像

如何在身体内容中附加图像.我写了下面的代码
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
string UserName = "xyz@someorg.com";
string Password = "my password";
message.To.Add(new System.Net.Mail.MailAddress("toaddress@toadddress.com"));
message.From = new  System.Net.Mail.MailAddress("fromaddress@fromaddress.com");              
message.Subject = "test subject";
message.Body = "<img src=@'C:\\Sunset.jpg'/>";                
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
 smtpClient.Host = "hostname";
 smtpClient.Port = 25;
 smtpClient.Credentials = new System.Net.NetworkCredential(UserName,Password);
 smtpClient.Send(message);

代码很好,因为我也收到了消息,但图像在身体内部[X]而不是图像.
怎么解决这个?路径是正确的?

解决方法

string attachmentPath = Environment.CurrentDirectory + @"\test.png";
    Attachment inline = new Attachment(attachmentPath);
    inline.Contentdisposition.Inline = true;
    inline.Contentdisposition.dispositionType = dispositionTypeNames.Inline;
    inline.ContentId = contentID;
    inline.ContentType.MediaType = "image/png";
    inline.ContentType.Name = Path.GetFileName(attachmentPath);

    message.Attachments.Add(inline);

参考:Send an Email in C# with Inline attachments

原文地址:https://www.jb51.cc/csharp/98580.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐