473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

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 programmatically 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 SendEmailToContact from one of my main classes of the Add-In:

Expand|Select|Wrap|Line Numbers
  1. public void SendEmailToContact(Image img)
  2.         {
  3.             if (img == null)
  4.                 return;
  5.  
  6.             Outlook.NameSpace myNamespace = Globals.ThisAddIn.Application.GetNamespace("MAPI");
  7.             myNamespace.Logon(null, null, false, false);
  8.  
  9.             string from;
  10.             Outlook.Account fromAcct = null;
  11.             Outlook.Recipient currentUser = myNamespace.CurrentUser;
  12.             Outlook.Accounts accounts = myNamespace.Accounts;
  13.             foreach (Outlook.Account acct in accounts)
  14.             {
  15.                 if (String.Compare(acct.SmtpAddress, currentUser.Address, true) == 0)
  16.                 {
  17.                     fromAcct = acct;
  18.                     from = acct.SmtpAddress;
  19.                     break;
  20.                 }
  21.             }
  22.  
  23.             // check if the Contacts folder is active, if not make it active/selected
  24.             Outlook.MAPIFolder myContactsFolder = myNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
  25.             Outlook.Explorer activeExplorer = Globals.ThisAddIn.Application.ActiveExplorer();
  26.             if (!activeExplorer.IsFolderSelected(myContactsFolder))
  27.                 activeExplorer.SelectFolder(myContactsFolder);
  28.  
  29.             Outlook.Items contactItems = myContactsFolder.Items;
  30.  
  31.             string subject = "my map";
  32.             string body = "Attached is a map";
  33.             string to = "";
  34.             string smtpAddress = "";
  35.  
  36.             Outlook.Selection objSelection = activeExplorer.Selection;
  37.  
  38.             foreach (object cont in objSelection)
  39.             {
  40.                 to = (string)GetPropertyHelper(cont, "FirstName") + " " + (string)GetPropertyHelper(cont, "LastName");
  41.                 smtpAddress = (string)GetPropertyHelper(cont, "Email1Address"); 
  42.                 SendEmailFromAccount(img, subject, body, to, fromAcct, smtpAddress);
  43.             }
  44.         }
  45.  
  46.         private object GetPropertyHelper(object targetObject, string propertyName)
  47.         {
  48.             return targetObject.GetType().InvokeMember(propertyName,
  49.               System.Reflection.BindingFlags.Public |
  50.               System.Reflection.BindingFlags.Instance |
  51.               System.Reflection.BindingFlags.GetProperty,
  52.               null,
  53.               targetObject,
  54.               null,
  55.               System.Globalization.CultureInfo.CurrentCulture);
  56.         }
  57.  
  58.         public void SendEmailFromAccount(Image img, string subject, string body, string to, Outlook.Account fromAcct, string smtpAddress)
  59.         {
  60.             if (img == null || String.IsNullOrEmpty(smtpAddress))
  61.                 return;
  62.  
  63.             // Create a new MailItem and set the To, Subject, and Body properties.
  64.             Outlook._MailItem newMail = (Outlook._MailItem)Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem);
  65.             newMail.To = to;
  66.             newMail.Subject = subject;
  67.             newMail.Body = body;
  68.             newMail.SendUsingAccount = fromAcct;
  69.             Outlook.Inspector inspector = newMail.GetInspector;
  70.  
  71.             // Retrieve the account that has the specific SMTP address.
  72.             //Outlook.Account account = GetAccountForEmailAddress(Globals.ThisAddIn.Application, smtpAddress);
  73.  
  74.             // Use this account to send the e-mail.
  75.             //newMail.SendUsingAccount = account;
  76.  
  77.             // if 'to' is empty then use the user part of the email address
  78.             if (String.IsNullOrEmpty(to) == true)
  79.             {
  80.                 int idx = smtpAddress.IndexOf('@');
  81.                 to = smtpAddress.Substring(0, idx);
  82.             }
  83.  
  84.             string filePath = System.IO.Path.GetTempPath() + "Maps\\";
  85.             if (Directory.Exists(filePath) == false)
  86.                 Directory.CreateDirectory(filePath);
  87.  
  88.             // save image in a file
  89.             filePath += "myMap.png";
  90.             img.Save(filePath, ImageFormat.Png);
  91.  
  92.             try
  93.             {
  94.                 string name = "MYMAP.png";
  95.                 newMail.Attachments.Add(filePath, Outlook.OlAttachmentType.olByValue, filePath.Length + 10, name);
  96.             }
  97.             catch (Exception ex)
  98.             {
  99.                 Trace.WriteLine("SendEmailFromAccount: newMail.Attachments.Add: exception: " + ex.Message);
  100.             }
  101.  
  102.             ((Outlook._MailItem)newMail).Send();
  103.         }
  104.  
  105.         public static Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress)
  106.         {
  107.  
  108.             // Loop over the Accounts collection of the current Outlook session.
  109.             Outlook.Accounts accounts = application.Session.Accounts;
  110.             foreach (Outlook.Account account in accounts)
  111.             {
  112.                 // When the e-mail address matches, return the account.
  113.                 if (account.SmtpAddress == smtpAddress)
  114.                 {
  115.                     return account;
  116.                 }
  117.             }
  118.             throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress));
  119.         }
Nov 21 '10 #1
0 1491

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: VB Programmer | last post by:
I have an ASPX page where I send out emails through my mail server mail.MyDomain.com. When I send emails to MyName@MyDomain.com it sends PERFECTLY. When I try sending an email to any other address...
3
by: Justin | last post by:
While trying to send an email using the .net mail class I get this error: "The server rejected one or more recipient addresses. The server response was: 553 sorry, you have not authenticated for...
0
by: JohnB | last post by:
Hi I am trying to send an email throw Outlook Object Model and i receive the error : "File or assembly name Interop.Outlook, or one of its dependencies, was not found." I add Microsoft Outlook...
1
by: nesr235 | last post by:
I am trying to send an email using SMARTHOST. But I am running into problems. Hopefully someone will be able to help. In my web application, I have the following code ... Imports...
14
by: Rostau | last post by:
Hi all I'm writing a small app that needs to be in C++ and needs to send email from inside the program. I can do this easily in PHP, Perl and Java, but have no clue how to do it in C++. Does...
0
by: arunpanoli | last post by:
i want to send email (Using Visual Basic 6.0) into my Oulook Express Outbox With out interrepting any messages between the operation .
6
by: KatMagic | last post by:
The following code wrks fine when running from my pc (localhost); but errors when I put the asp.net code up to the production web server. Theerror I get is...
1
by: =?Utf-8?B?U3RldmUgUmFuZGFsbA==?= | last post by:
Hi, Does anyone have any code samples (or links to) for sending an email message via outlook using late binding? Many thanks.
1
by: seanhirshberg | last post by:
I have code that current sends spreadsheets by email from a user's Outlook. I need to modify the code to send the email from their group email box, so the recipient can respond to the group email....
1
by: creative1 | last post by:
When I test the application I get follwowing error: could not connect to smtp host: connection timeout error can someone please check if I have rigth settings? Where I am wrong here Is IP and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.