EWS托管API:如何设置来自电子邮件?

时间:2012-01-19 06:28:13

标签: c# ews-managed-api

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

但我尝试使用代码 - 这不起作用,在邮件中我在“来自”“帐户 @ domain.com”字段中阅读。

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

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

2 个答案:

答案 0 :(得分:6)

自从我摆弄同样的事情已经有一段时间了,我总结说尽管有“发送为”的权利,但这是不可能的。

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

ExchangeService service = new ExchangeService();
service.UseDefaultCredentials = true;
service.AutodiscoverUrl("app@domain.com");

// impersonate user e.g. by specifying an SMTP address:
service.ImpersonatedUserId = new ImpersonatedUserId(
    ConnectingIdType.SmtpAddress, "user@domain.com");

如果未启用模拟,则必须代表您要操作的用户提供用户的凭据。请参阅this MSDN article

ExchangeService service = new ExchangeService();
service.Credentials = new NetworkCredential("user", "password", "domain");
service.AutodiscoverUrl("user@domain.com");

或者,您只需指定reply-to address

EmailMessage mail = new EmailMessage(service);
mail.ReplyTo.Add("user@email.com");

但是,使用System.Net.Mail发送邮件时,“发送为”权限执行适用,在许多情况下,只需发送电子邮件即可。有tons of examples说明how to do this

// create new e-mail
MailMessage mail = new MailMessage();
mail.From = new MailAddress("user@domain.com");
mail.To.Add(new MailAdress("recipient@somewhere.com"));
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);

答案 1 :(得分:1)

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

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