【问题标题】:C# - Send email with inline attachment WITHOUT Outlook's paperclip icon?C# - 在没有 Outlook 的回形针图标的情况下发送带有内联附件的电子邮件?
【发布时间】:2015-12-15 18:56:18
【问题描述】:

我有一个发送带有内嵌图片的电子邮件的系统。问题是 Outlook 2013 如何显示附件。我能否以一种告诉 Outlook 显示此处看到的回形针图标的方式更新我的代码?

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

这是生成电子邮件的代码。创建一个基本的控制台应用,指定你的 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;
}

而且我还更新了 main 方法:

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

【问题讨论】:

  • 这对你有用吗?我面临同样的问题。它在 Outlook 中显示图钉图标。

标签: c# email outlook inline


【解决方案1】:

确保您的附件具有 Content-ID MIME 标头,并且邮件的 HTML 正文使用 cid 属性引用它们:&lt;img src="cid:xyz"&gt;(其中 xyz 是 Content-ID MIME 标头的值)。

【讨论】:

猜你喜欢
  • 2011-11-22
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-07
  • 2011-11-10
  • 2023-03-10
相关资源
最近更新 更多