Problem with sending email programmatically from Outlook 2007 Add-In

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Leon H

    Problem with sending email programmatically from Outlook 2007 Add-In

    I probably read all possible articles on the discussion forums about how to send email programmaticall y from Outlook, so below is my code. Basically, it executes just fine without exceptions or errors, and it creates email item and the attachment that I can see in the Outbox. But that's about it - it is not being sent, and it also does not respond to user "Send All" button nor to "Send" button when I open this emai. On the other hand when I create email item as a user Outlook sends it without a problem. Could anybody shed a light on what's wrong? I call SendEmailToCont act from one of my main classes of the Add-In:

    Code:
    public void SendEmailToContact(Image img)
            {
                if (img == null)
                    return;
    
                Outlook.NameSpace myNamespace = Globals.ThisAddIn.Application.GetNamespace("MAPI");
                myNamespace.Logon(null, null, false, false);
    
                string from;
                Outlook.Account fromAcct = null;
                Outlook.Recipient currentUser = myNamespace.CurrentUser;
                Outlook.Accounts accounts = myNamespace.Accounts;
                foreach (Outlook.Account acct in accounts)
                {
                    if (String.Compare(acct.SmtpAddress, currentUser.Address, true) == 0)
                    {
                        fromAcct = acct;
                        from = acct.SmtpAddress;
                        break;
                    }
                }
    
                // check if the Contacts folder is active, if not make it active/selected
                Outlook.MAPIFolder myContactsFolder = myNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
                Outlook.Explorer activeExplorer = Globals.ThisAddIn.Application.ActiveExplorer();
                if (!activeExplorer.IsFolderSelected(myContactsFolder))
                    activeExplorer.SelectFolder(myContactsFolder);
    
                Outlook.Items contactItems = myContactsFolder.Items;
    
                string subject = "my map";
                string body = "Attached is a map";
                string to = "";
                string smtpAddress = "";
    
                Outlook.Selection objSelection = activeExplorer.Selection;
    
                foreach (object cont in objSelection)
                {
                    to = (string)GetPropertyHelper(cont, "FirstName") + " " + (string)GetPropertyHelper(cont, "LastName");
                    smtpAddress = (string)GetPropertyHelper(cont, "Email1Address"); 
                    SendEmailFromAccount(img, subject, body, to, fromAcct, smtpAddress);
                }
            }
    
            private object GetPropertyHelper(object targetObject, string propertyName)
            {
                return targetObject.GetType().InvokeMember(propertyName,
                  System.Reflection.BindingFlags.Public |
                  System.Reflection.BindingFlags.Instance |
                  System.Reflection.BindingFlags.GetProperty,
                  null,
                  targetObject,
                  null,
                  System.Globalization.CultureInfo.CurrentCulture);
            }
    
            public void SendEmailFromAccount(Image img, string subject, string body, string to, Outlook.Account fromAcct, string smtpAddress)
            {
                if (img == null || String.IsNullOrEmpty(smtpAddress))
                    return;
    
                // Create a new MailItem and set the To, Subject, and Body properties.
                Outlook._MailItem newMail = (Outlook._MailItem)Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem);
                newMail.To = to;
                newMail.Subject = subject;
                newMail.Body = body;
                newMail.SendUsingAccount = fromAcct;
                Outlook.Inspector inspector = newMail.GetInspector;
    
                // Retrieve the account that has the specific SMTP address.
                //Outlook.Account account = GetAccountForEmailAddress(Globals.ThisAddIn.Application, smtpAddress);
                
                // Use this account to send the e-mail.
                //newMail.SendUsingAccount = account;
    
                // if 'to' is empty then use the user part of the email address
                if (String.IsNullOrEmpty(to) == true)
                {
                    int idx = smtpAddress.IndexOf('@');
                    to = smtpAddress.Substring(0, idx);
                }
    
                string filePath = System.IO.Path.GetTempPath() + "Maps\\";
                if (Directory.Exists(filePath) == false)
                    Directory.CreateDirectory(filePath);
    
                // save image in a file
                filePath += "myMap.png";
                img.Save(filePath, ImageFormat.Png);
    
                try
                {
                    string name = "MYMAP.png";
                    newMail.Attachments.Add(filePath, Outlook.OlAttachmentType.olByValue, filePath.Length + 10, name);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("SendEmailFromAccount: newMail.Attachments.Add: exception: " + ex.Message);
                }
    
                ((Outlook._MailItem)newMail).Send();
            }
    
            public static Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress)
            {
    
                // Loop over the Accounts collection of the current Outlook session.
                Outlook.Accounts accounts = application.Session.Accounts;
                foreach (Outlook.Account account in accounts)
                {
                    // When the e-mail address matches, return the account.
                    if (account.SmtpAddress == smtpAddress)
                    {
                        return account;
                    }
                }
                throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress));
            }
    Last edited by Dormilich; Nov 22 '10, 06:42 AM. Reason: please use [CODE] [/CODE] tags when posting code
Working...