MailKit save Attachments - c#

I'm try save attachments from message
foreach(MimeKit.MimeEntity at message.Attachments)
{
at.WriteTo("nameFile");
}
File saved, but when I open I get the error
the file is corrupted or too large
The size of this file is 88 kb, but size of the file should be equal to 55 kb.
I think that in all recorded message file.
How do I only record the attachment?
MailKit v1.2.0.0 MimeKit 1.2.0.0

You are saving the entire MIME object (including the headers). What you need to do is save the content.
foreach (var attachment in message.Attachments) {
using (var stream = File.Create ("fileName")) {
if (attachment is MessagePart) {
var part = (MessagePart) attachment;
part.Message.WriteTo (stream);
} else {
var part = (MimePart) attachment;
part.Content.DecodeTo (stream);
}
}
}

Related

Stream was not readable with Using

I am getting an error
File is being used by another process
trying to implement using for a FileStream. However, I encountered the error of Stream was not readable.
This is my code:
Before: working, but encounters 'file being used by another process' error periodically
EmailMessage responseMessageWithAttachment = responseMessage.Save();
foreach (var attachment in email.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
fileAttachment.Load();
fileAttachment.Load(AppConfig.EmailSaveFilePath + fileAttachment.Name);
FileStream fs = new FileStream(AppConfig.EmailSaveFilePath + fileAttachment.Name, FileMode.OpenOrCreate);
responseMessageWithAttachment.Attachments.AddFileAttachment(attachment.Name, fs);
}
}
responseMessageWithAttachment.SendAndSaveCopy();
After: encounters 'stream was not readable' error
EmailMessage responseMessageWithAttachment = responseMessage.Save();
foreach (var attachment in email.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
fileAttachment.Load();
fileAttachment.Load(AppConfig.EmailSaveFilePath + fileAttachment.Name);
using (FileStream fs = new FileStream(AppConfig.EmailSaveFilePath + fileAttachment.Name, FileMode.OpenOrCreate))
{
responseMessageWithAttachment.Attachments.AddFileAttachment(attachment.Name, fs);
};
}
}
responseMessageWithAttachment.SendAndSaveCopy();
working, but encounter 'file being used by another process' error periodically
This means what it says: some other process is touching the file. If you want to solve this, you need to figure out what's using the file. This will happen whether you use using or not.
If this code is running multiple times in parallel, it could be your own code interfering. Either way, you could avoid it by open for reading only, but specifically allowing other processes to open it for writing. You would do that like this:
var fs = new FileStream(Path.Combine(AppConfig.EmailSaveFilePath, fileAttachment.Name),
FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
encounter 'stream was not readable' error
This depends on how AddFileAttachment is implemented. You don't show the stack trace, so it's possible that it doesn't read the stream until you call SendAndSaveCopy(), which is outside the using and the stream is closed.
An easy way to work around this is to just use the overload of AddFileAttachment that just takes the path to the file as a string, so you don't need to manage the FileStream yourself:
responseMessageWithAttachment.Attachments.AddFileAttachment(attachment.Name,
Path.Combine(AppConfig.EmailSaveFilePath, fileAttachment.Name));
I use Path.Combine since it avoids problems where there may or may not be a trailing \ in your EmailSaveFilePath setting.
I wonder if you can avoid saving the files and just use Content and AddFileAttachment(String, Byte[])
foreach (var attachment in email.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
fileAttachment.Load();
responseMessageWithAttachment.Attachments.AddFileAttachment(attachment.Name, fileAttachment.Content);
}
}
responseMessageWithAttachment.SendAndSaveCopy();

Is there a way to Extract attachment from Email Message Mime Content without using Exchange Service?

