mike wolf
Posts: 4
Joined: 2006-02-16
|
hello,
would it be possible to get some insight into how you would pull out a senders from address from a new message. I have looked at the example in this message (http://www.add-in-express.com/forum/read.php?a&FID=5&TID=658&MID=3128&phrase_id=138680) , and tried to alter it using the folling
Outlook.Inspector ins = OutlookHost().ActiveInspector();
Outlook.MailItem m = ins.CurrentItem as Outlook.MailItem;
This gets me details about the current message, but when I try to us it with in the example above I get exceptions about an object not existing.
thanks |
|
mike wolf
Posts: 4
Joined: 2006-02-16
|
I got it doing the following
OutlookHost().GetNamespace("mapi").CurrentUser.Address
any one know any negative side effect of this? |
|
Sergey Grischenko
Add-in Express team
Posts: 7235
Joined: 2004-07-05
|
Hi Mike.
The fact is that the example above doesn't work with new messages. It works fine with received messages.
A new Outlook message doesn't contain the PR_SENDER_EMAIL_ADDRESS property. That is why you can't get the email address.
But you can use the code below to obtain the sender email address from a new Outlook message. However there is one restriction. You should definitely set the sender account via the Accounts popup control in the new message inspector window.
private class MAPI
{
........
public const uint PR_NEWMESSAGE_SENDER_EMAIL_ADDRESS = unchecked((uint)(PT_STRING8 | (0x8033 << 16)));
........
}
private void adxCommandBarButton2_Click(object sender)
{
object mapiObject = null;
Outlook.Inspector ins = OutlookHost().ActiveInspector();
object item = ins.CurrentItem;
if (item is Outlook._MailItem)
{
(item as Outlook._MailItem).Save();
mapiObject = (item as Outlook._MailItem).MAPIOBJECT;
if (mapiObject != null)
{
string senderType = String.Empty;
string senderEmail = 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;
pPropValue = IntPtr.Zero;
try
{
MAPI.HrGetOneProp(unk, MAPI.PR_NEWMESSAGE_SENDER_EMAIL_ADDRESS, out pPropValue);
MAPI.SPropValue propValue = (MAPI.SPropValue)Marshal.PtrToStructure(pPropValue, typeof(MAPI.SPropValue));
senderEmail = Marshal.PtrToStringAnsi(new IntPtr(propValue.Value));
}
catch
{
}
if (pPropValue != IntPtr.Zero)
MAPI.MAPIFreeBuffer(pPropValue);
if (senderEmail != String.Empty)
{
int pos = senderEmail.IndexOf((char)1);
if (pos > -1)
{
senderEmail = senderEmail.Remove(0, pos + 1);
}
MessageBox.Show(senderEmail);
}
else
MessageBox.Show("Error: Can't get the sender email address.");
}
}
finally
{
Marshal.Release(unkObj);
if (unk != IntPtr.Zero) Marshal.Release(unk);
Marshal.ReleaseComObject(mapiObject);
}
}
}
if (item != null) Marshal.ReleaseComObject(item);
Marshal.ReleaseComObject(ins);
}
I got it doing the following
OutlookHost().GetNamespace("mapi").CurrentUser.Address
any one know any negative side effect of this?
Sometimes this property doesn't return the SMTP address.
You can get other types of email address. E.g. X400 address.
|
|
Esteban Astudillo
Guest
|
Hi Sergey,
I'm trying to use the sample code you provided above and I can't make it to work. I'm using it in the event handler for the adxOutlookEvents.ItemSend event.
I get an "Object reference not set to an instance of an object.." exception in the call to (MAPI.SPropValue) Marshal.PtrToStructure
I'm using OL 2003 and Visual Studio 2003 on an XP SP2 machine. I have another example (also from you; thank you!) that retrieves a different MAPI property and works perfectly.
Any ideas what could be the problem? |
|
Sergey Grischenko
Add-in Express team
Posts: 7235
Joined: 2004-07-05
|
Hi Esteban.
That means the property you need doesn't exist in the current email.
In this case you get IntPtr.Zero as a return value. |
|
Esteban Astudillo
Guest
|
Eh, I knew that much.. ;)
The actual question was why do you think the current email wouldn't have that property?
Maybe I should start from scratch. I need to get the sender's email address for a new message that hasn't been sent yet. I thought that the code you included in your post was doing exactly that. Did I miss interpret it?
Do you know when your code could not retrieve the email address I'm looking for? Do you know an alternative method?
Thanks!
|
|
Sergey Grischenko
Add-in Express team
Posts: 7235
Joined: 2004-07-05
|
Hi Esteban.
I have already answered Mike regarding this issue. Please read my answer above.
.....
You should definitely set the sender account via the Accounts popup control in the new message inspector window
.....
|
|