0

I want to send an email using a given shared mailbox name programmatically. I don't have access to a smtp server so I can't used System.Net.Mail.

I am using Outlook = Microsoft.Office.Interop.Outlook;

How can I send from a shared mailbox email rather than the default email address?

outlook.MailItem mail application.CreateItem(outlook.OlItemType.olMailItem) as outlook.MailItem;
try
   {          

    if (mail.Subject.Contains("Highway Alert")
    {

        mail.SendUsingAccount = "sharedmailboxemail@email.com"
        mail.Send();
        System.Diagnostics.Debug.WriteLine("Email Sent ");
    }
    else
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

The MailItem.SendUsingAccount property returns or sets an Account object that represents the account under which the MailItem is to be sent. For example:

private void SendUsingAccountExample()
{
    Outlook.MailItem mail = Application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
    mail.Subject = "Our itinerary";
    mail.Attachments.Add(@"c:\travel\itinerary.doc", Outlook.OlAttachmentType.olByValue,
        Type.Missing, Type.Missing);
    Outlook.Account account = Application.Session.Accounts["Hotmail"];
    mail.SendUsingAccount = account;
    mail.Send();
}

See Send a mail item by using a Hotmail account for more information.

Please remember that another account should be configured in Outlook in that case.

The SentOnBehalfOfName property makes sense only in case of Exchange profiles/accounts. Moreover, you need to have the required permissions to send on behalf of another person. See Issue with SentOnBehalfOfName for a similar discussion.

In case if you have multiple accounts configured in the profile you can use the SendUsingAccount property which allows to an Account object that represents the account under which the MailItem is to be sent.

 Sub SendUsingAccount() 
  Dim oAccount As Outlook.account 
  For Each oAccount In Application.Session.Accounts 
   If oAccount.AccountType = olPop3 Then 
    Dim oMail As Outlook.MailItem 
    Set oMail = Application.CreateItem(olMailItem) 
    oMail.Subject = "Sent using POP3 Account" 
    oMail.Recipients.Add ("someone@example.com") 
    oMail.Recipients.ResolveAll 
    oMail.SendUsingAccount = oAccount 
    oMail.Send 
   End If 
  Next 
 End Sub 
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45