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 !
Lovely website! I am loving it!! Will come back again. I am bookmarking your feeds also
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.
I was looking through some of your blog posts on this website and I conceive this website is really informative! Keep posting.
I’ve learn a few excellent stuff here. Certainly value bookmarking for revisiting. I surprise how so much attempt you put to make one of these wonderful informative website.
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.
I was studying some of your articles on this internet site and I conceive this web site is really instructive! Continue putting up.
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
I am lucky that I observed this web blog, precisely the right info that I was searching for!
Thanks for any other informative web site. Where else may I get that type of info written in such a perfect means? I have a project that I am just now operating on, and I have been on the look out for such info.
You have noted very interesting details! ps nice website.
Regards for all your efforts that you have put in this. very interesting information. “The price of greatness is responsibility.” by Sir Winston Leonard Spenser Churchill.
Hi! Do you know if they make any plugins to help with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good results. If you know of any please share. Kudos!
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.
Rattling superb info can be found on website . “I said I didn’t want to run for president. I didn’t ask you to believe me.” by Mario M Cuomo.
Of course, what a fantastic website and instructive posts, I will bookmark your blog.Have an awsome day!
I like this post, enjoyed this one appreciate it for posting .
Wow, marvelous weblog format! How lengthy have you ever been blogging for? you make blogging look easy. The overall look of your site is wonderful, let alone the content material!
Very interesting points you have observed, appreciate it for putting up.
I went over this site and I believe you have a lot of superb info, saved to my bookmarks (:.
Some really wonderful posts on this website , thankyou for contribution.
Very interesting info!Perfect just what I was looking for!
You could definitely see your expertise within the work you write. The world hopes for more passionate writers like you who are not afraid to mention how they believe. At all times go after your heart.
I was recommended this blog by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my problem. You are wonderful! Thanks!
Very superb information can be found on site.
Very interesting details you have remarked, appreciate it for putting up.
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.
Rattling wonderful info can be found on web site.
I savor, cause I found just what I used to be looking for. You have ended my 4 day lengthy hunt! God Bless you man. Have a great day. Bye
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.
I conceive this web site holds very superb composed content material content.
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.
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!
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!
Very interesting details you have mentioned, regards for posting.
Excellent read, I just passed this onto a friend who was doing some research on that. And he actually bought me lunch as I found it for him smile Thus let me rephrase that: Thanks for lunch!
I think you have mentioned some very interesting points , thanks for the post.
Amaze! Thank you! I constantly wished to produce in my internet site a thing like that. Can I take element of the publish to my blog?
I wanted to show my respect for your good work. I just don’t know how you been able to makes this interesting, but you did. Thank you.
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.
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!
Good blog! I truly love how it is simple on my eyes and the data are well written. I’m wondering how I might be notified when a new post has been made. I have subscribed to your feed which must do the trick! Have a nice day!
Thank you for the auspicious writeup. It actually was a leisure account it. Look complicated to more added agreeable from you! However, how can we communicate?
hallo bros!! Huge site!
Some truly wonderful blog posts on this website , regards for contribution.
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!
Wohh just what I was looking for, regards for putting up.
I’ve recently started a blog, the information you offer on this website has helped me greatly. Thanks for all of your time & work.
I’ve got the impression that anyone who visits this blog falls in love with it
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.
I think you have mentioned some very interesting points , regards for the post.
Awesome piece of info! May I reference part of this on my blog if I post a backlink to this webpage? Thx.
Your method of telling all in this post is in fact fastidious, all be capable of without difficulty know it, Thanks a lot.
Very interesting topic, regards for putting up.
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.
Very interesting information!Perfect just what I was looking for!
There is visibly a bundle to realize about this. I assume you made various nice points in features also.
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!
I enjoy the efforts you have put in this, thanks for all the great blog posts.
I like this site very much so much superb info.
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.
Thankyou for all your efforts that you have put in this. very interesting information. “You can’t help someone get up a hill without getting closer to the top yourself.” by Gen. H. Norman Schwarzkopf.
I agree with most of your points, but a few need to be discussed further, I will hold a small talk with my partners and maybe I will look for you some suggestion soon.
I agree with most of your points, but a few need to be discussed further, I will hold a small talk with my partners and maybe I will look for you some suggestion soon.
I conceive this web site holds very excellent indited subject matter content.
As soon as I found this website I went on reddit to share some of the love with them.
I am glad that I discovered this web blog, precisely the right info that I was searching for!
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.
I like this weblog it’s a master piece! Glad I discovered this on google.
I was reading through some of your articles on this internet site and I think this web site is real instructive! Keep on putting up.
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.
Absolutely pent content material, Really enjoyed looking through.
Great article, just what I was looking for.
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.
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 🙂
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!
I was looking at some of your content on this site and I believe this website is very informative! Keep putting up.
I believe this website has some very fantastic information for everyone :D. “Calamity is the test of integrity.” by Samuel Richardson.
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.
Great post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!
Wow! Even though I know this stuff is out of my league. Its still very interesting.
Just added this blog to my favorites. I enjoy reading your blogs and hope you keep them coming!
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.
I love the efforts you have put in this, appreciate it for all the great articles.
Excellent job!
You are my aspiration , I have few web logs and often run out from to brand.
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
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!
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.