I have sent myself a test email which has an image in the body of the email. This image is not an attachment, just pasted into the body of the email.
I am using MailKit to read this incoming email, but cannot find how to access that image.
I'm using:
MimeMessage message = client.Inbox.GetMessage(uid);
If I use message.ToString()
, I can see that it is there:
...
Content-Disposition: inline; filename="image001.png"; size=4570;
creation-date="Mon, 16 Sep 2019 09:21:07 GMT";
modification-date="Mon, 16 Sep 2019 09:21:07 GMT"
Content-ID: <image001.png@01D56A4B.7CD234E0>
Content-Transfer-Encoding: base64
iVBORw0KGgoAAAANSUhEUgAAAJUAAAAlCAY...
And I assume the base64 encoded lines are the actual image, but how do I get at it?
EDIT
Here is my test code:
static void Main(string[] args)
{
ImapClient client = new ImapClient();
client.Connect("...
client.Authenticate("...
client.Inbox.Open(FolderAccess.ReadWrite);
IList<UniqueId> uids = client.Inbox.Search(SearchQuery.All);
foreach (UniqueId uid in uids)
{
MimeMessage message = client.Inbox.GetMessage(uid);
IList<IMessageSummary> info = client.Inbox.Fetch(new[] { uid }, MessageSummaryItems.All);
foreach (MimeEntity me in message.Attachments)
HandleMimeEntity(me, 1);
}
client.Disconnect(true);
}
static void HandleMimeEntity(MimeEntity entity)
{
int i, j;
Multipart multipart = entity as Multipart;
if (multipart != null)
{
Console.WriteLine("multipart");
for (i = 0; i < multipart.Count; i++)
{
Console.WriteLine(i + " - " + multipart[i].ContentType.MimeType + " (" + multipart[i].IsAttachment + ")");
HandleMimeEntity(multipart[i]);
}
return;
}
MessagePart rfc822 = entity as MessagePart;
if (rfc822 != null)
{
MimeMessage message = rfc822.Message;
Console.WriteLine("mimemessage - " + message.Subject);
HandleMimeEntity(message.Body, lvl + 1);
return;
}
MimePart part = (MimePart)entity;
Console.WriteLine("mimepart - " + part.FileName);
// do something with the MimePart, such as save content to disk
}