Get XML file from email attachement to variable - c#

So, I want to be able to get an XML file attachment from an email and store it in the database for later use.
I can retrieve the emails and store the SenderEmailAdress, sentOn and the body in the database. Now I want to add the XML file attachment to the database too, so I can use it later.
How can I get the attachment file (or the content) from the email and to a variable so I can add it to the DB?
Here's the code for the attachment I have now (so after I get the Mail Item):
//Check for attachments.
int AttachCnt = oMsg.Attachments.Count;
// Check if there are any attachments
if (AttachCnt > 0)
{
for (int i = 1; i <= AttachCnt; i++)
{
System.Diagnostics.Debug.WriteLine(i.ToString() + " - FileName: " + oMsg.Attachments[i].FileName);
String ext = Path.GetExtension(oMsg.Attachments[i].FileName);
// check if the file extention is .xml
if (ext == ".xml")
{
// Get the file ready to store in the DB. This is what I want to know!
return;
}
}
}

Save the attachment as a file (Attachment.SaveAsFile), then read the file data into a variable.

Related

Convert word document in to pdf at run time (at the time of uploading)

I have a scenario in my application in which user can upload business case document which is word document, and other users(admin and super admin) can see that business case and approve the document.
What is happening is admins and super admins have to download word document , which i don't want , i want them to see that document in a pdf format.
I could do that by restricting the user to upload only pdf files but that is not i want , I want him to upload word document and at the time of saving that file in to server , i want to save as PDF formtat .
Current code is working fine but it is saving file as word file , i want to save the file as pdf format.
int count = 0;
foreach (HttpPostedFileBase file in emailasign.Files)
{
var filename = "";
//Checking file is available to save.
if (file != null)
{
var random = new Random();
filename = random.Next(111111, 999999).ToString() + Path.GetExtension(file.FileName);
var ServerSavePath = Path.Combine(Server.MapPath("~/UploadedFiles/") + filename);
//Save file to server folder
file.SaveAs(ServerSavePath);
count++;
}
if (count > 1)
{
TempData["OrignalFile"] += "," + file.FileName;
TempData["FileName"] += "," + filename;
}
else if (count == 1)
{
TempData["OrignalFile"] = file.FileName;
TempData["FileName"] = filename;
}
}
Note: Right now it is accepting all format but i will restrict it in to word document only , so at the the time of uploading the document , i want to convert it in to PDF format and than save it on server

Save Embedded images and files from outlook

I want to save embedded images from outlook. After my analysis i found that user can embed images, and also PDF files etc. But in case of embedding PDF or other than image files(In outlook by using Insert menu and then Object option) the mail item attachment object also contains a .mso file(An MSO file is sent with email attachments and contains information that allows the attachment to be rendered within the email message) which i am not sure like i need to save it or not.
1)So is there any need to save or process the mso file or can i just ignore it and save all other file types?
2)Based on my observation even if user embed pdf file it will be attached as image file with .png extension in outlook inbox. So can i convert all those images to .gif as shown below? Will it failure in any scenario?
if (item.Attachments.Count > 0)
{
for (int j = 1; j <= item.Attachments.Count; j++)
{
string value = item.Attachments[j].PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E");
if (!String.IsNullOrEmpty(value))
{
index++;
string tempFilename = "C:\\TestFileSave\\" + "inlineimg_" + index.ToString() + System.IO.Path.GetExtension(item.Attachments[j].FileName);
if (System.IO.Path.GetExtension(item.Attachments[j].FileName) == "mso")
{
continue;
}
item.Attachments[j].SaveAsFile(tempFilename);
string mimeType = MimeMapping.GetMimeMapping(tempFilename);
if (mimeType.Contains("image/"))
{
System.Drawing.Image Image1 = System.Drawing.Image.FromFile(tempFilename);
Image1.Save("C:\\TestFileSave\\" + "inlineimg_" + index.ToString() + ".gif", System.Drawing.Imaging.ImageFormat.Gif);
Image1.Dispose();
System.IO.File.Delete(tempFilename);
}
}
}
}

How to upload multiple files to server with form post MVC?

