简体   繁体   中英

Create mail in Exchange Online inbox programatically

Have been facing an issue since days about EWS. So my scenario is;

I was to programmatically sync GMAIL and EXCHANGE ONLINE. So here is what I have done;

  • Connect to Gmail using Gmail API
  • Fetch mail from gmail get the email body, to, from, attachment and all other thing
  • connect to Exchange online using EWS 2.0

Now the problem is here, how can I create an email in Inbox which looks like incoming mail from the sender;

Here is the code I have done;

_service = new ExchangeService(ExchangeVersion.Exchange2013);
        _service.TraceEnabled = true;
        _service.Credentials = new WebCredentials("admin@xyz.onmicrosoft.com", "password");
        _service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
        _service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "xyz@xyz.onmicrosoft.com");

        EmailMessage message = new EmailMessage(_service);
        Random r = new Random();
        message.Subject = "Email Message";
        message.From = new EmailAddress("xyz@gmail.com");
        message.Sender = new EmailAddress("xyz@gmail.com");
        message.Body = new MessageBody(BodyType.HTML, "<HTML><body><h1>This is a voice mail.</h1></BODY></HTML>");
        message.ToRecipients.Add(new EmailAddress(""));
        message.Save(WellKnownFolderName.Inbox);

This way its creating an email in inbox but it shows as a draft mail. I dont want it, I want it to look as RECEIVED mail.

Am I doing anything wrong?

You need to set a couple of properties before saving the message.

// Set a delivery time
ExtendedPropertyDefinition PidTagMessageDeliveryTime =
    new ExtendedPropertyDefinition(0x0E06, MapiPropertyType.SystemTime);
DateTime deliveryTime = DateTime.Now; // Or whatever deliver time you want
message.SetExtendedProperty(PidTagMessageDeliveryTime, deliveryTime);

// Indicate that this email is not a draft. Otherwise, the email will appear as a 
// draft to clients.
ExtendedPropertyDefinition PR_MESSAGE_FLAGS_msgflag_read = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
message.SetExtendedProperty(PR_MESSAGE_FLAGS_msgflag_read, 1);

These properties aren't settable after the items is saved, so it's important to do it before the first Save call.

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