Email document via Office 365 Exchange Web Services Managed API 1.1 from Windows File Context Menu

I was just exploring the Microsoft Exchange Web Services API for Office 365 through which you can programmatically perform several operations on Office 365 Exchange account.

In this post i will walk you through the application which sends the selected file to particular recipient as an attachment. It does not only sends a document but also registers a new context menu on files/document in windows registry that gives an option to user to just select the file and email it as an attachment.

Below snap shows the console window that displays the processing log when the user initiated the request.

Let’s start with the actual work.

1. First of all you need to create an Office365 account from MS Office365 site and download the Microsoft Exchange Web API 1.1. from Microsoft Exchange Web Services API 1.1..

2. Now there are two projects mainly, one which creates an entry into registry. For this i have created a windows application that makes an entry into the registry. Other one is the console application which sends a document selected to the recipient specified in the app.config. This is just for the demo purpose you can also specify a list somewhere in the database etc and read the recipient addresses from there.

3. Below is the screen shot of the Windows form application from where the user creates a context menu in the windows registry at file level.

Above screen show two text field controls named Menu Name and Exe Path. In the “Menu Name” text field we just specify the menu we want to create in the windows registry where as the “Exe Path” holds the executable (in our case a console application) that launches when user click on that menu.

Following is the code snippet for making an entry into registry.

private void btnCreate_Click(object sender, EventArgs e)
{

RegistryKey regmenu = null;
RegistryKey regcmd = null;
try
{
regmenu = Registry.ClassesRoot.CreateSubKey(MenuName);
if (regmenu != null)
regmenu.SetValue("", this.txtMenuName.Text);

regcmd = Registry.ClassesRoot.CreateSubKey(Command);
if (regcmd != null)
regcmd.SetValue("", this.txtExecutablePath.Text+ " \"%1\"");

}
catch (Exception ex)
{
MessageBox.Show(this, ex.ToString());
}
finally
{
if (regmenu != null)
regmenu.Close();
if (regcmd != null)
regcmd.Close();
}

}
This will create an entry into the registry.

Lets now create a console application which will use Web Exchange API to send emails.

4. Create a new Console Application project.

5. Add Reference to  Microsoft.Exchange.WebServices.

6. Following code snippet shows the way of using Web Exchange Service and send an email as an attachment.

class Program
{
static String _primaryLabUserId;
static ExchangeService _service;
static void Main(string[] args)
{
String filepath = args[0].ToString();
Console.WriteLine(filepath);
Console.WriteLine("Sending Email");
ConnectToExchangeService();
SendEmail("From_address@youroffice365domain.com", "to_Address@abc.com", "Subject", "Body", filepath);
Console.WriteLine("Email Sent!");
Console.Read();
}

private static void ConnectToExchangeService()
{
//PrimaryLabUserId is your Office 365 Id. Specify PrimaryLabUserId in app.config under AppSettings tag
if (String.IsNullOrEmpty(System.Configuration.ConfigurationSettings.AppSettings["PrimaryLabUserId"]))
{
throw new ArgumentNullException("Please provide value for PrimaryLabUserId in app.config");
}

_primaryLabUserId = ConfigurationSettings.AppSettings["PrimaryLabUserId"];

// Create an instance of ExchangeService for a specific version of Exchange Server
_service = new ExchangeService(ExchangeVersion.Exchange2010);
_service.Credentials = new System.Net.NetworkCredential()
{
UserName = _primaryLabUserId,
Password = "yourPassword";
};

// Call Autodiscover to return the Url of the most efficient CAS server for the given email address
_service.AutodiscoverUrl(_primaryLabUserId, UrlValidationCallback);
}

private static bool UrlValidationCallback(string redirectionUrl) { return true; }
private static void SendEmail(string from, string to, string subject, string body, string attachmentName)
{
//Create an email message and initialize it with the from address, to address, subject and the body of the email.
EmailMessage emailMessage = new EmailMessage(_service);

//Specified Recipients
EmailAddress addressTo = new EmailAddress();
addressTo.Address = to;
emailMessage.ToRecipients.Add(addressTo);

//Specified From
emailMessage.From = new EmailAddress() { Address = from };
emailMessage.Subject = subject;
emailMessage.Body = body;
emailMessage.Attachments.AddFileAttachment(attachmentName);
emailMessage.Send();
}
}

Below is the screen shot of the email item received on my Inbox at Office 365 exchange.

Happy Coding !

