【问题标题】:Issue with To field in Outlook 2016Outlook 2016 中的收件人字段问题
【发布时间】:2018-11-21 17:20:26
【问题描述】:

我正在尝试创建一封电子邮件并将其保存到当前工作正常的用户草稿文件夹中。但是,该电子邮件有一个巨大的“收件人”字段,当用户从他们的草稿文件夹发送电子邮件时,该电子邮件无法正确发送。有任何想法吗?这个问题似乎只出现在 Outlook 2016 中。这是我目前的保存方法。

public void Save(string saveToEmailAddress, string recipient, string subject, IEnumerable<MailAttachment> attachments)
        {
            try
            {
                // We need to impersonate the User whose account we are adding a Draft email to
                _exchangeService.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, saveToEmailAddress);

                var email = new EmailMessage(_exchangeService)
                {
                    Subject = subject
                };
                email.ToRecipients.Add(recipient);

                foreach (var attachment in attachments)
                {
                    email.Attachments.AddFileAttachment(attachment.FileName, attachment.Content);
                }
                email.Save();
            }
            finally
            {
                // Stop impersonating the User account
                // If this doesn't happen, any email sent from this Service
                // will appear to be coming from the Impersonated account (not the Intranet)
                _exchangeService.ImpersonatedUserId = null;
            }
        }

【问题讨论】:

  • 看起来你的recipient 参数可能有一些你需要去掉的空格?可能应该指定您将其保存到代码中的草稿文件夹,以便更明确地说明它在做什么。 email.Save(WellKnownFolderName.Drafts);
  • 是的,你是对的。在某些情况下,我添加了导致问题的空白收件人。
  • 亲爱的,很高兴你把它整理好了 :-)

标签: c# .net email outlook exchangewebservices


【解决方案1】:

您可以参考以下代码:

Microsoft.Office.Interop.Outlook.Application app;
                try
                {
                    app = (Microsoft.Office.Interop.Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
                }
                catch
                {
                    app = new Microsoft.Office.Interop.Outlook.Application();
                }

                if (app == null)
                {
                    return;
                }
                string stringHtmlBodyfromFile = File.ReadAllText(@"codeFileUrl");
                Microsoft.Office.Interop.Outlook.MailItem mailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem) as
                                                                     Microsoft.Office.Interop.Outlook.MailItem;
                mailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
                mailItem.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh;
                mailItem.Subject = “subject”;
               mailItem.Recipients.Add("");
                mailItem.HTMLBody = stringHtmlBodyfromFile;
                mailItem.CC = cc;

                mailItem.Attachments.Add(@"AttachmentUrl");
                mailItem.Save();
                        }
            catch (Exception eX)
            {
               // XtraMessageBox.Show(eX.Message + "\n" + eX.StackTrace + "\n" + eX.Source + "\n" + eX.InnerException);
            }

【讨论】:

    猜你喜欢
    • 2016-05-28
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多