I am working on a project of Asp.Net MVC-4. In my project user post requirements. Post contains title, tags, files etc.File may be multiple and it may be of any type like Video, doc ( ppt, excel, pdf, etc), images etc.
My problem is the handling of multiple file upload. Now first of all i tell you
currently how i am handling this :
I am using Jquery FIle Uplaod plugin. Through this plugin i am uploading file to server sequentially and on server i am saving those file with SessionId.Now when user post there requirement form than i just rename those file with userId.
drawback of my approach
First i have to save those files with session id and than i have to rename it with userId. So if i save my file in Window Azure Blobs than in that case for uploading single file i have to do 2 transaction. First save the blob with SessionId and than Renaming the blob with userid. Which i think result extra processing and extra cost.
Now i want to know if there is any approach by which i can upload all file (with progress bar for individual file [required]) with form post. So that user's requirement form (tags, titile etc) with all files go to server together than in that case i will save the user first in the database and than i will save the files with the userId ??
Note: I cannot save File with guid or other. UserId is required in the file name to uniquely identify user's files.
You can upload using following code in controller
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files) {
foreach (var file in files) {
if (file.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
}
return RedirectToAction("Index");
}
more details here
Not sure if i completely understand the question, but you could post the model for your user details in the same POST as the file upload (same form?), then on the server:
[HttpPost]
public JsonResult AddNewImage(UserModel user)
{
ReturnArgs r = new ReturnArgs();
repo.AddUser(user) // add your user to DB here
SaveImages(user.Id);
r.Status = 200;
r.Message = "OK!";
return Json(r);
}
private void SaveImages(string userid)
{
for (var i = 0; i < Request.Files.Count; i++)
{
var file = Request.Files[i] as HttpPostedFileBase;
string fileName = userid + "_" + i;
// saving file to DB here, but you can do what you want with
// the inputstream
repo.SaveImage(fileName, file.InputStream, file.ContentType);
}
}

vsto + differentiate attachments

I need to get and save the attachments(s) from a mail item, but using the code below returns all attachments - meaning it also returns the embedded images like the sender's signature with logo which is an image. How can I differentiate a true attachment vs. embedded images? I have seen a lot from forums but it is still unclear to me.
public static void SaveData(MailItem currentMailItem)
{
if (currentMailItem != null)
{
if (currentMailItem.Attachments.Count > 0)
{
for (int i = 1; i <= currentMailItem.Attachments.Count; i++)
{
currentMailItem.Attachments[i].SaveAsFile(#"C:\TestFileSave\" + currentMailItem.Attachments[i].FileName);
}
}
}
}
You can check whether an attachment is inline or not by using the following pseudo-code from MS Technet Forums.
if body format is plain text then
no attachment is inline
else if body format is RTF then
if PR_ATTACH_METHOD value is 6 (ATTACH_OLE) then
attachment is inline
else
attachment is normal
else if body format is HTML then
if PR_ATTACH_FLAGS value has the 4 bit set (ATT_MHTML_REF) then
attachment is inline
else
attachment is normal
You can access the message body format using MailItem.BodyFormat and the MIME attachment properties using Attachment.PropertyAccessor.
string PR_ATTACH_METHOD = 'http://schemas.microsoft.com/mapi/proptag/0x37050003';
var attachMethod = attachment.PropertyAccessor.Get(PR_ATTACH_METHOD);
string PR_ATTACH_FLAGS = 'http://schemas.microsoft.com/mapi/proptag/0x37140003';
var attachFlags = attachment.PropertyAccessor.Get(PR_ATTACH_FLAGS);

Append to file failure when executable not in same folder as data files

Problem is now solved. Mistake by me that I hadn't seen before.
I am pretty new to coding in general and am very new to C# so I am probably missing something simple. I wrote a program to pull data from a login website and save that data to files on the local hard drive. The data is power and energy data for solar modules and each module has its own file. On my main workstation I am running Windows Vista and the program works just fine. When I run the program on the machine running Server 2003, instead of the new data being appended to the files, it just overwrites the data originally in the file.
The data I am downloading is csv format text over a span of 7 days at a time. I run the program once a day to pull the new day's data and append it to the local file. Every time I run the program, the local file is a copy of the newly downloaded data with none of the old data. Since the data on the web site is only updated once a day, I have been testing by removing the last day's data in the local file and/or the first day's data in the local file. Any time I change the file and run the program, the file contains the downloaded data and nothing else.
I just tried something new to test why it wasn't working and think I have found the source of the error. When I ran on my local machine, the "filePath" variable was set to "". On the server and now on my local machine I have changed the "filePath" to #"C:\Solar Yard Data\" and on both machines it catches the file not found exception and creates a new file in the same directory which overwrites the original. Anyone have an idea as to why this happens?
The code is the section that download's each data set and appends any new data to the local file.
int i = 0;
string filePath = "C:/Solar Yard Data/";
string[] filenamesPower = new string[]
{
"inverter121201321745_power",
"inverter121201325108_power",
"inverter121201326383_power",
"inverter121201326218_power",
"inverter121201323111_power",
"inverter121201324916_power",
"inverter121201326328_power",
"inverter121201326031_power",
"inverter121201325003_power",
"inverter121201326714_power",
"inverter121201326351_power",
"inverter121201323205_power",
"inverter121201325349_power",
"inverter121201324856_power",
"inverter121201325047_power",
"inverter121201324954_power",
};
// download and save every module's power data
foreach (string url in modulesPower)
{
// create web request and download data
HttpWebRequest req_csv = (HttpWebRequest)HttpWebRequest.Create(String.Format(url, auth_token));
req_csv.CookieContainer = cookie_container;
HttpWebResponse res_csv = (HttpWebResponse)req_csv.GetResponse();
// save the data to files
using (StreamReader sr = new StreamReader(res_csv.GetResponseStream()))
{
string response = sr.ReadToEnd();
string fileName = filenamesPower[i] + ".csv";
// save the new data to file
try
{
int startIndex = 0; // start index for substring to append to file
int searchResultIndex = 0; // index returned when searching downloaded data for last entry of data on file
string lastEntry; // will hold the last entry in the current data
//open existing file and find last entry
using (StreamReader sr2 = new StreamReader(fileName))
{
//get last line of existing data
string fileContents = sr2.ReadToEnd();
string nl = System.Environment.NewLine; // newline string
int nllen = nl.Length; // length of a newline
if (fileContents.LastIndexOf(nl) == fileContents.Length - nllen)
{
lastEntry = fileContents.Substring(0, fileContents.Length - nllen).Substring(fileContents.Substring(0, fileContents.Length - nllen).LastIndexOf(nl) + nllen);
}
else
{
lastEntry = fileContents.Substring(fileContents.LastIndexOf(nl) + 2);
}
// search the new data for the last existing line
searchResultIndex = response.LastIndexOf(lastEntry);
}
// if the downloaded data contains the last record on file, append the new data
if (searchResultIndex != -1)
{
startIndex = searchResultIndex + lastEntry.Length;
File.AppendAllText(filePath + fileName, response.Substring(startIndex+1));
}
// else append all the data
else
{
Console.WriteLine("The last entry of the existing data was not found\nin the downloaded data. Appending all data.");
File.AppendAllText(filePath + fileName, response.Substring(109)); // the 109 index removes the file header from the new data
}
}
// if there is no file for this module, create the first one
catch (FileNotFoundException e)
{
// write data to file
Console.WriteLine("File does not exist, creating new data file.");
File.WriteAllText(filePath + fileName, response);
//Debug.WriteLine(response);
}
}
Console.WriteLine("Power file " + (i + 1) + " finished.");
//Debug.WriteLine("File " + (i + 1) + " finished.");
i++;
}
Console.WriteLine("\nPower data finished!\n");
Couple of suggestions wich I think will probably resolve the issue
First change your filePath string
string filePath = #"C:\Solar Yard Data\";
create a string with the full path
String fullFilePath = filePath + fileName;
then check to see if it exists and create it if it doesnt
if (!File.Exists(fullFilePath ))
File.Create(fullFilePath );
put the full path to the file in your streamReader
using (StreamReader sr2 = new StreamReader(fullFilePath))

Categories