Skip to content

Commit edc63e5

Browse files
Embedded images within HTML bodies were not being recognized by the MailKit as an attachment. (#984)
1 parent cf3e68e commit edc63e5

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

dotnet/src/dotnetframework/GxMail/Pop3MailKit.cs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal class Pop3MailKit : Pop3SessionBase
1515
private static readonly IGXLogger log = GXLoggerFactory.GetLogger<Pop3MailKit>();
1616

1717
private Pop3Client client;
18+
const string INLINE_IMAGE_PREFIX = "cid:";
1819

1920
public override int GetMessageCount()
2021
{
@@ -161,7 +162,7 @@ public override void Receive(GXPOP3Session sessionInfo, GXMailMessage gxmessage)
161162
}
162163
gxmessage.DateReceived = Internals.Pop3.MailMessage.GetMessageDate(GetHeaderFromMimeMessage(msg,"Delivery-Date"));
163164
AddHeader(gxmessage, "DispositionNotificationTo", GetHeaderFromMimeMessage(msg, "Disposition-Notification-To"));
164-
ProcessMailAttachments(gxmessage, msg.Attachments);
165+
ProcessMailAttachments(gxmessage, msg);
165166
}
166167
}
167168
}
@@ -192,8 +193,9 @@ private string GetHeaderFromMimeMessage(MimeMessage msg, string headerValue)
192193
return msg.Headers.Contains(headerValue) ? msg.Headers[msg.Headers.IndexOf(headerValue)].ToString() : null;
193194
}
194195

195-
private void ProcessMailAttachments(GXMailMessage gxmessage, IEnumerable<MimeEntity> attachs)
196+
private void ProcessMailAttachments(GXMailMessage gxmessage, MimeMessage msg)
196197
{
198+
IEnumerable<MimeEntity> attachs = msg.Attachments;
197199
if (attachs == null)
198200
return;
199201

@@ -202,28 +204,49 @@ private void ProcessMailAttachments(GXMailMessage gxmessage, IEnumerable<MimeEnt
202204
if (!Directory.Exists(AttachDir))
203205
Directory.CreateDirectory(AttachDir);
204206

205-
foreach (var attach in attachs)
207+
foreach (MimeEntity attach in attachs)
206208
{
207209
string attachName = FixFileName(AttachDir, attach is MessagePart ? attach.ContentDisposition?.FileName : ((MimePart)attach).FileName);
208-
if (!string.IsNullOrEmpty(attach.ContentId) && attach.ContentDisposition != null && !attach.ContentDisposition.IsAttachment)
209-
{
210-
string cid = "cid:" + attach.ContentId;
211-
attachName = String.Format("{0}_{1}", attach.ContentId, attachName);
212-
gxmessage.HTMLText = gxmessage.HTMLText.Replace(cid, attachName);
213-
}
214-
try
215-
{
216-
SaveAttachedFile(attach, attachName);
217-
gxmessage.Attachments.Add(attachName);
218-
}
219-
catch (Exception e)
210+
ProcessMailAttachment(gxmessage, attach, attachName);
211+
}
212+
foreach (MimeEntity attach in msg.BodyParts)
213+
{
214+
if (IsEmbeddedImage(attach, msg))
220215
{
221-
LogError("Could not add Attachment", "Failed to save attachment", MailConstants.MAIL_InvalidAttachment, e, log);
216+
string attachName = FixFileName(AttachDir, attach.ContentDisposition!=null ? attach.ContentDisposition?.FileName : ((MimePart)attach).FileName);
217+
ProcessMailAttachment(gxmessage, attach, attachName);
222218
}
223219
}
224220
}
225221
}
226222

223+
private bool IsEmbeddedImage(MimeEntity entity, MimeMessage msg)
224+
{
225+
MimePart attach = entity as MimePart;
226+
return (attach!=null && attach.ContentId != null && attach.Content != null && attach.ContentType.MediaType == "image" &&
227+
(msg.HtmlBody.IndexOf(INLINE_IMAGE_PREFIX + attach.ContentId) > -1));
228+
}
229+
230+
private void ProcessMailAttachment(GXMailMessage gxmessage, MimeEntity attach, string attachName)
231+
{
232+
if (!string.IsNullOrEmpty(attach.ContentId) && attach.ContentDisposition != null && !attach.ContentDisposition.IsAttachment)
233+
{
234+
string cid = INLINE_IMAGE_PREFIX + attach.ContentId;
235+
attachName = String.Format("{0}_{1}", attach.ContentId, attachName);
236+
gxmessage.HTMLText = gxmessage.HTMLText.Replace(cid, attachName);
237+
}
238+
try
239+
{
240+
SaveAttachedFile(attach, attachName);
241+
gxmessage.Attachments.Add(attachName);
242+
}
243+
catch (Exception e)
244+
{
245+
LogError("Could not add Attachment", "Failed to save attachment", MailConstants.MAIL_InvalidAttachment, e, log);
246+
}
247+
248+
}
249+
227250
private void SaveAttachedFile(MimeEntity attach, string attachName)
228251
{
229252
using (var stream = File.Create(Path.Combine(AttachDir, attachName)))

0 commit comments

Comments
 (0)