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

10 浏览
0 Comments

使用C#发送电子邮件以在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);

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

编辑:

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

public override System.Net.Mail.MailMessage GenerateMessage()
{
var keys = new Dictionary();
var fileBytes = new Dictionary();
var attachments = new List();
var message = new MailMessage();
//获取字节数组和字符串形式的附件图像   
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("{2}", 
                innerMsg, attachment.ContentId, fileName, "test"));
            break;
        case "img-reject":
            keys.Add(fileName, String.Format("{2}", 
                innerMsg1, attachment.ContentId, fileName, "test"));
            break;
        case "img-buyer":
            keys.Add(fileName, String.Format("picture", imageURL, attachment.ContentId));
            break;
        case "img-env":
            keys.Add(fileName, String.Format("{1}", attachment.ContentId, "test"));
            break;
        case "img-computer":
            keys.Add(fileName, String.Format("{1}", attachment.ContentId, "test"));
            break;
        case "logo":
            keys.Add(fileName, String.Format("{1}", attachment.ContentId, "test"));
            break;
        default:
            keys.Add(fileName, String.Format("{1}", attachment.ContentId, fileName));
            break;
    }
}
//获取此电子邮件特定的其他键
GenerateAdditionalKeys(keys, message);
//构建电子邮件
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;
}

0
0 Comments

问题出现的原因是尝试同时使用Attachments和LinkedResources导致内容显示异常。解决方法是创建一个alternate view,并将文件作为LinkedResources附加到邮件中,同时停止使用Attachments。以下是解决问题的代码:

public override System.Net.Mail.MailMessage GenerateMessage()
{
    var keys = new Dictionary();
    var fileBytes = new Dictionary();
    var attachments = new List();
    var message = new MailMessage();
    
    // 获取邮件附件的字节流和名称
    fileBytes = GetEmailAttachments();
    var resourceList = new List();
    
    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;
        
        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)
    {
        string fileName = attachment.ContentId.Split('.')[0];
        
        switch (fileName)
        {
            case "img-approve":
                keys.Add(fileName, String.Format("{2}", 
                    innerMsg, attachment.ContentId, fileName, "test"));
                break;
            case "img-reject":
                keys.Add(fileName, String.Format("{2}", 
                    innerMsg1, attachment.ContentId, fileName, "test"));
                break;
            case "img-buyer":
                keys.Add(fileName, String.Format("picture", imageURL, attachment.ContentId));
                break;
            case "img-env":
                keys.Add(fileName, String.Format("{1}", attachment.ContentId, "test"));
                break;
            case "img-computer":
                keys.Add(fileName, String.Format("{1}", attachment.ContentId, "test"));
                break;
            case "logo":
                keys.Add(fileName, String.Format("{1}", attachment.ContentId, "test"));
                break;
            default:
                keys.Add(fileName, String.Format("{1}", attachment.ContentId, fileName));
                break;
        }
    }
    
    GenerateAdditionalKeys(keys, message);
    
    var body = ReplaceTemplateKeys(keys, _template);
    
    if (!string.IsNullOrEmpty(toEmail))
    {
        message.To.Add(toEmail);
    }
    
    if (!string.IsNullOrEmpty(ccEmail))
    {
        message.CC.Add(ccEmail);
    }
    
    message.IsBodyHtml = true;
    
    return message;
}

0