Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当您正在模拟并且没有邮箱时,如何发送带有附件的电子邮件_C#_Email_Exchange Server_Exchangewebservices_Impersonation - Fatal编程技术网

C# 当您正在模拟并且没有邮箱时,如何发送带有附件的电子邮件

C# 当您正在模拟并且没有邮箱时,如何发送带有附件的电子邮件,c#,email,exchange-server,exchangewebservices,impersonation,C#,Email,Exchange Server,Exchangewebservices,Impersonation,我有一个服务在机器上作为计划作业运行。它在没有自己邮箱的服务帐户下运行。我们希望它从团队的共享收件箱发送电子邮件。 我试着在这里使用模拟 var service = new ExchangeService { TraceEnabled = true, ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, Resources.EmailUsername) }; service.Autod

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

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


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

经过多次搜索,我终于来到这里:
这表明:

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();