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 !
Like this:
Like Loading...