EWS Managed API: how to set From of email?
Asked Answered
T

2

11

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

Tintinnabulum answered 19/1, 2012 at 6:28 Comment(0)
R
6

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);
Rivero answered 20/7, 2012 at 9:32 Comment(4)
mail.ReplyTo will not work, there is no setter on this. public EmailAddressCollection ReplyTo { get; }Latisha
Right, it's a collection. So you'll need to do mail.ReplyTo.Add("[email protected]") - I'll fix the example above.Rivero
Using Impersonation I get an error on mail.send. - The primary SMTP address must be specified when referencing a mailboxHeger
@Heger a quick search on Google, and I found this: social.msdn.microsoft.com/Forums/exchange/en-US/… - As the error indicates, you must use the primary SMTP address when specifying the mailbox you want to access, not a proxy/secondary address or alias.Rivero
M
1

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();
Mickens answered 4/9, 2013 at 8:55 Comment(1)
No this doesn't appear to work. I've tried setting sender, sender.name and sender.Address and even though they get set, the email still comes from the account specified in your service setup.Latisha

© 2022 - 2024 — McMap. All rights reserved.