I have this code that actually works in all cases except when the image is sent by an IOS device, in that case attachments appears in blank.
I connect trough IMAP and take UIDS, then I extract information about email, save attachments and save entire email.
Everything is fine except when the sender attach documents or images by IOS, in that cases the code cant find any documents.
What can I do ?
Thanks
try {
IMailFolder mailFolder = imapClient.GetFolder(Folder);
mailFolder.Open(FolderAccess.ReadOnly);
MimeMessage m = mailFolder.GetMessage(new UniqueId(Decimal.ToUInt32(UID)));
//MailMessage m = imapClient.GetMessage(UID, false, false);
Subject = m.Subject;
From_Name = (m.Sender != null) ? m.Sender.ToString() : "";//m.Sender.DisplayName;
From_Address = (m.From != null) ? m.From.ToString() : "";
Cc_Address = (m.Cc != null) ? m.Cc.ToString() : "";
Date_Sent = m.Date.DateTime;
MessageID = m.MessageId;
Body = m.TextBody;
m.WriteTo (EmailName);
if ( Save_Attachments && Attachments_Path != "") {
System.IO.Directory.CreateDirectory(Attachments_Path);
DataTable dt = new DataTable();
dt.Columns.Add("Path", typeof(String));
foreach (MimeEntity attachment in m.Attachments)
{
if (attachment is MimePart)
{
MimePart part = (MimePart)attachment;
string path = System.IO.Path.Combine(Attachments_Path, removeIllegal(part.FileName));
using (var stream = File.Create(path))
{
part.Content.DecodeTo(stream);
}
DataRow dr = dt.NewRow();
dr["Path"] = path;
dt.Rows.Add(dr);
}
}
Attachments = dt;
} else {
Attachments = null;
}
}
catch (Exception ex) {
Subject = "";
From_Name = "";
From_Address = "";
Cc_Address = "";
Date_Sent = new DateTime();
MessageID = "";
Body = "";
Attachments = null;
throw ex;
}
Instead of using MimeMessage.Attachments, you need to use MimeMessage.BodyParts - but be aware that BodyParts also contains the text body of the message.
I have more information here: http://www.mimekit.net/docs/html/Working-With-Messages.htm
A quick & dirty way of accomplishing what you want to do is probably something like this:
foreach (var attachment in m.BodyParts.Where (x => x.ContentDisposition?.FileName != null))
(Note: you'll need to make sure to add using System.Linq; at the top of your C# file)
Related
I am trying to find a solution to distinguish between an embedded image and attachment in Outlook mail. After doing some research, i found the following code works for most of the case
foreach (Outlook.Attachment attachment in mailItem.Attachments)
{
try
{
var attachmentType = attachment.FileName.Substring(attachment.FileName.LastIndexOf('.'));
if (attachmentType!=null&&attachmentType.Trim().Length>1&&_fileTypeFilter.Contains(attachmentType.Substring(1).ToLower()))
{
prop=attachment.PropertyAccessor;
string conentId = (string)prop.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E");
if ((attachmentType.Substring(1).ToLower() == "pdf") ||(conentId==null||conentId.Trim().Length==0))
{
//Always allow PDF
// This is an attachement
}
}
}
catch (Exception ex)
{
}
}
The issue is when a mail is send from other mail systems (for eg: hotmail ) then the content id is not null for attachments. This cause the attachments to be ignored .
Another suggestion i tired is to check the property based on following StackFlow don't save embedded
foreach (Outlook.Attachment attachment in mailItem.Attachments)
{
try
{
// var tst = attachment.Type;
var attachmentType = attachment.FileName.Substring(attachment.FileName.LastIndexOf('.'));
if (attachmentType!=null&&attachmentType.Trim().Length>1&&_fileTypeFilter.Contains(attachmentType.Substring(1).ToLower()))
{
prop=attachment.PropertyAccessor;
string conentId = (string)prop.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E");
var flags = prop.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x37140003");
var asize = attachment.Size;
if ((attachmentType.Substring(1).ToLower() == "pdf") ||
(asize>0&&(flags!=4 &&(int)attachment.Type != 6))) // As per present understanding - If rtF mail attachment comes here - and the embeded image is treated as attachment then Type value is 6 and ignore it
// (conentId==null||conentId.Trim().Length==0))
{
//This is a valid attachment
}
}
}
catch (Exception ex)
{
}
}
But this sometimes includes the image in signature
the most reliable way is to parse the HTML body (MailItem.HTMLBody property) to extract all img tags and check their scr attributes. If its is of the form ""cid:xyz", then "xyz" will be the value of the PR_ATTACH_CONTENT_ID property on the attachment. It can also refer to the graphics file by its file name.
Here is the working solution
var selection = explorer.Selection;
if (selection.Count == 1)
{
object selectedItem = selection[1];
var mailItem = selectedItem as Outlook.MailItem;
if (mailItem == null) return;
foreach (Outlook.Attachment attachment in mailItem.Attachments)
{
bool validAttachment = isAnAttachment(attachment);
}
}
private bool isAnAttachment(Outlook.Attachment attachment)
{
bool isValid = false;
var attachmentType = attachment.FileName.Substring(attachment.FileName.LastIndexOf('.'));
if (attachmentType != null && attachmentType.Trim().Length > 1 && _fileTypeFilter.Contains(attachmentType.Substring(1).ToLower()))
{
Outlook.PropertyAccessor prop = attachment.PropertyAccessor;
var flags = prop.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x37140003");
var asize = attachment.Size;
// As per present understanding - If rtF mail attachment comes here - and the embeded image is treated as attachmet
if ((attachmentType.Substring(1).ToLower() == "pdf") || (asize > 0 && flags != 4 && (int)attachment.Type != 6))
{
isValid = true;
}
}
return isValid;
}
I'm using MailKit library to handle emails, which has been working well. However, I'm trying to split emails into their constituent files a) Main email (no attachments) b) Individual attachment files, to store on the filesystem.
I can save the attachments individually, but can't seem to remove them from the email body code. I.e. they're getting saved along with the main email, so duplicating data. :/
I've tried:
foreach (MimePart part in inMessage.BodyParts)
{
if (part.IsAttachment)
{
// Remove MimePart < This function isn't available on the collection.
}
}
Have also tried:
var builder = new BodyBuilder();
foreach (MimePart part in inMessage.BodyParts)
{
if (!part.IsAttachment)
{
// Add MimeParts to collection < This function isn't available on the collection.
}
}
outMessage.Body = builder.ToMessageBody();
If anyone can help with this, I'd much appreciate it.
Solution implemented FYI:
private string GetMimeMessageOnly(string outDirPath)
{
MimeMessage message = (Master as fsEmail).GetMimeMessage();
if (message.Attachments.Any())
{
var multipart = message.Body as Multipart;
if (multipart != null)
{
while (message.Attachments.Count() > 0)
{
multipart.Remove(message.Attachments.ElementAt(0));
}
}
message.Body = multipart;
}
string filePath = outDirPath + Guid.NewGuid().ToString() + ".eml";
Directory.CreateDirectory(Path.GetDirectoryName(outDirPath));
using (var cancel = new System.Threading.CancellationTokenSource())
{
using (var stream = File.Create(filePath))
{
message.WriteTo(stream, cancel.Token);
}
}
return filePath;
}
And to get the attachments only:
private List<string> GetAttachments(string outDirPath)
{
MimeMessage message = (Master as fsEmail).GetMimeMessage();
List<string> list = new List<string>();
foreach (MimePart attachment in message.Attachments)
{
using (var cancel = new System.Threading.CancellationTokenSource())
{
string filePath = outDirPath + Guid.NewGuid().ToString() + Path.GetExtension(attachment.FileName);
using (var stream = File.Create(filePath))
{
attachment.ContentObject.DecodeTo(stream, cancel.Token);
list.Add(filePath);
}
}
}
return list;
}
You could retrieve all MimeParts that are attachments https://github.com/jstedfast/MimeKit/blob/master/MimeKit/MimeMessage.cs#L734 and then iterate over the all Multiparts and call https://github.com/jstedfast/MimeKit/blob/master/MimeKit/Multipart.cs#L468 for the attachments to remove.
The sample below makes a few assumptions about the mail e.g. there is only one Multipart some email client (Outlook) are very creative how mails are crafted.
static void Main(string[] args)
{
var mimeMessage = MimeMessage.Load(#"x:\sample.eml");
var attachments = mimeMessage.Attachments.ToList();
if (attachments.Any())
{
// Only multipart mails can have attachments
var multipart = mimeMessage.Body as Multipart;
if (multipart != null)
{
foreach(var attachment in attachments)
{
multipart.Remove(attachment);
}
}
mimeMessage.Body = multipart;
}
mimeMessage.WriteTo(new FileStream(#"x:\stripped.eml", FileMode.CreateNew));
}
Starting with MimeKit 0.38.0.0, you'll be able to use a MimeIterator to traverse the MIME tree structure to collect a list of attachments that you'd like to remove (and remove them). To do this, your code would look something like this:
var attachments = new List<MimePart> ();
var multiparts = new List<Multipart> ();
var iter = new MimeIterator (message);
// collect our list of attachments and their parent multiparts
while (iter.MoveNext ()) {
var multipart = iter.Parent as Multipart;
var part = iter.Current as MimePart;
if (multipart != null && part != null && part.IsAttachment) {
// keep track of each attachment's parent multipart
multiparts.Add (multipart);
attachments.Add (part);
}
}
// now remove each attachment from its parent multipart...
for (int i = 0; i < attachments.Count; i++)
multiparts[i].Remove (attachments[i]);
I created an application, that downloads emails and attachments as well using Mailkit.
I faced one problem: E-Mails sent from iOS with attached pictures were not processed correctly. MailKit did not add the images to the Attachments list.
I used this method to get only the text of the message:
private static string GetPlainTextFromMessageBody(MimeMessage message)
{
//content type needs to match text/plain otherwise i would store html into DB
var mimeParts = message.BodyParts.Where(bp => bp.IsAttachment == false && bp.ContentType.Matches("text", "plain"));
foreach (var mimePart in mimeParts)
{
if (mimePart.GetType() == typeof(TextPart))
{
var textPart = (TextPart)mimePart;
return textPart.Text;
}
}
return String.Empty;
}
This is the method I used to download only the .jpg files:
foreach (var attachment in message.BodyParts.Where(bp => !string.IsNullOrEmpty(bp.FileName)))
{
if (attachment.FileName.ToLowerInvariant().EndsWith(".jpg"))
{
//do something with the image here
}
}
I want ask if the folder exist, if no create it.I try the foloowing but when I became to the bold row below in the code I get error:NO Command received in Invalid state.
using (ImapClient ic = new ImapClient(imapAddr, myEmailID, myPasswd, ImapClient.AuthMethods.Login, portNo, secureConn))
{
bool headersOnly = false;
ic.SelectMailbox("INBOX");
Lazy<MailMessage>[] messages = ic.SearchMessages(SearchCondition.Unseen(), headersOnly);
foreach (Lazy<MailMessage> message in messages)
{
MailMessage m = message.Value;
AE.Net.Mail.Imap.Mailbox IsExistFolder = ic.Examine("INBOX/zipi4");
if (IsExistFolder == null)
{
ic.CreateMailbox("INBOX/zipi4");
}
string s = "INBOX/zipi2";
**ic.MoveMessage(m.Uid, s);**
ic.Expunge();
TextWriter writer1 = File.CreateText("Y:\\perl4.txt");
m.Save(writer1);
}
}
I have created a Web Email Application, How do I view and save attached files?
I am using OpenPop, a third Party dll, I can send emails with attachments and read emails with no attachments.
This works fine:
Pop3Client pop3Client = (Pop3Client)Session["Pop3Client"]; // Creating newPopClient
int messageNumber = int.Parse(Request.QueryString["MessageNumber"]);
Message message = pop3Client.GetMessage(messageNumber);
MessagePart messagePart = message.MessagePart.MessageParts[1];
lblFrom.Text = message.Headers.From.Address; // Writeing message.
lblSubject.Text = message.Headers.Subject;
lblBody.Text=messagePart.BodyEncoding.GetString(messagePart.Body);
This second portion of code displays the contents of the attachment, but that's only useful if its a text file. I need to be able to save the attachment. Also the bottom section of code I have here over writes the body of my message, so if I receive an attachment I can't view my message body.
if (messagePart.IsAttachment == true) {
foreach (MessagePart attachment in message.FindAllAttachments()) {
if (attachment.FileName.Equals("blabla.pdf")) { // Save the raw bytes to a file
File.WriteAllBytes(attachment.FileName, attachment.Body); //overwrites MessagePart.Body with attachment
}
}
}
If anyone is still looking for answer this worked fine for me.
var client = new Pop3Client();
try
{
client.Connect("MailServerName", Port_Number, UseSSL); //UseSSL true or false
client.Authenticate("UserID", "password");
var messageCount = client.GetMessageCount();
var Messages = new List<Message>(messageCount);
for (int i = 0;i < messageCount; i++)
{
Message getMessage = client.GetMessage(i + 1);
Messages.Add(getMessage);
}
foreach (Message msg in Messages)
{
foreach (var attachment in msg.FindAllAttachments())
{
string filePath = Path.Combine(#"C:\Attachment", attachment.FileName);
if(attachment.FileName.Equals("blabla.pdf"))
{
FileStream Stream = new FileStream(filePath, FileMode.Create);
BinaryWriter BinaryStream = new BinaryWriter(Stream);
BinaryStream.Write(attachment.Body);
BinaryStream.Close();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("", ex.Message);
}
finally
{
if (client.Connected)
client.Dispose();
}
for future readers there is easier way with newer releases of Pop3
using( OpenPop.Pop3.Pop3Client client = new Pop3Client())
{
client.Connect("in.mail.Your.Mailserver.com", 110, false);
client.Authenticate("usernamePop3", "passwordPop3", AuthenticationMethod.UsernameAndPassword);
if (client.Connected)
{
int messageCount = client.GetMessageCount();
List<Message> allMessages = new List<Message>(messageCount);
for (int i = messageCount; i > 0; i--)
{
allMessages.Add(client.GetMessage(i));
}
foreach (Message msg in allMessages)
{
var att = msg.FindAllAttachments();
foreach (var ado in att)
{
ado.Save(new System.IO.FileInfo(System.IO.Path.Combine("c:\\xlsx", ado.FileName)));
}
}
}
}
The OpenPop.Mime.Message class has ToMailMessage() method that converts OpenPop's Message to System.Net.Mail.MailMessage, which has an Attachments property. Try extracting attachments from there.
I wrote this quite a long time ago, but have a look at this block of code that I used for saving XML attachments within email messages sat on a POP server:
OpenPOP.POP3.POPClient client = new POPClient("pop.yourserver.co.uk", 110, "your#email.co.uk", "password_goes_here", AuthenticationMethod.USERPASS);
if (client.Connected) {
int msgCount = client.GetMessageCount();
/* Cycle through messages */
for (int x = 0; x < msgCount; x++)
{
OpenPOP.MIMEParser.Message msg = client.GetMessage(x, false);
if (msg != null) {
for (int y = 0; y < msg.AttachmentCount; y++)
{
Attachment attachment = (Attachment)msg.Attachments[y];
if (string.Compare(attachment.ContentType, "text/xml") == 0)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
string xml = attachment.DecodeAsText();
doc.LoadXml(xml);
doc.Save(#"C:\POP3Temp\test.xml");
}
}
}
}
}
List<Message> lstMessages = FetchAllMessages("pop.mail-server.com", 995, true,"Your Email ID", "Your Password");
The above line of code gets the list of all the messages from your email using corresponding pop mail-server.
For example, to get the attachment of latest (or first) email in the list, you can write following piece of code.
List<MessagePart> lstAttachments = lstMessages[0].FindAllAttachments(); //Gets all the attachments associated with latest (or first) email from the list.
for (int attachment = 0; attachment < lstAttachments.Count; attachment++)
{
FileInfo file = new FileInfo("Some File Name");
lstAttachments[attachment].Save(file);
}
private KeyValuePair<byte[], FileInfo> parse(MessagePart part)
{
var _steam = new MemoryStream();
part.Save(_steam);
//...
var _info = new FileInfo(part.FileName);
return new KeyValuePair<byte[], FileInfo>(_steam.ToArray(), _info);
}
//... How to use
var _attachments = message
.FindAllAttachments()
.Select(a => parse(a))
;
Just in case someone wants the code for VB.NET:
For Each emailAttachment In client.GetMessage(count).FindAllAttachments
AttachmentName = emailAttachment.FileName
'----// Write the file to the folder in the following format: <UniqueID> followed by two underscores followed by the <AttachmentName>
Dim strmFile As New FileStream(Path.Combine("C:\Test\Attachments", EmailUniqueID & "__" & AttachmentName), FileMode.Create)
Dim BinaryStream = New BinaryWriter(strmFile)
BinaryStream.Write(emailAttachment.Body)
BinaryStream.Close()
Next
I need to extract/export the lotus notes email attachment into file system. for that I wrote following method but each time I am receiving an error at line foreach (NotesItem nItem in items).. Can anybody please tell me what I am doing wrong ..
Thanks
Jwalin
public void GetAttachments()
{
NotesSession session = new NotesSession();
//NotesDocument notesDoc = new NotesDocument();
session.Initialize("");
NotesDatabase NotesDb = session.GetDatabase("", "C:\\temps\\lotus\\sss11.nsf", false); //Open Notes Database
NotesView inbox = NotesDb.GetView("By _Author");
NotesDocument docInbox = inbox.GetFirstDocument();
object[] items = (object[])docInbox.Items;
**foreach (NotesItem nItem in items)**
{
//NotesItem nItem = (NotesItem)o1;
if (nItem.Name == "$FILE")
{
NotesItem file = docInbox.GetFirstItem("$File");
string fileName = ((object[])nItem.Values)[0].ToString();
NotesEmbeddedObject attachfile = (NotesEmbeddedObject)docInbox.GetAttachment(fileName);
if (attachfile != null)
{
attachfile.ExtractFile("C:\\temps\\export\\" + fileName);
}
}
}
You don't need to use the $File item to get the attachment name(s). Rather, you can use the HasEmbedded and EmbeddedObject properties of the NotesDocument class.
public void GetAttachments()
{
NotesSession session = new NotesSession();
//NotesDocument notesDoc = new NotesDocument();
session.Initialize("");
NotesDatabase NotesDb = session.GetDatabase("", "C:\\temps\\lotus\\sss11.nsf", false); //Open Notes Database
NotesView inbox = NotesDb.GetView("By _Author");
NotesDocument docInbox = inbox.GetFirstDocument();
// Check if any attachments
if (docInbox.hasEmbedded)
{
NotesEmbeddedObject attachfile = (NotesEmbeddedObject)docInbox.embeddedObjects[0];
if (attachfile != null)
{
attachfile.ExtractFile("C:\\temps\\export\\" + attachfile.name);
}
}
Ed's solution didn't work for me. Something about my Notes db design seems to have left the EmbeddedObjects property null even when the HasEmbedded flag is true. So I sort of combined the Ed's and Jwalin's solutions, modifying them to fetch all attachments from a Notes document.
NotesDocument doc = viewItems.GetNthEntry(rowCount).Document;
if (doc.HasEmbedded)
{
object[] items = (object[])doc.Items;
foreach (NotesItem item in items)
{
if(item.Name.Equals("$FILE"))
{
object[] values = (object[])item.Values;
doc.GetAttachment(values[0].ToString()).ExtractFile(fileSavePath + values[0].ToString());
}
}