代码之家  ›  专栏  ›  技术社区  ›  Jean-Bernard Pellerin

如何在您模拟且没有邮箱的情况下发送带有附件的电子邮件

  •  0
  • Jean-Bernard Pellerin  · 技术社区  · 6 年前

    我有一个服务在机器上作为计划作业运行。它在一个没有自己邮箱的服务帐户下运行。我们希望它能从团队的共享收件箱发送电子邮件。
    我试着在这里使用模拟。

    var service = new ExchangeService
    {
        TraceEnabled = true,
        ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, Resources.EmailUsername)
    };
    
    service.AutodiscoverUrl(Resources.EmailUsername, RedirectionUrlValidationCallback);
    
    var email = new EmailMessage(service);
    
    if (!string.IsNullOrWhiteSpace(recipients))
    {
        foreach (var recipient in recipients.Split(','))
        {
            email.ToRecipients.Add(recipient.Trim());
        }
    }
    
    email.Subject = subject;
    email.Body = new MessageBody(BodyType.HTML, body);
    
    if (attachmentName != null && attachment != null)
    {
        email.Attachments.AddFileAttachment(attachmentName, attachment);                        
    }
    
    email.Send();
    

    它失败了,我得到一个例外,说:

    当以没有邮箱的帐户发出请求时 必须为任何可分辨邮件指定邮箱主smtp地址 文件夹ID。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Jean-Bernard Pellerin    6 年前

    这个 TraceEnabled 这一部分让我注意到了 MessageDisposition=SaveOnly 是在xml中设置的。我查了一下,觉得我只想要SendOnly。

    经过多次搜索,我终于来到这里: https://github.com/OfficeDev/ews-managed-api/blob/master/Core/ServiceObjects/Items/EmailMessage.cs
    这表明:

    public void Send()
    {
        this.InternalSend(null, MessageDisposition.SendOnly);
    }
    

    看起来这正是我想要的。。。但是:

    private void InternalSend(FolderId parentFolderId, MessageDisposition messageDisposition)
    {
        this.ThrowIfThisIsAttachment();
    
        if (this.IsNew)
        {
            if ((this.Attachments.Count == 0) || (messageDisposition == MessageDisposition.SaveOnly))
            {
                this.InternalCreate(
                    parentFolderId,
                    messageDisposition,
                    null);
            }
            else
            {
                // If the message has attachments, save as a draft (and add attachments) before sending.
                this.InternalCreate(
                    null,                           // null means use the Drafts folder in the mailbox of the authenticated user.
                    MessageDisposition.SaveOnly,
                    null);
    
                this.Service.SendItem(this, parentFolderId);
            }
        }
        ...
    

    这两条评论是最有启发性的部分。附件将保存到运行流程的用户的草稿文件夹中。
    为了避免这种情况,我们必须在调用Send时保存消息。让我们确保它保存在我们知道存在的邮箱中。因此,我们删除模拟,添加一个步骤来保存它,并修改From字段。然后我们可以安全地发送消息,它将从草稿文件夹中删除自己。

    var service = new ExchangeService
    {
        TraceEnabled = true
    };
    
    service.AutodiscoverUrl(Resources.EmailUsername, RedirectionUrlValidationCallback);
    
    var email = new EmailMessage(service);
    
    if (!string.IsNullOrWhiteSpace(recipients))
    {
        foreach (var recipient in recipients.Split(','))
        {
            email.ToRecipients.Add(recipient.Trim());
        }
    }
    
    email.Subject = subject;
    email.Body = new MessageBody(BodyType.HTML, body);
    
    if (attachmentName != null && attachment != null)
    {
        email.Attachments.AddFileAttachment(attachmentName, attachment);                        
    }
    
    var folderId = new FolderId(WellKnownFolderName.SentItems, Resources.EmailUsername);
    
    email.Save(folderId);
    email.From = Resources.EmailUsername;
    email.Send();