【问题标题】:Send Mail with Outlook on Exchange Account在 Exchange 帐户上使用 Outlook 发送邮件
【发布时间】:2017-06-14 16:39:04
【问题描述】:

我想用 C# 和 Microsoft.Office.Interop.Outlook 库发送电子邮件。

我的 Outlook 帐户被锁定在 a@domain.com 上,并在交换服务器上获得了从 b@domain.comc@domain.com 发送邮件的权限。

我找不到任何方法可以使用BC 发送邮件。

我的用户帐户拥有a@domain.com 的邮件,一切正常。

现在我找不到办法获取b@domain.com 的帐户并以B 的名义发送邮件。

我的问题:

如何访问其他帐户?

这是我的代码:

using Outlook = Microsoft.Office.Interop.Outlook;
        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;
        if (_IsDebug)
            Console.WriteLine($"Anzahl Accounts: {accounts.Count}");
        foreach (Outlook.Account account in accounts)
        {
            // When the e-mail address matches, return the account. 
            if (_IsDebug)
                Console.WriteLine($"Account: {account.SmtpAddress}");
            if (String.Compare(account.SmtpAddress, smtpAddress, true) == 0)
            {
                return account;
            }
        }
        throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress));
    }
        static void SendEMail(string emailadress)
    {
        try
        {
            var outlookApplication = new Outlook.Application();
            var outlookMailItem = (Outlook.MailItem)outlookApplication.CreateItem(Outlook.OlItemType.olMailItem);

            outlookMailItem.SendUsingAccount = GetAccountForEmailAddress(outlookApplication, ConfigurationManager.AppSettings[SENDER]);


            if (_IsDebug)
                Console.WriteLine($"Absender: {outlookMailItem?.SendUsingAccount?.SmtpAddress}");

            outlookMailItem.HTMLBody = ConfigurationManager.AppSettings[BODY];

            if (_IsDebug)
                Console.WriteLine($"Body: {outlookMailItem?.HTMLBody}");

            var file = GetPDFFile();
            if (_IsDebug)
                Console.WriteLine($"File: {file?.Name}");

            if (file == null)
            {
                Console.WriteLine("Keine Datei gefunden!");
                return;
            }

            string attachementDisplayName = file.Name;

            int attachementPosition = outlookMailItem.HTMLBody.Length + 1;
            int attachementType = (int)Outlook.OlAttachmentType.olByValue;

            if (_IsDebug)
                Console.WriteLine($"Dateianhang: {file.FullName}");

            Outlook.Attachment outlookAttachement = outlookMailItem.Attachments.Add(file.FullName, attachementType, attachementPosition, attachementDisplayName);

            outlookMailItem.Subject = ConfigurationManager.AppSettings[SUBJECT];

            Outlook.Recipients outlookRecipients = outlookMailItem.Recipients;
            Outlook.Recipient outlookRecipient = outlookRecipients.Add(emailadress);

            outlookRecipient.Resolve();

            outlookMailItem.Send();

            outlookRecipient = null;
            outlookRecipients = null;
            outlookMailItem = null;
            outlookApplication = null;

            if (_IsDebug)
                Console.ReadLine();
        }
        catch (Exception)
        {
            throw;
        }
    }

【问题讨论】:

    标签: c# outlook


    【解决方案1】:

    您必须向发送邮箱的所有者授予对您要从中发送电子邮件的邮箱的“代理发送”权限。代表权限发送将起作用,但会说“A 代表 B”作为发件人。

    【讨论】:

    猜你喜欢
    • 2014-02-06
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    相关资源
    最近更新 更多