简体   繁体   中英

EWS Managed API: how to set From of email?

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? :)

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("app@domain.com");

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

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("user@domain.com");

Alternatively you can simply specify a reply-to address .

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM