How to send a mail using Microsoft.Office.Interop.Outlook.MailItem by specifying the From Address

后端 未结 2 1760
一生所求
一生所求 2020-12-02 02:03

I\'m using Interop for sending e-mails via Outlook, but I am not able to specify the From e-mail address.

I want to send mails to multiple users originating from the

相关标签:
2条回答
  • 2020-12-02 02:40

    You are using outlook to send the mail. Since outlook must be configured to use the from address of your mail, you cannot provide the from address directly. However, you can select an account available on outlook. For example :

    using Outlook = Microsoft.Office.Interop.Outlook;
    
    Outlook.Accounts accounts = olkApp1.Session.Accounts;
    foreach (Outlook.Account account in accounts)
    {
        // When the e-mail address matches, send the mail.
        if (account.SmtpAddress == "from@mail.com")
        {
                olkMail1.SendUsingAccount = account;
                ((Outlook._MailItem)olkMail1).Send();
                break;
        }
    }
    
    0 讨论(0)
  • 2020-12-02 02:48

    The Send method sends the mail using the default account. To specify a different account to send the mail, set the SendUsingAccount property to the desired Account prior to calling the Send method.

    var application = new Application();
    var mail = (_MailItem) application.CreateItem(OlItemType.olMailItem);
    mail.To = "anonymous@somedomain.com";
    ....
    Outlook.Account account = Application.Session.Accounts["MyOtherAccount"];
    mailItem.SendUsingAccount = account;
    mail.Send();
    

    More information can be found here:

    http://msdn.microsoft.com/en-us/library/ff184652.aspx

    0 讨论(0)
提交回复
热议问题