在Exchange帐户上使用Outlook发送邮件

时间:2017-06-14 16:39:05

标签: c# outlook

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

我被锁定在a@domain.com的Outlook帐户中,并获得了Exchange服务器上发送来自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;
        }
    }

1 个答案:

答案 0 :(得分:0)

您必须向要从中发送电子邮件的邮箱授予发送邮箱“代理发送”权限的所有者。代表权限发送会有效但会代表B"作为发件人。