I am trying to send am email from a C# SharePoint application that includes custom styles and attachments. It uses a template structure and I have successfully configured the email to use the new styles and attachments for inline images and it displays fine for Outlook clients. However, when I try to view the email on an iOS device, I seeing the images twice; once inline and once again at the end of the email.
The closest I could find to a solution was written for Django and I haven't had much success porting that solution to C#. I found that answer here: Displaying inline images on iPhone, iPad
I configure the attachments in this way:
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);
How can I go about displaying these images only once? I need to be able to display them in such a way that they are only displayed inline. I am not sure if this means I should use alternative views and if so, how to go about using them.
EDIT:
Here is the remainder of the code that I use to generate the emails:
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;
}