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

编程入门 行业动态 更新时间:2024-10-20 16:21:26
本文介绍了在 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"; string Password = "my password"; message.To.Add(new System.Net.Mail.MailAddress("toaddress@toadddress")); message.From = new System.Net.Mail.MailAddress("fromaddress@fromaddress"); 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# 的邮件正文中附加图像

本文发布于:2023-10-12 23:57:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1486234.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文中   图像   邮件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!