Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, June 30, 2011

Sending Email Using Exchange Server 2010 EWS in Asp.Net / C#

Sending email using Exchange server EWS is breeze as compare to horrible syntaxed Web Dav ( sorry to WebDav Lovers :) ) . I worked with WebDav and many times banged my head against wall figuring out the actual issue in case of error.

EWS is web service based API of exchange server and provide very managed and readable code for sending email.

Let's start writing some code to work with ESW 2010.

First of all Add reference to Microsoft.Exchange.WebServices.dll and place Microsoft.Exchange.WebServices.xml file in your bin 

Next import following namespace.

 

using Microsoft.Exchange.WebServices;


 

public static void SendExchangeEmail(string to, string from, string subject, string body, string fileattach)

 

        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);

        service.Credentials = new NetworkCredential("ExchangeUser", "UserPassword");

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

        EmailMessage newMsg = new EmailMessage(service);

        foreach (string s in to.Split(new char[] { ',' }))

        {

            newMsg.ToRecipients.Add(s.Trim());

        }

        newMsg.Body = body;

        newMsg.Subject = subject;

        newMsg.From = from;        

 

        if (!string.IsNullOrEmpty(fileattach))

        {

            newMsg.Attachments.AddFileAttachment(fileattach);

        }

 

        newMsg.SendAndSaveCopy();  OR ( depending upon your requirment)

        newMsg.Send();

}

 

Note: Use Send method just to send and use "SendAndSave" to save a copy in your sent items

Cheers :) 

Tuesday, August 24, 2010

Silent Printing PDF File in C# and closing Adobe Popup

Printing is very common task in Desktop App. In this article i will demonstrate how to print a PDF File Silently. There are two approaches. First We can print the file using PDF Sharp ( a open source library to support PDF Related Operations) and Arobate exe. Second Approach is to intiate a process that call Acrobate now doubt both need at least acrobat installed on machine, as PDF Sharp can not render PDF It self. I will demonstrate the first way.
Add Reference to PDF Sharp. First inform PdfFilePrinter about path of Adobe reader exe on target machine.
PdfFilePrinter.AdobeReaderPath = @"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe";

///Then in next step create a instance of the class PdfFilePrinter and provide path of file to print and your default active printer.

try
{
PdfFilePrinter Pdfprinter = new PdfFilePrinter(filetoprint, printer);
Pdfprinter.Print();
}
catch(Exception ex)
{
throw ex;
}
Now get the Adobe Process and kill it if it has finished its job.

foreach (Process proc in Process.GetProcesses())
{
if (proc.ProcessName.StartsWith("Acro"))
{
if (proc.HasExited == false)
{
proc.WaitForExit(10000);
proc.Kill();
break;
}
else
{
proc.Kill();
break;
}
}
}


if you have any question please free to ask. Hope this helps.