88 thoughts on “Email document via Office 365 Exchange Web Services Managed API 1.1 from Windows File Context Menu

  1. I cherished as much as you will receive performed proper here. The sketch is attractive, your authored subject matter stylish. nonetheless, you command get got an shakiness over that you want be handing over the following. in poor health indubitably come further earlier once more since exactly the same nearly a lot frequently within case you defend this increase.

  2. Thank you so much pertaining to giving me personally an update on this theme on your web page. Please be aware that if a fresh post becomes available or if perhaps any changes occur on the current post, I would be interested in reading more and understanding how to make good utilization of those strategies you reveal. Thanks for your time and consideration of people by making your blog available.

  3. I don’t know whether it’s just me or if everyone else encountering problems with your site. It appears like some of the text in your posts are running off the screen. Can someone else please comment and let me know if this is happening to them as well? This might be a issue with my internet browser because I’ve had this happen before. Kudos

  4. I do agree with all of the concepts you have offered on your post. They are really convincing and will definitely work. Still, the posts are very brief for starters. May you please lengthen them a bit from next time? Thanks for the post.

  5. Great – I should certainly pronounce, impressed with your web site. I had no trouble navigating through all tabs as well as related info ended up being truly easy to do to access. I recently found what I hoped for before you know it at all. Reasonably unusual. Is likely to appreciate it for those who add forums or something, site theme . a tones way for your client to communicate. Nice task.

  6. You could certainly see your expertise in the paintings you write. The world hopes for more passionate writers like you who aren’t afraid to say how they believe. At all times follow your heart.

  7. I cherished as much as you will obtain carried out proper here. The cartoon is tasteful, your authored material stylish. however, you command get bought an impatience over that you wish be turning in the following. ill for sure come more in the past once more since exactly the similar just about a lot regularly within case you protect this hike.

  8. Hi there, just became aware of your blog through Google, and found that it is truly informative. Iím going to watch out for brussels. Iíll be grateful if you continue this in future. Numerous people will be benefited from your writing. Cheers!

  9. Good blog! I truly love how it is easy on my eyes and the data are well written. I am wondering how I might be notified whenever a new post has been made. I’ve subscribed to your feed which must do the trick! Have a great day!

  10. Excellent weblog right here! Also your website loads up very fast! What host are you using? Can I am getting your affiliate link in your host? I desire my web site loaded up as fast as yours lol.

  11. Certainly I like your website, but you have to check the spelling on quite a few of your posts. A number of them are rife with spelling problems and I find it very bothersome to inform you. Nevertheless I’ll certainly come back again!

  12. I think this is by far the most important info for me personally. And i’m glad going through your page. However need to provide feedback on some general things, The site style and design is excellent, the articles is indeed very good. Excellent job, cheers!

  13. Sharing some thing is better than keeping up-to our self, therefore the YouTube video that is posted at this place I am going to share by my family and mates.

  14. But wanna admit that this is extremely helpful, Thanks for taking your time to write this. “The least of learning is done in the classrooms.” by Thomas Merton.

  15. you’re in point of fact a good webmaster. The site loading velocity is incredible. It sort of feels that you are doing any distinctive trick. Moreover, The contents are masterwork. you have performed a fantastic task in this topic!

  16. We would like to thank you all over again for the lovely ideas you offered Jesse when preparing her post-graduate research and also, most importantly, for providing all the ideas in one blog post. Provided that we had been aware of your blog a year ago, we would have been kept from the nonessential measures we were choosing. Thank you very much.

  17. Thank you for the sensible critique. Me & my neighbor were just preparing to do some research about this. We got a grab a book from our local library but I think I learned more from this post. I am very glad to see such wonderful info being shared freely out there.

  18. If you’re still on the fence: grab your favorite earphones, head down to a Best Buy and ask to plug them into a Zune then an iPod and see which one sounds better to you, and which interface makes you smile more. Then you’ll know which is right for you.

  19. I had been honored to obtain a call coming from a friend as soon as he identified the important tips shared on your own site. Examining your blog posting is a real great experience. Thank you for thinking about readers much like me, and I want for you the best of success like a professional arena.

  20. This is just what I was looking for. I was not expecting that I’d get so much out of reading your write up! You’ve just earned yourself a returning visitor 🙂

  21. Hiya! Fantastic blog! I happen to be a daily visitor to your site (somewhat more like addict 😛 ) of this website. Just wanted to say I appreciate your blogs and am looking forward for more!

  22. I became honored to receive a call from my friend as soon as he discovered the important points shared on the site. Examining your blog post is a real fantastic experience. Thanks again for thinking about readers much like me, and I want for you the best of achievements being a professional realm.

  23. I am only writing to let you know what a extraordinary encounter my child undergone studying your web site. She came to understand so many issues, which include how it is like to have a great giving spirit to let certain people with ease thoroughly grasp a number of extremely tough subject areas. You really exceeded visitors’ expectations. Many thanks for coming up with the interesting, healthy, explanatory as well as fun guidance on the topic to Gloria.

  24. There are some attention-grabbing closing dates in this article but I don’t know if I see all of them heart to heart. There is some validity but I’ll take maintain opinion till I look into it further. Good article , thanks and we wish extra! Added to FeedBurner as well

  25. Good blog! I really love how it’s easy on my eyes as well as the data are well written. I am wondering how I could be notified whenever a new post has been made. I have subscribed to your rss feed which should do the trick! Have a nice day!

  26. Hey There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and come back to read more of your useful info. Thanks for the post. I will definitely return.

Leave a comment