Email incoming or sent

Add-in Express™ Support Service
That's what is more important than anything else

Email incoming or sent
 
Russell Edwards




Posts: 6
Joined: 2007-04-13
Hi

I hope this is a simple question, feel dumb for asking but is there a way where I can find out if an email is a draft, incoming or sent email.

My users tend to move emails around so I can't rely on the folder in which they are in.

Thanks
Posted 13 Apr, 2007 08:34:27 Top
David Ing




Posts: 39
Joined: 2007-02-16
The MailItem .Sent property is good for sent detection, i.e.
http://msdn2.microsoft.com/en-us/library/bb175529.aspx

Incoming would also have a .ReceivedTime property not null, (although that's worth checking as that's from my (poor) memory).

Those two things should give you the 3 states you need.
Posted 13 Apr, 2007 09:26:34 Top
Russell Edwards




Posts: 6
Joined: 2007-04-13
Thanks for that will look into it
Posted 13 Apr, 2007 09:36:54 Top
Russell Edwards




Posts: 6
Joined: 2007-04-13
Had a look the Sent property is true for both incoming and sent emails and the ReceivedTime is populated in both cases.

So unfortunately both properties dont help - open to suggestions
Posted 13 Apr, 2007 10:10:00 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Russell.

Please look at the following example:
http://www.add-in-express.com/projects/newmessageexample.zip


P.S. Note that we take up your forum requests in the order we receive them. Besides, it may take us some time to investigate your issue. Please be sure we will let you know as soon as the best possible solution is found.
Posted 13 Apr, 2007 12:34:42 Top
Russell Edwards




Posts: 6
Joined: 2007-04-13
Had a look at the example and unfortunately that doesnt work.

When I run it, the mail.SenderName is populated for both incoming and sent emails. For example for sent mails it comes up with 'Russell' for incoming it is the senders name.

I did try the approach of trying to get the login users email address and doing a comparison

i.e.
ns = this.OutlookApp.Session;
recpt = ns.CurrentUser;
sLoginUserAddress = recpt.Address;

and comparing

internal static bool GetEmailSender(object mapiObject, ref string senderEmailAddress)
{
bool bSuccess = false;

if (mapiObject == null)
return false;

string senderType = String.Empty;
IntPtr unk = IntPtr.Zero;
IntPtr unkObj = IntPtr.Zero;

try
{
unkObj = Marshal.GetIUnknownForObject(mapiObject);
Guid iMapiProp = new Guid("00020303-0000-0000-C000-000000000046");
Marshal.QueryInterface(unkObj, ref iMapiProp, out unk);

if (unk != IntPtr.Zero)
{
IntPtr pPropValue = IntPtr.Zero;

try
{
MAPI.HrGetOneProp(unk, MAPI.PR_SENDER_ADDRTYPE, out pPropValue);
MAPI.SPropValue propValue = (MAPI.SPropValue)Marshal.PtrToStructure(pPropValue, typeof(MAPI.SPropValue));
senderType = Marshal.PtrToStringAnsi(new IntPtr(propValue.Value));
}
catch
{
}

if (pPropValue != IntPtr.Zero)
MAPI.MAPIFreeBuffer(pPropValue);

if (senderType == "SMTP")
{
pPropValue = IntPtr.Zero;
try
{
MAPI.HrGetOneProp(unk, MAPI.PR_SENDER_EMAIL_ADDRESS, out pPropValue);
MAPI.SPropValue propValue = (MAPI.SPropValue)Marshal.PtrToStructure(pPropValue, typeof(MAPI.SPropValue));
senderEmailAddress = Marshal.PtrToStringAnsi(new IntPtr(propValue.Value));

bSuccess = true;
}
catch
{
}

if (pPropValue != IntPtr.Zero)
MAPI.MAPIFreeBuffer(pPropValue);
}
else if (senderType == "EX")
{
pPropValue = IntPtr.Zero;

try
{
MAPI.HrGetOneProp(unk, MAPI.PR_SENDER_NAME, out pPropValue);
MAPI.SPropValue propValue = (MAPI.SPropValue)Marshal.PtrToStructure(pPropValue, typeof(MAPI.SPropValue));
senderEmailAddress = Marshal.PtrToStringAnsi(new IntPtr(propValue.Value));

bSuccess = true;
}
catch
{
}

if (pPropValue != IntPtr.Zero)
MAPI.MAPIFreeBuffer(pPropValue);
}
else
throw new ArgumentException(string.Format("Failed to find sender type {0}, when retrieving senders email address"), senderType);
}
}
finally
{
Marshal.Release(unkObj);

if (unk != IntPtr.Zero)
Marshal.Release(unk);
}

if (bSuccess == false)
throw new ArgumentException("Failed to find senders email address");

return bSuccess;
}

however this seemed to work on some computers and not others. Was getting to be a pain trying to track down why - hence why I asked as I was worried I was going off track and making something very complicated for what should be a simple task.


So unfortunately still abit stumped...
Posted 13 Apr, 2007 13:24:38 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Russell, as far as I know the SenderName is populated when an email is sent. Do you want to detect emails in 'Sent Items' folders too?
Posted 13 Apr, 2007 13:41:08 Top
Russell Edwards




Posts: 6
Joined: 2007-04-13
Hi Sergey, and thanks for the help so far. For every email that is sent a copy of that email is moved to a folder by a rule. The same principle applies to incoming email.

These emails are vetted and its at this point I wish to determine whether or not it was an incoming or outgoing email from these folders. These emails can be moved around at the users disgression and consequently the folder location is not a reliable source.

The method of getting the login users email address and that of the senders email address seemed a bit overhanded but worked quite well in principle. Unfortunately I ran into occasions where the senders email address couldnt be determined - which I'm still ongoing testing to find out why.

One scenario, which I noticed was that outgoing email addresses i.e. the sender could vary if the user had multiple accounts i.e.

russell@test.com
russell@work.com

Thankfully, the number of users with the above are rare so I can say for the time being thats not supported.

Just trying to find something reliable for what I would assume to be a simple task. Note: not directing at you guys its not your product - 'Outlook', appreciate your help

btw I am using Outlook 2003
Posted 13 Apr, 2007 14:47:03 Top
Russell Edwards




Posts: 6
Joined: 2007-04-13
Yay! Got the email address comparison thing working in the end :) so no matter the email, the compare of who sent it to the logged in email address - result :)

Mark Ronson - Ooh Wee feat. Ghostface Killah, Nate Dogg
comes to mind

anyway the issue I had was that my machine has had multiple versions of outlook. When linking I was using some of the Mapi stuff, however, other machines only had outlook 2003 installed hence I could run stuff but on theres it did a wobbler.

Removed all outlook references readded the 11 library and touch wood going smoothly now.

Ta for the help though - thought I would let you know
Posted 13 Apr, 2007 17:28:03 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Russell.

Ok. Please let me know if you face any other difficulties.
Posted 16 Apr, 2007 08:01:01 Top