0
votes

I am trying to send an email from the Outlook COM object but from an 'Account' that is not my main account. I have tried to search through my accounts, but it only list 1 account and it always sends only from that account. The other account I am trying to send from is a shared mailbox eg a folder. I am able to go to the mailbox and reply to a message in Outlook and it gives the correct email from, but in the program it only gives my standard from.

Microsoft.Office.Interop.Outlook.Application oApp = new 
Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItemFromTemplate("c:\\test\\CAEmail.oft", inbox);

Outlook.Accounts accounts = oApp.Session.Accounts;

foreach (Outlook.Account account in accounts)
{
   oMsg.Subject = subj;
   oMsg.HTMLBody = message;
   oMsg.To = emailTo;
   oMsg.SendUsingAccount = account;
   oMsg.Send();
}
2
You can only send from the account you are logged into. The email Interop is using the windows users environment to access outlook. The email is in a pst file in the users folder which can only be accessed by the owner. - jdweng

2 Answers

0
votes

First of all, you need to create a new mail item each time you need to send an email in Outlook:

 Microsoft.Office.Interop.Outlook.MailItem oMsg = null;
 Outlook.Accounts accounts = oApp.Session.Accounts;

 foreach (Outlook.Account account in accounts)
 {
      oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItemFromTemplate("c:\\test\\CAEmail.oft", inbox);
      oMsg.Subject = subj;
      oMsg.HTMLBody = message;
      oMsg.To = emailTo;
      oMsg.SendUsingAccount = account;
      oMsg.Send();
 }

Be aware, an account should be configured in Outlook to be able to send the item.

Also you may consider using the SentOnBehalfOfName property of the MailItem class which allows to set a string indicating the display name for the intended sender of the mail message. Note, you need to have sufficient permissions to send on behalf of another person.

0
votes

You need to create the message in the target mailbox - instead of using GetDefaultFolder, use GetSharedDefaultFolder.