繁体   English   中英

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

[英]Sending email with C# to display in 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<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;
}

所以,我想我发现了我的问题所在。 我所做的是许多其他解决方案的组合。 我没有将文件附加为附件,而是创建了备用视图,并将文件附加为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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM