【问题标题】:Sending email from the specified Outlook Account从指定的 Outlook 帐户发送电子邮件
【发布时间】:2015-01-25 13:25:01
【问题描述】:

我正在使用 Outlook2013,它有许多来自 Exchange 和 pop 服务器的邮箱。(Rob@mydomain.com[默认交换]、rob@somethingdifferent.com[POP]、support@mydomain.com[exchange])

我正在尝试使用 Outlook 自动化通过 support@mydomain.com 帐户发送电子邮件。

我遇到的问题是下面的代码在支持发件箱中创建了一个邮件项目,但发件人字段是 rob@mydomain.com 而不是 support@mydomain.com。这会阻止它被发送。

我想将发件人地址更改为 support@mydomain.com。我认为通过设置 Sendusingaccount 属性可以做到这一点。

非常感谢任何帮助。

    public static string Send_Email_Outlook(string _recipient, string _message, string _subject, string _cc, string _bcc, string accountname)
    {
        try
        {

            Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();

            // Get the NameSpace and Logon information.
            Microsoft.Office.Interop.Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

            // Log on by using a dialog box to choose the profile.
            oNS.Logon(Missing.Value, Missing.Value, true, true);

            // Create a new mail item.
            Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

            // Set the subject.
            oMsg.Subject = _subject;

            // Set HTMLBody.
            oMsg.HTMLBody = _message;

            oMsg.To = _recipient;
            oMsg.CC = _cc;
            oMsg.BCC = _bcc;


            #region Send via another account

            if (accountname.Trim().Length != 0)
            {
                Microsoft.Office.Interop.Outlook.Accounts accounts = oMsg.Session.Accounts;
                for (int i = 1; i <= accounts.Count; i++)
                {
                    string accountfound = accounts[i].DisplayName.ToLower();
                    if (accountname.ToLower() == accountfound)
                    {
                        oMsg.SendUsingAccount = accounts[i]; // Send using support account
                        Microsoft.Office.Interop.Outlook.Recipient recipient = oMsg.Session.CreateRecipient(accountfound);
                        oMsg.Sender = recipient.AddressEntry;
                        break;
                    }
                }
            }
            #endregion

            // Send.
            (oMsg as Microsoft.Office.Interop.Outlook._MailItem).Send();

            // Log off.
            oNS.Logoff();

            // Clean up.
            //oRecip = null;
            //oRecips = null;
            oMsg = null;
            oNS = null;
            oApp = null;


        }

     // Return Error Message
        catch (Exception e)
        {
            return e.Message;
        }

        // Default return value.
        return "";

    }

【问题讨论】:

  • 尝试使用语法:oMsg.SendUsingAccount = oApp.Session.Accounts.Item(idx)。另一种可能的解决方案是创建一个存储为草稿的模板电子邮件,该电子邮件使用感兴趣的 Outlook 帐户:这将需要大量重新编码。亲切的问候,
  • 我已经修好了。我有分配给我的邮箱作为交换,它总是让它出现。一旦我从我的个人资料中取消分配邮箱并重新添加,问题就解决了。感谢您的帮助!
  • 当然,不客气!祝你的项目好运。 Rgds,

标签: c# email outlook office-interop mailitem


【解决方案1】:

是的,您可以使用 SendUsingAccount 属性来设置发送商品所需的正确帐户。

 public static Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress) 
 { 

     // Loop over the Accounts collection of the current Outlook session. 
     Outlook.Accounts accounts = application.Session.Accounts; 
     foreach (Outlook.Account account in accounts) 
     { 
         // When the e-mail address matches, return the account. 
         if (account.SmtpAddress == smtpAddress) 
         { 
            return account; 
         } 
     } 
     throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress)); 
 } 

public static string Send_Email_Outlook(string _recipient, string _message, string _subject, string _cc, string _bcc, string accountname)
{
    try
    {

        Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();

        // Get the NameSpace and Logon information.
        Microsoft.Office.Interop.Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

        // Log on by using a dialog box to choose the profile.
        oNS.Logon(Missing.Value, Missing.Value, true, true);

        // Create a new mail item.
        Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

        // Set the subject.
        oMsg.Subject = _subject;

        // Set HTMLBody.
        oMsg.HTMLBody = _message;

        oMsg.To = _recipient;
        oMsg.CC = _cc;
        oMsg.BCC = _bcc;


        #region Send via another account

         // Retrieve the account that has the specific SMTP address. 
        Outlook.Account account = GetAccountForEmailAddress(oApp , "support@mydomain.com"); 
        // Use this account to send the e-mail. 
        oMsg.SendUsingAccount = account; 

        // Send.
        (oMsg as Microsoft.Office.Interop.Outlook._MailItem).Send();

        // Log off.
        oNS.Logoff();

        // Clean up.
        //oRecip = null;
        //oRecips = null;
        oMsg = null;
        oNS = null;
        oApp = null;


    }

 // Return Error Message
    catch (Exception e)
    {
        return e.Message;
    }

    // Default return value.
    return "";

}

【讨论】:

    猜你喜欢
    • 2015-07-26
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    相关资源
    最近更新 更多