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

前端之家收集整理的这篇文章主要介绍了在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.f2er.com/csharp/98580.html

猜你在找的C#相关文章