I am having a client application which will save eml files to the local disk. Need to get the attachment inside the eml file which is saved without using the exchange service because the Mailbox keeps changing of its capacity.Please help if anyone have come across similar issue
I have tried the reverse process of getting the eml file and load it again to get the details.
You could use something like MimeKit for this. The GitHub page has examples on how to parse MIME messages and how to get attachments.
Here is an example of how to get the attachments in an array of bytes:
var mimeMessage = MimeMessage.Load(#"test.eml");
var attachments = mimeMessage.Attachments.ToList();
foreach (var attachment in attachments)
{
using (var memory = new MemoryStream())
{
if (attachment is MimePart)
((MimePart)attachment).Content.DecodeTo(memory);
else
((MessagePart)attachment).Message.WriteTo(memory);
var bytes = memory.ToArray();
}
}
First of all Big Thanks to MadDev for helping out !!!
Here is the Code which I used:
Note: Here in case, the stored email will always have another eml file attached to it and this is based on the business logic.
protected static void MimeProcessor(MemoryStream stream)
{
try
{
var parser = new MimeParser(stream, MimeFormat.Default);
var message = parser.ParseMessage();
var multipart = message.Body as Multipart;
//Found the Attachment as Message Part
var OriginalMessage = multipart.ToList().LastOrDefault();
if (OriginalMessage is MessagePart)
{
using (var memory = new MemoryStream())
{
((MessagePart)OriginalMessage).Message.WriteTo(memory);
var bytes = memory.ToArray();
File.WriteAllBytes("C:\\Test\\TestMessage.eml", bytes);
}
}
}
catch (Exception)
{
throw;
}
}

Deleting files gives an error (The process cannot access the file..)

I'm currently debugging my code because it gives me an error:
The process cannot access the file because it is being used by another process.
And i think that the error occurs in this lines of code
foreach (var filename in filenames)
{
var file = Path.Combine(filePath, filename);
mail.Attachments.Add(new Attachment(file));
}
// Send Mail
smtpServer.Send(mail);
DeleteFiles();
I want to delete the files in the folder when the mail is sent using this method
private void DeleteFiles()
{
string filePath = Server.MapPath("~/Content/attachments");
Array.ForEach(Directory.GetFiles(filePath), System.IO.File.Delete);
}
I read about closing/disposing? FileStream and etc. but how can i use that in my code? Thanks in advance.
mail.dispose(); You should dispose mail before deleting the file. This should remove the lock on the file.
foreach (var filename in filenames)
{
var file = Path.Combine(filePath, filename);
mail.Attachments.Add(new Attachment(file));
}
// Send Mail
smtpServer.Send(mail);
mail.Dispose();
DeleteFiles();
https://msdn.microsoft.com/en-us/library/0w54a951(v=vs.110).aspx
using(FileStream stream = new FileStream("thepath"))
{
//do stuff with the file
stream .Close();
}
Now the stream will be closed and disposed.

"Invalid folder" error when compressing files with SharpZipLib

I have this function that I'm using to compress a list of files from the user's session, and then streaming it to the user's browser for download:
public static void DownloadAllPhotos()
{
HttpContext.Current.Response.AddHeader(
"Content-Disposition", "attachment; filename=Photos.zip");
HttpContext.Current.Response.ContentType = "application/zip";
List<string> photos= new List<string>();
if (HttpContext.Current.Session != null &&
HttpContext.Current.Session["userPhotos"] != null)
{
photos = (List<string>)HttpContext.Current.Session["userPhotos"];
}
using (var zipStream = new
ZipOutputStream(HttpContext.Current.Response.OutputStream))
{
foreach (string photoUrl in photos)
{
byte[] fileBytes = File.ReadAllBytes(photoUrl);
var fileEntry = new ZipEntry(
Path.GetFileName(photoUrl))
{
Size = fileBytes.Length
};
zipStream.PutNextEntry(fileEntry);
zipStream.Write(fileBytes, 0, fileBytes.Length);
}
zipStream.Flush();
zipStream.Close();
// reset session
HttpContext.Current.Session["userPhotos"] = new List<string>();
}
}
When the user has photo urls in their session, and they click a button to call this function, the files are compressed and the download starts in the user's browser.
But when I try to open the compressed file, I get this error:
Windows cannot open the folder.
The compressed folder "{Path to my file}" is invalid.
Am I doing something wrong that's causing this error?
Check out the placement of Response.Flush and ZipEntry.CleanName in this example and see if writing something similar corrects the problem.
Also per the example in #cfeduke 's answer, there is a comment in the 'Create a Zip as a browser download attachment in IIS' that suggests changing Response.ContentType = "application/octet-stream" instead of "application/zip"
// If the browser is receiving a mangled zipfile, IIS Compression may
cause this problem. Some members have found that
//Response.ContentType = "application/octet-stream" has solved this.
May be specific to Internet Explorer.
Worked for me. And it was not IE specific (I use Chrome).

Converting attachment to image

Up to know i can load the attachments to memory and i know its right cause i can print the name of the file. What i need is to convert this attachment to an image object which i will later add to a sharepoint picture library. But forget about the sharepoint part i know how to do that, am stuck in the part that after loading the attachments how do i conver this into images. I dont want to save the images in disk cause thats not the point i already load them in memory.
foreach (Item item in findResults.Items)
{
if (item is EmailMessage && item.HasAttachments)
{
EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
foreach (Attachment attachment in message.Attachments)
{
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
// Load the file attachment into memory and print out its file name.
fileAttachment.Load();
Console.WriteLine("Attachment name: " + fileAttachment.Name);
//this is where i would create the image of object but dont know how
}
}
}
}
You already have the FileAttachment object, and you even access one of its properties. You only need to take the next step, and access not only the Name but also the Content.
if (attachment is FileAttachment)
{
FileAttachment fileAttachment = attachment as FileAttachment;
fileAttachment.Load();
byte[] fileContent = fileAttachment.Content;
}
This will give you the contents on the attachemnts, as an array of bytes. I don't remember what the Sharepoint API wants to receive, but it's either this byte array or something you can easily build out of it.

Categories