【问题标题】:Sending email with C# to display in iOS使用 C# 发送电子邮件以在 iOS 中显示
【发布时间】:2014-06-26 02:28:42
【问题描述】:

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

我能找到的最接近解决方案的解决方案是为 Django 编写的,但我将该解决方案移植到 C# 并没有取得多大成功。我在这里找到了答案:Displaying inline images on 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<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;
}

【问题讨论】:

  • 提供用于构建 MailMessage 的其余代码可能会有所帮助。
  • 我查看了您链接到 @Adriano 的解决方案,但使用该方法似乎将 .bin 文件添加到 Outlook 中的电子邮件中,并且无法解决 iOS 中的格式问题。
  • 谢谢@mason,我上传了其余的代码。

标签: c# ios email html-email email-attachments


【解决方案1】:

所以,我想我发现了我的问题所在。我所做的是结合了许多其他解决方案。我没有将文件附加为附件,而是创建了一个备用视图并将文件附加为 LinkedResources。它看起来很像@Adriano 在上面的评论中提供的链接,但我必须确保并做的是停止尝试同时使用 Attachments 和 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-08
    • 2016-04-17
    • 2012-01-06
    • 1970-01-01
    • 2012-02-25
    • 2010-10-01
    相关资源
    最近更新 更多