2014-05-08 16 views
5

Özel stiller ve ekler içeren bir C# SharePoint uygulamasından am e-posta göndermeye çalışıyorum. Bir şablon yapısı kullanır ve inline görüntüler için yeni stilleri ve ekleri kullanacak şekilde e-postayı başarıyla yapılandırdım ve Outlook istemcileri için iyi görünüyor. Ancak, bir iOS cihazında e-postayı görüntülemeye çalıştığımda, görüntüleri iki kez görüyorum; e-postanın sonunda bir kez ve bir kez daha.iOS'ta görüntülemek için C# ile e-posta gönderiliyor

Django için bir çözüm bulabildiğim en yakın ve bu çözümü C# 'ye taşıyarak çok fazla başarı elde edemedim. Burada o cevap buldu:

Displaying inline images on iPhone, iPad ben bu şekilde ekleri yapılandırın:

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

nasıl sadece bir kez bu görüntüleri görüntüleme hakkında gidebilir? Onları sadece satır içi olarak görüntülenecek şekilde görüntüleyebilmem gerekir. Bunun alternatif görünümler kullanmam gerektiği ve eğer varsa bunları kullanmaya nasıl devam edeceğimi bilmemden emin değilim.

DÜZENLEME:

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

yerleşik MailMessage için kullanılan kodunuzun geri kalanını temin etmek yararlı olabilir. – mason

+0

Bu çözümü kontrol ettiniz mi: http://stackoverflow.com/questions/18358534/send-inline-image-in-email? –

+0

@Adriano'ya bağlandığınız çözüme baktım ancak bu yöntemi kullanarak Outlook'taki e-postaya .bin dosyaları eklediğini ve iOS'ta biçimlendirme sorunlarını çözmediğini görüyorum. – awh112

cevap

4

Yani, benim sorunun ne olduğunu buldum: İşte

Ben e-postalar oluşturmak için kullandığınız kod kalandır. Yaptığım şey diğer pek çok çözümün bir kombinasyonuydu. Dosyaları Ekler olarak eklemek yerine alternatif bir görünüm oluşturdum ve dosyaları LinkedResources olarak ekledim. Yukarıdaki yorumda @Adriano tarafından sağlanan bağlantıya çok benziyordu, ancak emin olmak istediğim şey hem Ekleri hem de Bağlantılı Kaynakları kullanmaya çalışmayı bırakmaktı. İşaretlemede kullanılan ContentID'yi sağlamak için Eklere güvenmeyi bıraktıktan sonra, her şey beklendiği gibi çalışır. Ben çalışmak lazım kodu (ı değiştirildi şeyin netlik için dışarı yorumladı parçaları sol) aşağıdaki gibidir:

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; 
} 
İlgili konular