EWS 托管 API:如何设置电子邮件的发件人?

发布于 2024-12-27 22:07:21 字数 446 浏览 0 评论 0原文

我正在使用 EWS 托管 API 发送电子邮件。帐户“account@domain.com”具有“发送为”权限,可以使用“sender@domain.com”邮箱发送邮件(从 Outlook 可以正常工作)。

但我尝试从代码中 - 这是行不通的,在邮件中,我在“发件人”“account@domain.com”字段中读到。

....
EmailMessage message = new EmailMessage(service);
message.Body = txtMessage;
message.Subject = txtSubject;
message.From = txtFrom;
....
message.SendAndSaveCopy();

如何代表其他用户发送邮件? :)

I'm using EWS Managed API to sending email. Account "account@domain.com" have permissions "Send as" to use "sender@domain.com" mailbox to send messages (from Outlook, it's work fine).

But I try from code - it's not work, in mail i'm read in the field "From" "account@domain.com".

....
EmailMessage message = new EmailMessage(service);
message.Body = txtMessage;
message.Subject = txtSubject;
message.From = txtFrom;
....
message.SendAndSaveCopy();

How to make sending mail on behalf of another user? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫之都 2025-01-03 22:07:21

我已经有一段时间没有摆弄同样的事情了,我的结论是,尽管拥有“发送为”权限,但这是不可能的。

模拟是使用 EWS 的唯一方法,请参阅 MSDN

ExchangeService service = new ExchangeService();
service.UseDefaultCredentials = true;
service.AutodiscoverUrl("[email protected]");

// impersonate user e.g. by specifying an SMTP address:
service.ImpersonatedUserId = new ImpersonatedUserId(
    ConnectingIdType.SmtpAddress, "[email protected]");

如果未启用模拟,您必须提供您想要代表的用户的凭据。请参阅这篇 MSDN 文章

ExchangeService service = new ExchangeService();
service.Credentials = new NetworkCredential("user", "password", "domain");
service.AutodiscoverUrl("[email protected]");

或者,您可以简单地指定 回复地址

EmailMessage mail = new EmailMessage(service);
mail.ReplyTo.Add("[email protected]");

但是,使用 System.Net.Mail 发送邮件时,“发送为”权限确实适用,在许多情况下,仅发送电子邮件时就可以了。有大量示例说明如何执行此操作

// create new e-mail
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add(new MailAdress("[email protected]"));
message.Subject = "Subject of e-mail";
message.Body = "Content of e-mail";

// send through SMTP server as specified in the config file
SmtpClient client = new SmtpClient();
client.Send(mail);

It's been a while since I fiddled with the same thing, and I concluded that it isn't possible, in spite of having "Send as" rights.

Impersonation is the only way to go with EWS, see MSDN:

ExchangeService service = new ExchangeService();
service.UseDefaultCredentials = true;
service.AutodiscoverUrl("[email protected]");

// impersonate user e.g. by specifying an SMTP address:
service.ImpersonatedUserId = new ImpersonatedUserId(
    ConnectingIdType.SmtpAddress, "[email protected]");

If impersonation isn't enabled, you'll have to supply the credentials of the user on behalf of whom you want to act. See this MSDN article.

ExchangeService service = new ExchangeService();
service.Credentials = new NetworkCredential("user", "password", "domain");
service.AutodiscoverUrl("[email protected]");

Alternatively you can simply specify a reply-to address.

EmailMessage mail = new EmailMessage(service);
mail.ReplyTo.Add("[email protected]");

However, "Send as" rights do apply when sending mail using System.Net.Mail, which in many cases will do just fine when just sending e-mails. There are tons of examples illustrating how to do this.

// create new e-mail
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add(new MailAdress("[email protected]"));
message.Subject = "Subject of e-mail";
message.Body = "Content of e-mail";

// send through SMTP server as specified in the config file
SmtpClient client = new SmtpClient();
client.Send(mail);
∞琼窗梦回ˉ 2025-01-03 22:07:21

我认为您应该使用 Sender 属性,以便您的代码应如下所示:

EmailMessage message = new EmailMessage(service);
message.Body = txtMessage;
message.Subject = txtSubject;
message.Sender= txtFrom;
....
message.SendAndSaveCopy();

i think you should use the Sender property so the your code should look like:

EmailMessage message = new EmailMessage(service);
message.Body = txtMessage;
message.Subject = txtSubject;
message.Sender= txtFrom;
....
message.SendAndSaveCopy();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文