Hi ,
I`m just begining to learn c# and i ask if you please help me with a code to export in excel like described below :
I have a subfolder where emails contain a reply to an approval request email and i need to export this e-mails in excel this way ;
Aprroval response from the body of the response email in a cell and the date of the response in other cell
From the approval request : sender email in one cell , recipient in one cell , subject in one cell , date of approval request email in one cell and from the body of the email the first line of the email body in a cell , the second line in other cell...
For the begining i have the code below but i know is not right because is exporting first the emails in txt.Please tell me from where do i start to learn working in c# with outlook , word , excell because online i find very few tutorials on this.I like to
learn from tutorials but if it is more information on this in books, please guide me.
Thank you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Outlook;
namespace AccessingOutlookmails
{
class Program
{
public static void getEmails()
{
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
System.IO.StreamWriter file = new System.IO.StreamWriter("C:\\Users\\Daniel\\d.txt");
try
{
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
ns.Logon("daniel.tou@btrl.ro", "Albastru99", false, true);
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
subFolder = inboxFolder.Folders["Octavian"]; //folder.Folders[1]; also works
Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());
for (int i = 1; i <= subFolder.Items.Count; i++)
{
Microsoft.Office.Interop.Outlook.MailItem item = (Microsoft.Office.Interop.Outlook.MailItem)subFolder.Items[i];
file.WriteLine(i.ToString());
file.WriteLine(item.Subject);
file.WriteLine(item.SentOn.ToLongDateString());
file.WriteLine(item.Body.ToString());
}
file.Close();
}
catch (System.Runtime.InteropServices.COMException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
ns = null;
app = null;
inboxFolder = null;
}
}
public static void exportToExcel()
{
}
static void Main(string[] args)
{
getEmails();
Console.ReadKey();
}
}
}