gpt4 book ai didi

c# - ExchangeService with AutodiscoverUrl 从错误的电子邮件地址发送

转载 作者:行者123 更新时间:2023-11-30 16:50:42 25 4
gpt4 key购买 nike

我正在使用此代码在 winforms 应用程序中通过 Outlook 发送电子邮件。

_exchangeService = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
_exchangeService.AutodiscoverUrl(_sender);
// ...
var email = new EmailMessage(_exchangeService)
{
Subject = mailMessage.Subject,
Body = mailMessage.Body
};
email.ToRecipients.AddRange(mailMessage.To.Select(x => x.Address));
email.SendAndSaveCopy();

我在 _exchangeService.AutodiscoverUrl(_sender); 上设置了一个断点,我可以看到发件人是 customerservice@mycompany.com。但是,电子邮件是“从”登录到 winforms 应用程序的实际用户发送的。例如 person@mycompany.com

我们已尝试将 Active Directory 设置为“发送为”和“代表发送”,以允许此用户作为 customerservice@mycompany.com 发送,但没有帮助。我们还审查了这封电子邮件的 Exchange Server 日志记录,它显然是“发自”person@mycompany.com 发送的。

在此处的代码示例中,https://msdn.microsoft.com/en-us/library/dd634273 ,他们使用 NetworkCredential。我需要这样做吗?

为什么发件人(Email send FROM)不对?

最佳答案

_exchangeService = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
_exchangeService.AutodiscoverUrl(_sender);
using email = new EmailMessage(_exchangeService)
{
From = new MailAddress("customerservice@mycompany.com"),
Subject = subject,
Body = body
})
{
//System.Net.Mail.Attachment attachment;
//attachment = new System.Net.Mail.Attachment("your attachment file");
//mail.Attachments.Add(attachment);
message.To.Add(address);//Jess@Jess.com for example
_exchangeService.Send(message);
};

如果你想要一个发送电子邮件的好方法,我也为你创建了一个静态方法

public static void SendEmail(string subject, string body)
{
using (var client = new SmtpClient(utilities.EmailHost, 25))
using (var message = new MailMessage()
{
From = new MailAddress(utilities.FromEmail),
Subject = subject,
Body = body
})
{
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("your attachment file");
mail.Attachments.Add(attachment);
message.To.Add(address);//Jess@Jess.com for example
client.Send(message);
};
}

关于c# - ExchangeService with AutodiscoverUrl 从错误的电子邮件地址发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34679586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com