blah blah blah is here! blah blah » Close

up0down
link

So I have this code, right:
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
void Application_ItemSend(object Item, ref bool Cancel)
{
//Do stuff.
}
Which works fine.
However, I need to capture the message after it has been sent, so to get it when its Send property is True.
Any ideas? Been googling for hours but I can't find any events that register after the sending of a message...

last answered 2 years ago

1 answers

up0down
link

I found this <a target="_new" href="http://msdn.microsoft.com/en-us/library/aa289167(VS.71).aspx">lengthy article</a> on Outlook programming using C#, from which it appears that it's the ApplicationEvents_11_NewMailEx event that you need to handle.
Judging by the sample code in the article, I'd guess it's something like this that you need:
// wire up event
this.Application.NewMailEx += new Microsoft.Office.Interop.Outlook. ApplicationEvents_11_NewMailExEventHandler(Application_NewMailEx);
// handler
void Application_NewMailEx(string EntryIDCollection)
{
ApplicationClass o = new ApplicationClass();
NameSpace outlookNS = o.GetNamespace("MAPI");
MAPIFolder mFolder =
o.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
MailItem newMail = (MailItem)
outlookNS.GetItemFromID(EntryIDCollection, mFolder.StoreID);
// Now print out.
string message = "";
message += "\r\n\r\n***** New Message *****";
message += String.Format("-> Sender: {0}", newMail.SenderName);
message += String.Format("-> Subject: {0}", newMail.Subject);
message += String.Format("-> Body: {0}", newMail.Body);
message += "***********************\r\n\r\n";
MessageBox.Show(message);
}

up0down
link

I looked at the intellisense docs on the NewMail and NewMailEx events, but I guess I didn't look hard enough ... I will try this out soon and post back. Thank you! :)

up0down
link

It seems I didn't explain the situation correctly.
I need to capture e-mail messages when they are sent, not received. I tried altering the code from the article to take the message from the Sent Items folder, but this event only fires when something lands in the Inbox.
I can't find any events that fire when something lands in the Sent Items folder :(

up0down
link

Reading it again, I think you did explain it reasonably clearly but I misunderstood what you wanted :(
Anyway, having had another look around I think it's the Items.ItemAdd event you need to handle in relation to the SentMail folder.
I'd tentatively suggest the following code:
// wire up event
ApplicationClass o = new ApplicationClass();
NameSpace outlookNS = o.GetNamespace("MAPI");
MAPIFolder mFolder =
o.Session.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
mFolder.Items.ItemAdd +=new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(SentItems_ItemAdd);
// handler
void SentItems_ItemAdd(object Item)
{
MailItem sentMail = Item as MailItem
string message = "";
message += "\r\n\r\n***** New Message *****";
message += String.Format("-> Sender: {0}", sentMail.SenderName);
message += String.Format("-> Subject: {0}", sentMail.Subject);
message += String.Format("-> Body: {0}", sentMail.Body);
message += "***********************\r\n\r\n";
MessageBox.Show(message);
}

up0down
link

omg it workz!!1!one
:) sorry, just happened to read one of koolp2ppirate's posts before writing this
That one works like a charm, thanks :)

up0down
link

Alas, I spoke to soon.
It seems the add-in only works once, and then I have to restart my Outlook for it to kick in again. Also, Outlook pops up the "an application is attempting to access your mailbox" dialog when my add-in is activated...
Any ideas? :)

up0down
link

I thought it was too good to be true ;)
On the second point, I found <a target="_new" href="http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/2cd750b1-f732-4810-97e6-8e461dc514a6">this thread</a> which suggests that you have to use a 'trusted' application object (i.e. the one obtained from the OnConnection method) to avoid the Outlook security warnings.
I don't know what to suggest on the first point though it's possible that using the trusted object may improve matters there too.

up0down
link

That did the trick :D I simply used this code:
this.Application.Session.GetDefaultFolder(OlDefaultFolders.olFolderSentMail).Items.ItemAdd +=
new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(SentItems_ItemAdd);
... not something I would have stumbled upon myself though :-O

up0down
link

Did that solve both problems or just the second one?

up0down
link

Both :D
As the default choice for the "someone's doing something" dialog in outlook is to allow access for one minute, I imagine that somehow messes up my code for one minute... Not sure what the actual ramifications of that dialog are though :)

up0down
link

d'oh ... guess not
It seems that a few moments after my code has first run, it will never run again until Outlook is restarted.
This is my full code:
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
this.Application.Session.GetDefaultFolder(OlDefaultFolders.olFolderSentMail).Items.ItemAdd +=
new Microsoft.Office.Interop.Outlook.ItemsEvents_ItemAddEventHandler(SentItems_ItemAdd);
}
void SentItems_ItemAdd(object Item)
{
try
{
string archivesetting = ReadSetting();
if (Item != null && Item is MailItem && archivesetting != "Off")
{
bool go = false;
if (archivesetting == "Ask")
{
DialogResult r = MessageBox.Show();
if (r == DialogResult.Yes)
go = true;
}
else if (archivesetting == "On")
go = true;
if (go)
{
//Save the mail in a temporary location.
MailItem item = (MailItem)Item;
if (!Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), pathToTempFolder))
Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), pathToTempFolder));
string legal = "abcdefghijklmnopqrstuvwxyz1234567890-";
string subject = item.Subject;
if (subject.Trim() == "")
subject = DateTime.Now.ToString("ddMMyyyyHHmmss");
string temp = "";
foreach (char c in subject)
if (legal.Contains(c.ToString().ToLower()))
temp += c;
subject = temp;
string filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), pathToTempFolder + subject + ".msg");
while (File.Exists(filename))
filename = Path.GetFileNameWithoutExtension(filename) + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".msg";
item.SaveAs(filename, OlSaveAsType.olMSG);
Process[] procs = Process.GetProcessesByName(filename);
bool found = false;
foreach (Process p in procs)
{
if (GetProcessOwner(p.Id) == Environment.UserName)
{
found = true;
break;
}
}
if (!found)
{
//Start it
Process.Start(filename);
}
}
}
}
catch (System.Exception x)
{
MessageBox.Show(x.ToString());
}
}
Any ideas?

up0down
link

Garbage-collector, thou art a heartless b****...
Hint taken from <a target="_new" href="http://stackoverflow.com/questions/294529/why-does-the-itemadd-event-stop-being-handled-on-my-sent-items-folder">here</a>
It seems my eventhandler isn't getting handled because of a scope-issue.
This is my current code, still doesn't work:
public partial class ThisAddIn
{
private MAPIFolder sentitems = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
sentitems = this.Application.Session.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
sentitems.Items.ItemAdd += SentItems_ItemAdd;
}
Any idea on how to keep this from getting torn down?

up0down
link

Well I went and made a right mess of this thread now didn't I?
this is the solution:
private MAPIFolder sentfolder = null;
private Items sentitems = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
sentfolder = this.Application.Session.GetDefaultFolder(OlDefaultFolders.olFolderSentMail);
sentitems = sentfolder.Items;
sentitems.ItemAdd += SentItems_ItemAdd;
}
Taken from <a target="_new" href="http://blogs.msdn.com/mstehle/archive/2007/12/06/oom-net-part-1-introduction-why-events-stop-firing.aspx">here</a>
Apparently, the Items instance is the key to this problem...

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback