none
C#: Sending email from a specific account when multiple accounts exist in Outlook? RRS feed

  • Question

  • Somehow I accidentally deleted this message trying to edit it... sorry.
    Microsoft.Office.Interop.Outlook.Application outApp = null;
       outApp = Marshal.GetActiveObject("Outlook.Application") as Microsoft.Office.Interop.Outlook.Application;
       MailItem mailItem = (MailItem)outApp.CreateItem(OlItemType.olMailItem);
       mailItem.Subject = "Director Script Confirmation Request";
       mailItem.To = person;
       mailItem.Body = message;
       //mailItem.Display(false);
       mailItem.Send();



    How do I send email from a specific account when multiple accounts exist in Outlook?

    Below is the code that I have. I think I'm supposed to use the AddressEntry or AddressEntries to designate where emails are sent from. However, at the moment, I haven't found an example.

    SV

    • Moved by Kristin Xie Tuesday, January 6, 2015 8:37 AM move to appropriate forum
    Monday, January 5, 2015 2:46 PM

Answers

  • Hello SV,

    The MailItem class provides the SendUsingAccount property which allows to set an Account object that represents the account under which the MailItem is to be sent. Here is what MSDN states:

    The SendUsingAccount property can be used to specify the account that should be used to send the MailItem when the Send method is called.

    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 

    Tuesday, January 6, 2015 8:58 AM
  • It doesn't matter what programming language you use. The OOM is the same.

    See How to: Send an E-mail Given the SMTP Address of an Account (Outlook) for a sample code in C#.


    Tuesday, January 6, 2015 2:33 PM

All replies

  • Hi SV,

    Like your title mentioned, your case related to Outlook, I will move your thread to Outlook for Developers forum for better support.   

    Have a nice day!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, January 6, 2015 8:36 AM
  • Hello SV,

    The MailItem class provides the SendUsingAccount property which allows to set an Account object that represents the account under which the MailItem is to be sent. Here is what MSDN states:

    The SendUsingAccount property can be used to specify the account that should be used to send the MailItem when the Send method is called.

    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 

    Tuesday, January 6, 2015 8:58 AM
  • I'm working in C# and that method is not an option.  Is there a C# version? I think the solution may surround the use of the AddressEntry or AddressEntries methods.

    SV

    Tuesday, January 6, 2015 2:29 PM
  • It doesn't matter what programming language you use. The OOM is the same.

    See How to: Send an E-mail Given the SMTP Address of an Account (Outlook) for a sample code in C#.


    Tuesday, January 6, 2015 2:33 PM
  • Yes, you are correct. I was looking at the SMTPClient and not the Outlook. Thanks for the assistance. 



    • Edited by svMSDN Wednesday, January 7, 2015 12:38 PM
    Wednesday, January 7, 2015 12:13 PM
  • I don't see the same SMTP address. They are different. Take a look at the numbers.
    Wednesday, January 7, 2015 12:36 PM