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: - 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));
-
}
0 1491 Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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 .
|
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...
|
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.
|
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....
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |