Selected Email Information

  • Thread starter Thread starter Barkster
  • Start date Start date
B

Barkster

I trying to build a toolbar button that collects information about the
selected email then stores the information in a database. I'm able to
extract some of the information but I would like to determine if the
email selected is in the inbox, subfolder or in sent items. I cannot
figure out how to do this nor can I figure out how to gather the
recipients into a string. Here is what I have so far, any help would
be appreicated. Also, what is storeID?

try
{
sel = activeExplorer.Selection;
if (sel.Count > 1)
{
MessageBox.Show("Please only select one email
at a time");
}
else
{
if (sel[1] is Outlook.MailItem)
{
try
{
// Generate the time stamp.
double timestamp =
ConvertToUnixTimestamp(DateTime.Now);

//need to somehow check of sent or
received
Outlook._MailItem item =
((object)sel[1] as Outlook._MailItem);

//instead of saving the file need to
pass it to the form
string from = item.SenderEmailAddress;
string subject = item.Subject;
string body = null;
DateTime received = item.ReceivedTime;

if (item.Body.Length > 250)
{
body = item.Body.Substring(0,
250);
}
else
{
body = item.Body;
}

Form1 form1 = new Form1(ds1);
if (form1.ShowDialog() ==
DialogResult.OK)
{
//lookup to see if has already
been filed;

//save the file to network
try
{
//item.SaveAs(@"c:\" + username
+ "_" + timestamp.ToString() + ".msg", Outlook.OlSaveAsType.olMSG);
}
catch (Exception ex)
{
MessageBox.Show("Sorry we were
unable to file your email message, please try again");
}
//add information to database
try
{
string s =
form1.comboBox1.SelectedValue.ToString();
MessageBox.Show("Email from: "
+ from + System.Environment.NewLine + "Subject: " + subject +
System.Environment.NewLine + "has been successfully filed on the X:
Drive");
}
catch (Exception ex)
{
MessageBox.Show("Sorry we were
unable to file your email message, please try again");
}
}
else
{
MessageBox.Show("Email not
saved.");
}

//(selecteditem as
Outlook._MailItem).SaveAs(@"c:\testmsg.msg",
Outlook.OlSaveAsType.olMSG);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("The item you have
selected is not an email message");
}

}
}
catch (Exception ex)
{
MessageBox.Show("Exception" + ex.Message,
"Exception in button click event");
}
 
The Parent property of any Outlook item returns a MAPIFolder or, in OUtlook 2007, Folder object representing the folder it is stored in. Use the Namespace.GetDefaultFolder method to return the Inbox or Sent Items folder for your comparison.

To gather recipients into a string, iterate the Recipients collection and append each Recipient.Name or Recipient.Address (or both) as you desire to the string.

StoreID is a unique identifier for a data store -- Exchange mailbox, ..pst file, Exchange public folders hierarchy -- that, together with the EntryID value for an item or folder, allows you to return that item using the Namespace.GetItemFromID or Namespace.GetFolderFromID method.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


Barkster said:
I trying to build a toolbar button that collects information about the
selected email then stores the information in a database. I'm able to
extract some of the information but I would like to determine if the
email selected is in the inbox, subfolder or in sent items. I cannot
figure out how to do this nor can I figure out how to gather the
recipients into a string. Here is what I have so far, any help would
be appreicated. Also, what is storeID?

try
{
sel = activeExplorer.Selection;
if (sel.Count > 1)
{
MessageBox.Show("Please only select one email
at a time");
}
else
{
if (sel[1] is Outlook.MailItem)
{
try
{
// Generate the time stamp.
double timestamp =
ConvertToUnixTimestamp(DateTime.Now);

//need to somehow check of sent or
received
Outlook._MailItem item =
((object)sel[1] as Outlook._MailItem);

//instead of saving the file need to
pass it to the form
string from = item.SenderEmailAddress;
string subject = item.Subject;
string body = null;
DateTime received = item.ReceivedTime;

if (item.Body.Length > 250)
{
body = item.Body.Substring(0,
250);
}
else
{
body = item.Body;
}

Form1 form1 = new Form1(ds1);
if (form1.ShowDialog() ==
DialogResult.OK)
{
//lookup to see if has already
been filed;

//save the file to network
try
{
//item.SaveAs(@"c:\" + username
+ "_" + timestamp.ToString() + ".msg", Outlook.OlSaveAsType.olMSG);
}
catch (Exception ex)
{
MessageBox.Show("Sorry we were
unable to file your email message, please try again");
}
//add information to database
try
{
string s =
form1.comboBox1.SelectedValue.ToString();
MessageBox.Show("Email from: "
+ from + System.Environment.NewLine + "Subject: " + subject +
System.Environment.NewLine + "has been successfully filed on the X:
Drive");
}
catch (Exception ex)
{
MessageBox.Show("Sorry we were
unable to file your email message, please try again");
}
}
else
{
MessageBox.Show("Email not
saved.");
}

//(selecteditem as
Outlook._MailItem).SaveAs(@"c:\testmsg.msg",
Outlook.OlSaveAsType.olMSG);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("The item you have
selected is not an email message");
}

}
}
catch (Exception ex)
{
MessageBox.Show("Exception" + ex.Message,
"Exception in button click event");
}
 
Thanks Sue, great information. Do you have an example on how to get
recipients?

The Parent property of any Outlook item returns a MAPIFolder or, in OUtlook 2007, Folder object representing the folder it is stored in. Use the Namespace.GetDefaultFolder method to return the Inbox or Sent Items folder for your comparison.

To gather recipients into a string, iterate the Recipients collection and append each Recipient.Name or Recipient.Address (or both) as you desire to the string.

StoreID is a unique identifier for a data store -- Exchange mailbox, .pst file, Exchange public folders hierarchy -- that, together with the EntryID value for an item or folder, allows you to return that item using the Namespace.GetItemFromID or Namespace.GetFolderFromID method.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54

Barkster said:
I trying to build a toolbar button that collects information about the
selected email then stores the information in a database. I'm able to
extract some of the information but I would like to determine if the
email selected is in the inbox, subfolder or in sent items. I cannot
figure out how to do this nor can I figure out how to gather the
recipients into a string. Here is what I have so far, any help would
be appreicated. Also, what is storeID?
try
{
sel = activeExplorer.Selection;
if (sel.Count > 1)
{
MessageBox.Show("Please only select one email
at a time");
}
else
{
if (sel[1] is Outlook.MailItem)
{
try
{
// Generate the time stamp.
double timestamp =
ConvertToUnixTimestamp(DateTime.Now);
//need to somehow check of sent or
received
Outlook._MailItem item =
((object)sel[1] as Outlook._MailItem);
//instead of saving the file need to
pass it to the form
string from = item.SenderEmailAddress;
string subject = item.Subject;
string body = null;
DateTime received = item.ReceivedTime;
if (item.Body.Length > 250)
{
body = item.Body.Substring(0,
250);
}
else
{
body = item.Body;
}
Form1 form1 = new Form1(ds1);
if (form1.ShowDialog() ==
DialogResult.OK)
{
//lookup to see if has already
been filed;
//save the file to network
try
{
//item.SaveAs(@"c:\" + username
+ "_" + timestamp.ToString() + ".msg", Outlook.OlSaveAsType.olMSG);
}
catch (Exception ex)
{
MessageBox.Show("Sorry we were
unable to file your email message, please try again");
}
//add information to database
try
{
string s =
form1.comboBox1.SelectedValue.ToString();
MessageBox.Show("Email from: "
+ from + System.Environment.NewLine + "Subject: " + subject +
System.Environment.NewLine + "has been successfully filed on the X:
Drive");
}
catch (Exception ex)
{
MessageBox.Show("Sorry we were
unable to file your email message, please try again");
}
}
else
{
MessageBox.Show("Email not
saved.");
}
//(selecteditem as
Outlook._MailItem).SaveAs(@"c:\testmsg.msg",
Outlook.OlSaveAsType.olMSG);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("The item you have
selected is not an email message");
}
}
}
catch (Exception ex)
{
MessageBox.Show("Exception" + ex.Message,
"Exception in button click event");
}
 
It's nothing but simple iteration and concatenation. I don't write C# code, but this is a VB.NET example:

Dim rString as String = ""
Dim r as Outlook.Recipient
For Each r in item.Recipients
rString = rString & ";" & r.Address
Next
MessageBox.Show rString

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


Barkster said:
Thanks Sue, great information. Do you have an example on how to get
recipients?

The Parent property of any Outlook item returns a MAPIFolder or, in OUtlook 2007, Folder object representing the folder it is stored in. Use the Namespace.GetDefaultFolder method to return the Inbox or Sent Items folder for your comparison.

To gather recipients into a string, iterate the Recipients collection and append each Recipient.Name or Recipient.Address (or both) as you desire to the string.

StoreID is a unique identifier for a data store -- Exchange mailbox, ..pst file, Exchange public folders hierarchy -- that, together with the EntryID value for an item or folder, allows you to return that item using the Namespace.GetItemFromID or Namespace.GetFolderFromID method.

Barkster said:
I trying to build a toolbar button that collects information about the
selected email then stores the information in a database. I'm able to
extract some of the information but I would like to determine if the
email selected is in the inbox, subfolder or in sent items. I cannot
figure out how to do this nor can I figure out how to gather the
recipients into a string. Here is what I have so far, any help would
be appreicated. Also, what is storeID?
try
{
sel = activeExplorer.Selection;
if (sel.Count > 1)
{
MessageBox.Show("Please only select one email
at a time");
}
else
{
if (sel[1] is Outlook.MailItem)
{
try
{
// Generate the time stamp.
double timestamp =
ConvertToUnixTimestamp(DateTime.Now);
//need to somehow check of sent or
received
Outlook._MailItem item =
((object)sel[1] as Outlook._MailItem);
//instead of saving the file need to
pass it to the form
string from = item.SenderEmailAddress;
string subject = item.Subject;
string body = null;
DateTime received = item.ReceivedTime;
if (item.Body.Length > 250)
{
body = item.Body.Substring(0,
250);
}
else
{
body = item.Body;
}
Form1 form1 = new Form1(ds1);
if (form1.ShowDialog() ==
DialogResult.OK)
{
//lookup to see if has already
been filed;
//save the file to network
try
{
//item.SaveAs(@"c:\" + username
+ "_" + timestamp.ToString() + ".msg", Outlook.OlSaveAsType.olMSG);
}
catch (Exception ex)
{
MessageBox.Show("Sorry we were
unable to file your email message, please try again");
}
//add information to database
try
{
string s =
form1.comboBox1.SelectedValue.ToString();
MessageBox.Show("Email from: "
+ from + System.Environment.NewLine + "Subject: " + subject +
System.Environment.NewLine + "has been successfully filed on the X:
Drive");
}
catch (Exception ex)
{
MessageBox.Show("Sorry we were
unable to file your email message, please try again");
}
}
else
{
MessageBox.Show("Email not
saved.");
}
//(selecteditem as
Outlook._MailItem).SaveAs(@"c:\testmsg.msg",
Outlook.OlSaveAsType.olMSG);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("The item you have
selected is not an email message");
}
}
}
catch (Exception ex)
{
MessageBox.Show("Exception" + ex.Message,
"Exception in button click event");
}
 
Thanks Sue, I'm not much of a programmer so this helped a bit. When I
was trying to access it I was getting object error. Thanks for all
the help!
 
Hey Sue, my outlook is connected to an exchange server and when I get
the recipients, any internal recipient is show as "/o=CompanyName/
ou="domain"/cn=recipients/cn=username;" Do you know how I can get the
actual email address. Of course it get the email address of any
external emails but it would be nice to have email of internal
emails. Any help would be appreciated. Thanks
 
That *is* an actual email address, in the X.400 format that Exchange uses. If you want the SMTP address for an Exchange sender or recipient in versions before Outlook 2007, you can use CDO or, to avoid security prompts, Redemption and the PR_EMAIL (&H39FE001E) MAPI property to obtain the SMTP address from the AddressEntry object. See http://www.outlookcode.com/d/code/getsenderaddy.htm#redemption and http://www.cdolive.com/cdo5.htm#EMailAddressOfSender for examples. In ..NET languages, Microsoft doesn't officially support CDO and Extended MAPI. Redemption should work fine, and there are examples of using Extended MAPI, e.g. http://anoriginalidea.wordpress.com...der-of-a-mailitem-from-outlook-in-vbnet-vsto/.

See http://groups.google.com/group/micr...e_frm/thread/4d4d5fece24a2a7/ad2fcbb691d5bf18 for a discussion of how to do it with Cached Exchange Mode in Outlook 2003 or later.
 
Back
Top