From my program I need to send emails through Outlook 2010 but using a certain account (Not through the default account) from my Outlook.
EXAMPLE of the scenario:
The emails must be deposited in the outbox of the account email_2.
My outlook has 3 email accounts (from an Exchange Server)
- email_1 (default)
- email_2
- email_3
I try this code through SendUsingAccount
, which searches or finds the existing email account in Outlook:
Class to find the email account:
public static Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress)
{
// Loop sobre la coleccion de cuentas de email de la actual sesion de outlook.
Outlook.Accounts accounts = application.Session.Accounts;
foreach (Outlook.Account account in accounts)
{
// Cuando encuentra la cuenta email, entonces devuelve la cuenta email.
if (account.SmtpAddress == smtpAddress) return account;
}
throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress));
}
From this code I call the class GetAccountForEmailAddress
:
Outlook.Account account = GetAccountForEmailAddress(oApp , "email_2");
oMsg.SendUsingAccount = account;
THE RARE thing:
When I try this on my development PC it works. The difference is that on my development PC (local), my Outlook has 3 email accounts, one of Hotmail , one of Gmail and another of Yahoo .
But when I try it in (network) with my Outlook with 3 email accounts but from the Exchange Server, it does not work ..... apparently it does not find the email account although it exists.
On the other hand, if I simply use SentOnBehalfOfName
, this simply defines the email that will appear as the one that sends, but the email is stored in the outbox of the default email account.
Why does not this class work?
How can I send an email through the email account that is chosen (the email must be deposited in the outbox) of Outlook, when these email accounts are in an Exchange Server?