gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:14:27 27 4
gpt4 key购买 nike

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

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

关于c# - 在 C# 中的邮件正文中附加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2317012/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com