gpt4 book ai didi

C# - 在没有 Outlook 回形针图标的情况下发送带有内联附件的电子邮件?

转载 作者:太空狗 更新时间:2023-10-30 01:33:18 25 4
gpt4 key购买 nike

我有一个发送带有内联图片的电子邮件的系统。问题在于 Outlook 2013 如何显示附件。我可以更新我的代码,让 outlook 显示这里看到的回形针图标吗?

Outlook 2013 preview pane

我的想法是,我只想在附加全尺寸图片时显示此图标。不是内联附件。

这是生成电子邮件的代码。创建一个基本的控制台应用程序,指定您的 To/mailserver/picture 路径,然后运行。

static void Main(string[] args)
{
Console.WriteLine("Prepping email message....");

var subject = "Test Subject With Inline";
var message = "<p>This is a test message.</p><br/><br/><p>[CompanyLogo]</p>";
var to = new List<string>();

to.Add("My.Name@company.com");

Console.WriteLine("Sending email message....");

if (SendMessageToFrom(subject, message, to, new List<string>()))
{
Console.WriteLine("Email sent! Check your inbox.");
}
else
{
Console.WriteLine("Error sending email!");
}
}

public static bool SendMessageToFrom(String subject, String message, List<String> to, List<String> cc)
{
try
{
// Construct the email
var sendMessage = new MailMessage()
{
IsBodyHtml = true,
From = new MailAddress("noreply@company.com"),
Subject = subject,
Body = message
};

if (sendMessage.Body.Contains("[CompanyLogo]"))
{
sendMessage.AlternateViews.Add(EmbedLogo(sendMessage.Body));
}

// Add the list of recipients
foreach (var recipient in to)
{
sendMessage.To.Add(recipient);
}
foreach (var recipient in cc)
{
sendMessage.CC.Add(recipient);
}

//Specify the SMTP server
var smtpServerName = "mailserver.company.com";

var mailClient = new SmtpClient(smtpServerName);

mailClient.Send(sendMessage);

return true;
}
catch
{
throw;
}
}

private static AlternateView EmbedLogo(string html)
{
var inline = new LinkedResource("img\\company-logo.jpg");
inline.ContentId = Guid.NewGuid().ToString();
html = html.Replace("[CompanyLogo]", string.Format(@"<img src='cid:{0}'/>", inline.ContentId));
var result = AlternateView.CreateAlternateViewFromString(html, null, System.Net.Mime.MediaTypeNames.Text.Html);
result.LinkedResources.Add(inline);
return result;
}

更新:这是实现这一技巧的代码:

private static MailMessage EmbedLogo(MailMessage mail)
{
var inline = new Attachment("img\\company-logo.jpg");
inline.ContentId = Guid.NewGuid().ToString();
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
mail.Body = mail.Body.Replace("[CompanyLogo]", string.Format(@"<img src='cid:{0}'/>", inline.ContentId));
mail.Attachments.Add(inline);
return mail;
}

而且我还将主要方法更新为:

if (sendMessage.Body.Contains("[CompanyLogo]"))
{
sendMessage = EmbedLogo(sendMessage);
}

最佳答案

确保您的附件具有 Content-ID MIME header 并且邮件的 HTML 正文使用 cid 属性引用它们:<img src="cid:xyz"> (其中 xyz 是 Content-ID MIME header 的值)。

关于C# - 在没有 Outlook 回形针图标的情况下发送带有内联附件的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34297230/

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