11
votes

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

2

2 Answers

6
votes

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);
1
votes

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