none
Recipients Address returning null RRS feed

  • Question

  • I have created an Outlook 2010 add-in with Visual Studio 2010 and the Redemption dll. Most of the time the app works fine but it looks like when a new email message comes up as a result of clicking on a mailto:emailaddress link on a web page that the recipient object exists but the recipient.address does not. I get an "Object reference not set to an instance of an object". Does Outlook treat these emails differently. If I type in the email address, i have not problems. Below is the code:

     

                    foreach (SafeRecipient recipient in mailItem.Recipients)
                    {
     
                        if (recipient.Address.Contains("@"))  //code fails here
                        {
                            recipients.Append(recipient.Address);
                            recipients.Append(",");
                        }
                    }
    Thanks for any information on this problem.

    Royal

    Royal
    Wednesday, July 20, 2011 12:40 PM

Answers

  • Using the below code fixed the problem:

     

     
     for (int i = 1; i <= item.Recipients.Count; i++)  {                           recipients.Append(item.Recipients[i].AddressEntry.Address);        recipients.Append(",");  }

    Thank for you input

    Royal
    • Marked as answer by royalhale Wednesday, July 20, 2011 5:13 PM
    Wednesday, July 20, 2011 4:45 PM

All replies

  • See if saving the Simple MAPI created email item (created by mailto:) before you assign it to a Redemption item helps.
     
    Can you retrieve the Recipient.AddressEntry object from the Outlook item? If so what about the Address property of the AddressEntry, is it populated?

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "royalhale" <=?utf-8?B?cm95YWxoYWxl?=> wrote in message news:017ae630-d162-4ec3-a634-1f3d5b7ad6c4...

    I have created an Outlook 2010 add-in with Visual Studio 2010 and the Redemption dll. Most of the time the app works fine but it looks like when a new email message comes up as a result of clicking on a mailto:emailaddress link on a web page that the recipient object exists but the recipient.address does not. I get an "Object reference not set to an instance of an object". Does Outlook treat these emails differently. If I type in the email address, i have not problems. Below is the code:

     

                    foreach (SafeRecipient recipient in mailItem.Recipients)
                    {
     
                        if (recipient.Address.Contains("@")) //code fails here
                        {
                            recipients.Append(recipient.Address);
                            recipients.Append(",");
                        }
                    }
    Thanks for any information on this problem.

    Royal

    Royal

    Ken Slovak MVP - Outlook
    Wednesday, July 20, 2011 1:27 PM
  • AddressEntry.address was also null. I think I am already saving the mail item below is all the code in the function

      Microsoft.Office.Interop.Outlook.Application oOutlook = new Microsoft.Office.Interop.Outlook.Application();
                    string sentBy = string.Empty;
     
                    item.Save();               
                    Microsoft.Office.Interop.Outlook.AddressEntry addrEntry = oOutlook.Session.CurrentUser.AddressEntry;
                    tracking += " Before SafemailItem  \r\n";
                    SafeMailItem mailItem = new SafeMailItem();
                    mailItem.Item = item;
                    string body = mailItem.Body;
                    tracking += " Before Safe Current User \r\n";
     
                    SafeCurrentUser cu = new SafeCurrentUser();
                    sentBy = cu.SMTPAddress;
                    string subject = item.Subject;
                    StringBuilder recipients = new StringBuilder();
                    tracking += " Before For Each \r\n";
     
                    foreach (SafeRecipient recipient in mailItem.Recipients)
                    {
                        if (recipient.Address.Contains("@"))
                        {
                            recipients.Append(recipient.Address);
                            recipients.Append(",");
                        }
     
                    }
                    if (recipients.Length > 1)
                    {
                        recipients.Remove(recipients.Length - 1, 1);
                    }
                    tracking += " Before Send to webservice \r\n";
                    crm.jbpub.recordemail.RecordEmail saveEmail = new crm.jbpub.recordemail.RecordEmail();
                    string invalidEmails = saveEmail.RecordEmailData(recipients.ToString(), sentBy, subject, body);
                    if (invalidEmails.Length > 0)
                    {
     
                        MessageBox.Show("Not all email addresses are in Lighthouse.\r\n" + invalidEmails);
                    }
                    tracking += " After sending to webservice \r\n";
                    IsolatedStorageFileStream stream = getIsolatedStorage(true"Errors.txt");
                    System.IO.StreamWriter writer1 = new StreamWriter(stream);
                    writer1.WriteLine(tracking);
                    writer1.Close();
                }
                catch (System.Exception ex)
                {
                    IsolatedStorageFileStream stream = getIsolatedStorage(true"Errors.txt");
                    if (stream != null)
                    {
                        System.IO.StreamWriter writer = new StreamWriter(stream);
                        writer.WriteLine(ex.Message + "\r " + tracking);
                        writer.Close();
     
                    }
                }
                finally
                {
                    (item as _MailItem).Send();
                    GC.Collect();
                }

     


    Royal
    Wednesday, July 20, 2011 3:11 PM
  • Are there any recipients on the item at all? What is item.Recipients.Count? I'd say that it would be best to get things working with Outlook objects before involving Redemption objects in the mix.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "royalhale" <=?utf-8?B?cm95YWxoYWxl?=> wrote in message news:513159fd-9402-4212-944a-fc10bdcbe9d8...

    AddressEntry.address was also null. I think I am already saving the mail item below is all the code in the function

      Microsoft.Office.Interop.Outlook.Application oOutlook = new Microsoft.Office.Interop.Outlook.Application();
                    string sentBy = string.Empty;
     
                    item.Save();               
                    Microsoft.Office.Interop.Outlook.AddressEntry addrEntry = oOutlook.Session.CurrentUser.AddressEntry;
                    tracking += " Before SafemailItem  \r\n";
                    SafeMailItem mailItem = new SafeMailItem();
                    mailItem.Item = item;
                    string body = mailItem.Body;
                    tracking += " Before Safe Current User \r\n";
     
                    SafeCurrentUser cu = new SafeCurrentUser();
                    sentBy = cu.SMTPAddress;
                    string subject = item.Subject;
                    StringBuilder recipients = new StringBuilder();
                    tracking += " Before For Each \r\n";
     
                    foreach (SafeRecipient recipient in mailItem.Recipients)
                    {
                        if (recipient.Address.Contains("@"))
                        {
                            recipients.Append(recipient.Address);
                            recipients.Append(",");
                        }
     
                    }
                    if (recipients.Length > 1)
                    {
                        recipients.Remove(recipients.Length - 1, 1);
                    }
                    tracking += " Before Send to webservice \r\n";
                    crm.jbpub.recordemail.RecordEmail saveEmail = new crm.jbpub.recordemail.RecordEmail();
                    string invalidEmails = saveEmail.RecordEmailData(recipients.ToString(), sentBy, subject, body);
                    if (invalidEmails.Length > 0)
                    {
     
                        MessageBox.Show("Not all email addresses are in Lighthouse.\r\n" + invalidEmails);
                    }
                    tracking += " After sending to webservice \r\n";
                    IsolatedStorageFileStream stream = getIsolatedStorage(true"Errors.txt");
                    System.IO.StreamWriter writer1 = new StreamWriter(stream);
                    writer1.WriteLine(tracking);
                    writer1.Close();
                }
                catch (System.Exception ex)
                {
                    IsolatedStorageFileStream stream = getIsolatedStorage(true"Errors.txt");
                    if (stream != null)
                    {
                        System.IO.StreamWriter writer = new StreamWriter(stream);
                        writer.WriteLine(ex.Message + "\r " + tracking);
                        writer.Close();
     
                    }
                }
                finally
                {
                    (item as _MailItem).Send();
                    GC.Collect();
                }

     


    Royal

    Ken Slovak MVP - Outlook
    Wednesday, July 20, 2011 3:20 PM
  • Using the below code fixed the problem:

     

     
     for (int i = 1; i <= item.Recipients.Count; i++)  {                           recipients.Append(item.Recipients[i].AddressEntry.Address);        recipients.Append(",");  }

    Thank for you input

    Royal
    • Marked as answer by royalhale Wednesday, July 20, 2011 5:13 PM
    Wednesday, July 20, 2011 4:45 PM