在 C# 中的邮件正文中附加图像 [英] Attaching Image in the body of mail in C#

查看:34
本文介绍了在 C# 中的邮件正文中附加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在正文内容中附加图像.我写了下面的代码

How can I attach an image in the body content . I have written the below code

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] 的形式出现在正文中,而不是图像.如何解决这个问题?路径是否正确?

The code is fine as I am receiving the message also but the image is coming as [X] inside the body and not as the image. How to solve this? The path is correct?

推荐答案

    string attachmentPath = Environment.CurrentDirectory + @"	est.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);

<小时>

参考:用 C# 发送带有内联附件的电子邮件

这篇关于在 C# 中的邮件正文中附加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