使用C#发送电子邮件以在iOS中显示

awh*_*112 5 c# email html-email email-attachments ios

我正在尝试从包含自定义样式和附件的C#SharePoint应用程序发送电子邮件.它使用模板结构,我已成功配置电子邮件以使用内联图像的新样式和附件,它可以很好地显示Outlook客户端.但是,当我尝试在iOS设备上查看电子邮件时,我会看到两次图像; 一旦内联,再次在电子邮件的末尾.

我能找到最接近解决方案的是为Django编写的,我没有太多成功将该解决方案移植到C#.我在这里找到答案:在iPhone,iPad上显示内嵌图像

我以这种方式配置附件:

System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(new MemoryStream(imageBytes.Key), MediaTypeNames.Image.Jpeg);
attachment.Name = imageBytes.Value;
attachment.ContentDisposition.Inline = true;
attachment.ContentId = imageBytes.Value;

attachments.Add(attachment);
Run Code Online (Sandbox Code Playgroud)

如何才能仅显示一次这些图像?我需要能够以只显示内联的方式显示它们.我不确定这是否意味着我应该使用替代视图,如果是,那么如何使用它们.

编辑:

以下是我用于生成电子邮件的其余代码:

public override System.Net.Mail.MailMessage GenerateMessage()
{
var keys = new Dictionary<string, string>();
var fileBytes = new Dictionary<byte[], string>();
var attachments = new List<System.Net.Mail.Attachment>();

var message = new MailMessage();

//get the attachment images as html in a dictionary object, byte[] and string   
fileBytes = GetEmailAttachments();

foreach (var imageBytes in fileBytes)
{
    System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(new MemoryStream(imageBytes.Key), MediaTypeNames.Image.Jpeg);
    attachment.Name = imageBytes.Value;
    attachment.ContentDisposition.Inline = true;
    attachment.ContentId = imageBytes.Value;

    attachments.Add(attachment);
}

foreach (var attachment in attachments)
{
    message.Attachments.Add(attachment);
    string fileName = attachment.Name.Split('.')[0];

    switch (fileName)
    {
        case "img-approve":
            keys.Add(fileName, String.Format("<a href={0} target=_top><img src='cid:{1}' alt={2} title={3}></a>", 
                innerMsg, attachment.ContentId, fileName, "test"));
            break;
        case "img-reject":
            keys.Add(fileName, String.Format("<a href={0} target=_top><img src='cid:{1}' alt={2} title={3}></a>", 
                innerMsg1, attachment.ContentId, fileName, "test"));
            break;
        case "img-buyer":
            keys.Add(fileName, String.Format("<a href={0} target=_top><img src='cid:{1}' height=100 alt=picture></a>", imageURL, attachment.ContentId));
            break;
        case "img-env":
            keys.Add(fileName, String.Format("<img src='cid:{0}' alt={1} style=vertical-aign: middle>", attachment.ContentId, "test"));
            break;
        case "img-computer":
            keys.Add(fileName, String.Format("<img src='cid:{0}' alt={1} style=vertical-aign: middle>", attachment.ContentId, "test"));
            break;
        case "logo":
            keys.Add(fileName, String.Format("<img src='cid:{0}' alt={1} style=vertical-aign: middle>", attachment.ContentId, "test"));
            break;
        default:
            keys.Add(fileName, String.Format("<img src='cid:{0}' alt={1}>", attachment.ContentId, fileName));
            break;
    }
}

//get the additional keys specific to this email
GenerateAdditionalKeys(keys, message);

//build the email
var body = ReplaceTemplateKeys(keys, _template);

if(!string.IsNullOrEmpty(toEmail))
{
    message.To.Add(toEmail);
}
if (!string.IsNullOrEmpty(ccEmail))
{
    message.CC.Add(ccEmail);
}

message.IsBodyHtml = true;
message.Body = body;

return message;
}
Run Code Online (Sandbox Code Playgroud)

awh*_*112 4

所以,我想我发现了我的问题所在。我所做的是许多其他解决方案的组合。我没有将文件作为附件附加,而是创建了一个备用视图并将文件作为 LinkedResources 附加。它看起来很像@Adriano 在上面的评论中提供的链接,但我必须确保并做的是停止尝试使用附件和 LinkedResources。一旦我停止依赖附件来提供标记中使用的 ContentID,一切都会按预期进行。我要工作的代码如下(为了清楚地了解更改的内容,我留下了注释掉的部分):

public override System.Net.Mail.MailMessage GenerateMessage()
{
var keys = new Dictionary<string, string>();
var fileBytes = new Dictionary<byte[], string>();
var attachments = new List<System.Net.Mail.Attachment>();

var message = new MailMessage();

//get the attachment images as html in a dictionary object, byte[] and string   
fileBytes = GetEmailAttachments();

var resourceList = new List<LinkedResource>();

foreach (var imageBytes in fileBytes)
{
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(new MemoryStream(imageBytes.Key), MediaTypeNames.Image.Jpeg);
attachment.Name = imageBytes.Value;
attachment.ContentDisposition.Inline = true;
attachment.ContentId = imageBytes.Value;

//attachments.Add(attachment);

var stream = new MemoryStream(imageBytes.Key);
var newResource = new LinkedResource(stream, "image/jpeg");
newResource.TransferEncoding = TransferEncoding.Base64;
newResource.ContentId = imageBytes.Value;
resourceList.Add(newResource);
}

foreach (var attachment in resourceList)
{
//message.Attachments.Add(attachment);
//string fileName = attachment.Name.Split('.')[0];

string fileName = attachment.ContentId.Split('.')[0];

switch (fileName)
{
    case "img-approve":
        keys.Add(fileName, String.Format("<a href={0} target=_top><img src='cid:{1}' alt={2} title={3}></a>", 
            innerMsg, attachment.ContentId, fileName, "test"));
        break;
    case "img-reject":
        keys.Add(fileName, String.Format("<a href={0} target=_top><img src='cid:{1}' alt={2} title={3}></a>", 
            innerMsg1, attachment.ContentId, fileName, "test"));
        break;
    case "img-buyer":
        keys.Add(fileName, String.Format("<a href={0} target=_top><img src='cid:{1}' height=100 alt=picture></a>", imageURL, attachment.ContentId));
        break;
    case "img-env":
        keys.Add(fileName, String.Format("<img src='cid:{0}' alt={1} style=vertical-aign: middle>", attachment.ContentId, "test"));
        break;
    case "img-computer":
        keys.Add(fileName, String.Format("<img src='cid:{0}' alt={1} style=vertical-aign: middle>", attachment.ContentId, "test"));
        break;
    case "logo":
        keys.Add(fileName, String.Format("<img src='cid:{0}' alt={1} style=vertical-aign: middle>", attachment.ContentId, "test"));
        break;
    default:
        keys.Add(fileName, String.Format("<img src='cid:{0}' alt={1}>", attachment.ContentId, fileName));
        break;
}
}

//get the additional keys specific to this email
GenerateAdditionalKeys(keys, message);

//build the email
var body = ReplaceTemplateKeys(keys, _template);

if(!string.IsNullOrEmpty(toEmail))
{
    message.To.Add(toEmail);
}
if (!string.IsNullOrEmpty(ccEmail))
{
    message.CC.Add(ccEmail);
}

message.IsBodyHtml = true;
//message.Body = body;

return message;
}
Run Code Online (Sandbox Code Playgroud)