Question

Locked

save email in msg format with c# add-in

By kanyon83 ·
I'm trying to create an add-in which will save selected emails in msg format. I have a custom toolbar with a button. The button opens a form, and on the form you can select the location you want the emails to be saved to. The problem occures when I try to save the emails. I get an error "System.NullReferenceException: Object reference not set to an instance of an object at button4_Click" (button4 starts the process). I've tried several methods - non of them worked Now I1m stuck and can't do much, although I know that the problem is with the Outlook.Mailitem declaration (it can't be null or something like that). I would really appreciate your help with this.
(I'm using Win XP, Outlook 2007, .NET framework 3.5 and language is C#.)
The code is:

private void button4_Click(object sender, EventArgs e)
{
Outlook.NameSpace _nameSpace = Globals.ThisAddIn.Application.GetNamespace("MAPI");
Outlook.MAPIFolder _mapiFolder = _nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Explorer _currentExplorer = Globals.ThisAddIn.Application.ActiveExplorer();
Outlook.MailItem _mailItem = _mapiFolder.Items as Outlook.MailItem;

string saveToFolder = comboBox1.Text;
//string _subject = _mailItem.Subject.ToString();
bool exists = Directory.Exists(saveToFolder);

try
{
Outlook.Selection _selection = _currentExplorer.Selection;
for (int i = 1; i <= _selection.Count; i++)
{
Object selObj = _mapiFolder.Items;
if (selObj is Outlook.MailItem && _mailItem != null)
{
if (exists == true)
{
_mailItem.SaveAs(saveToFolder, Outlook.OlSaveAsType.olMSG);
MessageBox.Show("you have " + _selection.Count + " items selected and your folder exists! Email saved!");
}
else if (exists == false)
{
System.IO.Directory.CreateDirectory(saveToFolder);
_mailItem.SaveAs(saveToFolder, Outlook.OlSaveAsType.olMSG);
MessageBox.Show("you have " + _selection.Count + " items selected and your folder was created successfully. Email saved!");
}
}
}
}
catch (NullReferenceException ex)//(Exception ex)
{
MessageBox.Show("Error: " + ex);
}
}

This conversation is currently closed to new comments.

10 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

solved

by kanyon83 In reply to save email in msg format ...

Okay, I managed to figure this one out on my own - which is good. I also made some modifications and expanded the code as well. If anybody's interested on how to create a tool that will save your emails in a specific format to a specified location, post here or send me an email and I'll do a tutorial on it.

Collapse -

e mail management with add-in

by ___krhn___ In reply to solved

Hi kanyon83,
I am Korhan, from Turkey, I am given a project about management of mails in outlook. I have to write a programme that will move the mails that are chosen to the specified folder. I will do this assignment in add-in which i have no idea yet; so while i was searching on net i saw your post. Glad that you were able to solve your problem yourself. I need your help for my project now. my email addresses are: korhan_ozturk_2359@hotmail.com and korhanozturk@ymail.com. Also you can contact with me by sending a message for sure.
Thanks in advance...

Collapse -

-

by kanyon83 In reply to e mail management with ad ...

Sent you a mail. Feel free to contact me, but I suggest we post results here so others may learn from it!

Collapse -

-

by ___krhn___ In reply to -

couldn't get your mail. you sure that you sent to the right address?

Collapse -

retrieving folders in outlook

by ___krhn___ In reply to -

Hello again
I'm stuck in the project that i've been working on and need some help. I want to retrieve all of the MAPI folders in outlook both default ones (inbox, drafts, etc) and the manually created ones. anyone with an idea please share.
thanks

Collapse -

Tutorial

by ekammer In reply to solved

Hi Kanyon83,

I need to write such a tool in c# and would be interested in your solution! My email is ekammer@carance.de.

Thanks,
ekammer

Collapse -

Reponse To Answer

by oeyee In reply to solved

Hi, Can you share with me the way to create the add-in and save email in a a specific format to a specific location? Thanks!

Collapse -

please create tutorial or send solution

by 3kwartappel In reply to save email in msg format ...

Hi kanyon83

Can you please give me your solution as well?
my message gets saved, but as soon as i close outlook, and try to open the saved message, it gives me the error you described.

thanks
David


Dim oApp As Outlook.Application
Dim oMsg As Outlook.MailItem
oApp = New Outlook.Application()
oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)
With oMsg
.To = "shashi.shah@imagingsciences.com"
.Subject = "Build Data"
.Body = "Data Attached."
.SaveAs("c:\temp.msg", OlSaveAsType.olMSG)
End With

Collapse -

can post the solution?

by juanjuanlee In reply to save email in msg format ...

Halo kanyon83
,

I am facing the same problem as you face before, where i need to save mail with certain "Subject' or highlight mails into my hard disc drive.

May i know how you do that?

Back to Windows Forum
10 total posts (Page 1 of 1)  

Related Discussions

Related Forums