简体   繁体   中英

Creating EmailMessage object without access to internet connection

I am creating a program that automatically sends an email using Microsoft Exchange after a process is done. If the user does not have an internet connection when sending the email, the program will save the email as a local .eml file. A separate service will periodically check and send these emails when the users connection is back up.

Here is my code for setting up a service and creating the message object:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Credentials = new WebCredentials("exampleaddress@example.com", "Password");
service.AutodiscoverUrl("exampleaddress@example.com", RedirectionUrlValidationCallback);
EmailMessage message = new EmailMessage(service);

The issue I'm running into is that in order to save the EmailMessage I need to initialize it with a service object. The service however requires the internet to autodiscover the URL. Requiring the internet to setup this service defeats the point of what I am trying to do.

Is there a way to setup this EmailMessage object without requiring a service and in turn the internet?

You don't necessarily need to use the autodiscover feature. You can provide the exchange URL manually if you know it in advance.

Instead of

service.AutodiscoverUrl("exampleaddress@example.com", RedirectionUrlValidationCallback);

use

service.Url = new Uri("https://example.com/EWS/Exchange.asmx");

How are you saving the message as an EML file eg if you using the MimeContent provided by the EmailMessage Class then what your trying to do wont work without a connection to Exchange. The MimeContent is generated by the Exchange Store so you would only get that Property populated for a message that exists in the Exchange Store.

If your generating the message from scratch just store the data in your own serialization format until your ready to send there shouldn't really be any advantage in using an EML file if your haven't yet generated any Mime content

Cheers Glen

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