2014-05-08 16 views
5

Tôi đang cố gắng gửi email từ một ứng dụng C# SharePoint bao gồm các kiểu và tệp đính kèm tùy chỉnh. Nó sử dụng một cấu trúc mẫu và tôi đã định cấu hình thành công email để sử dụng các kiểu và tệp đính kèm mới cho hình ảnh nội dòng và nó hiển thị tốt cho các máy khách Outlook. Tuy nhiên, khi tôi cố gắng xem email trên thiết bị iOS, tôi thấy hình ảnh hai lần; một lần nội tuyến và một lần nữa ở cuối email.Gửi email bằng C# để hiển thị trong iOS

Gần nhất tôi có thể tìm thấy giải pháp đã được viết cho Django và tôi đã không có nhiều thành công khi chuyển giải pháp đó cho C#. Tôi tìm thấy câu trả lời ở đây: Displaying inline images on iPhone, iPad

tôi cấu hình các file đính kèm theo cách này:

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); 

Làm thế nào tôi có thể đi về hiển thị những hình ảnh này chỉ một lần? Tôi cần để có thể hiển thị chúng theo cách mà chúng chỉ được hiển thị nội tuyến. Tôi không chắc chắn nếu điều này có nghĩa là tôi nên sử dụng quan điểm thay thế và nếu như vậy, làm thế nào để đi về việc sử dụng chúng.

EDIT:

Dưới đây là phần còn lại của mã mà tôi sử dụng để tạo ra các email:

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; 
} 
+0

Có thể hữu ích khi cung cấp phần còn lại của mã được sử dụng để xây dựng MailMessage. – mason

+0

Bạn đã kiểm tra giải pháp này chưa: http://stackoverflow.com/questions/18358534/send-inline-image-in-email? –

+0

Tôi đã xem giải pháp bạn đã liên kết với @Adriano nhưng sử dụng phương pháp đó có vẻ như thêm tệp .bin vào email trong Outlook và không giải quyết được sự cố định dạng trong iOS. – awh112

Trả lời

4

Vì vậy, tôi nghĩ rằng tôi phát hiện ra những gì vấn đề của tôi là gì. Những gì tôi đã làm là một sự kết hợp của nhiều giải pháp khác. Thay vì đính kèm các tệp dưới dạng tệp đính kèm, tôi đã tạo chế độ xem thay thế và đính kèm tệp dưới dạng LinkedResources. Nó trông giống như liên kết mà @Adriano cung cấp trong phần bình luận ở trên, nhưng những gì tôi phải đảm bảo và làm là ngừng sử dụng cả phần đính kèm và LinkedResources. Sau khi tôi ngừng dựa vào phần đính kèm để cung cấp ContentID được sử dụng trong đánh dấu, mọi thứ hoạt động như mong đợi. Mã tôi phải làm việc bên dưới (Tôi đã để lại nhận xét cho các phần rõ ràng về những gì đã được thay đổi):

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; 
} 
Các vấn đề liên quan