.net发送包含身体图像的电子邮件而不是附件

时间:2018-06-18 09:25:26

标签: .net image email

我正在尝试使用嵌入式图片创建电子邮件,但不希望这些图像显示为附件。 A已阅读多篇文章并按照说明操作,但我的图像仍然附加。我的代码:

public void SendFromTemplateWithImages(
        string recipients,
        string templateText,
        Dictionary<string, string> templateData = null,
        string replyTo = null,
        string bcc = null,
        bool replaceNewLinesWithHtml = true)
    {
        var messageTemplate = new MessageTemplate(
            templateText,
            templateData,
            replaceNewLinesWithHtml: replaceNewLinesWithHtml);

        ThrowHelper.ThrowIfNullOrEmpty(recipients, "recipients");
        ThrowHelper.ThrowIfNullOrEmpty(messageTemplate.Body, "body");

        recipients = recipients
            .Replace(" ", string.Empty)
            .Replace(";", ",");

        var message = new MailMessage();
        message.To.Add(recipients);
        message.Subject = messageTemplate.Subject;
        message.Body = messageTemplate.Body;
        message.AlternateViews.Add(GetAlternativeView(templateText));

        Send(message, true, replyTo: replyTo);
    }

    private AlternateView GetAlternativeView(string templateText)
    {
        var res = new LinkedResource(@"C:\Users\Borislav.Dimitrov\Desktop\screenShots\NewUser1_files\image001.png");
        res.ContentId = "logoID";
        var res2 = new LinkedResource(@"C:\Users\Borislav.Dimitrov\Desktop\screenShots\NewUser1_files\image002.png");
        res2.ContentId = "agroID";
        var res3 = new LinkedResource(@"C:\Users\Borislav.Dimitrov\Desktop\screenShots\NewUser1_files\image003.jpg");
        res3.ContentId = "linkedinID";
        var res4 = new LinkedResource(@"C:\Users\Borislav.Dimitrov\Desktop\screenShots\NewUser1_files\image004.jpg");
        res4.ContentId = "facebookID";
        var res5 = new LinkedResource(@"C:\Users\Borislav.Dimitrov\Desktop\screenShots\NewUser1_files\image005.jpg");
        res5.ContentId = "youtubeID";
        string htmlBody = templateText;
        AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
        alternateView.LinkedResources.Add(res);
        alternateView.LinkedResources.Add(res2);
        alternateView.LinkedResources.Add(res3);
        alternateView.LinkedResources.Add(res4);
        alternateView.LinkedResources.Add(res5);
        return alternateView;
    }

1 个答案:

答案 0 :(得分:0)

message.IsBodyHtml = true;
message.Body = "<b> Should be HTML Body </b>";

此方法“GetAlternativeView”是否返回HTML字符串?它应该是适当的HTML,可以在浏览器中验证。