ASP.NET - Add Email Message to Outlook Draft Folder with Attachements

I was recently looking for a way to add an email to a user's draft folder with attachements. Exchange web services was the ticket.  See the screen shot and code snippet attached.

The trick is to add to a user's outlook draft folder you need a user id with higher level rights and allow impersonation must be turned on in Exchange.

using System.DirectoryServices;
using Microsoft.Exchange.WebServices.Data;

namespace ExchangeWSTest
{
    public partial class ActiveXTest : System.Web.UI.Page
    {       
              
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
        protected void Page_Load(object sender, EventArgs e)
        {
            service.Credentials = new WebCredentials("youremail", "yourpassword");
            service.Url = new Uri("https://client.yourexchangeserver.com/ews/exchange.asmx");           
        }

        protected void BtnCreateMsg_Click(object sender, EventArgs e)
        {          
            EmailMessage message = new EmailMessage(service);
            message.Subject = TxtSubject.Text;

            string MSG = TxtBody.Text;
            MSG = MSG.Replace("\n", "
"); message.Body = MSG; message.Attachments.AddFileAttachment(@"C:\temp\TEST Word Doc.docx"); message.Attachments.AddFileAttachment(@"C:\temp\TestPDF.pdf"); message.Save(); LblMsg.Text = "Email created, please check your draft folder"; } } }

Bind Generic List to ASP.NET DropDownBox (or GridView)

//Class for generic list
public class MarketDates
{
    public string DisplayDate { get; set; }
    public string SelectDate { get; set; }
}

 //List for binding
List LstFromDate = new List();
List LstToDate = new List();

//Load List
for (int i = 1999; i < (DateTime.Now.Year + 1); i++)
{
   MarketDates DDLFromDate = new MarketDates();
   MarketDates DDLToDate = new MarketDates();

   DDLFromDate.DisplayDate = "JAN-" + i.ToString();
   DDLFromDate.SelectDate = i.ToString();
   LstFromDate.Add(DDLFromDate);

   DDLToDate.DisplayDate = "DEC-" + i.ToString();
   DDLToDate.SelectDate = i.ToString();
   LstToDate.Add(DDLToDate);
}

//Bind
DDLFromYear.DataTextField = "DisplayDate";
DDLFromYear.DataValueField = "SelectDate";
DDLFromYear.DataSource = LstFromDate;
DDLFromYear.DataBind();
     
DDLToYear.DataTextField = "DisplayDate";
DDLToYear.DataValueField = "SelectDate";
DDLToYear.DataSource = LstToDate;
DDLToYear.DataBind();  

Call SQL Server Procedure in a Select Statement

I have a procedure that returns data that is used to populate a grid. I wanted to use the same procedure to pull out x number of rows from another procedure. I thought if I could call a the stored procedure on a cursor vs. using a select statement I could reuse the procedure.  

I came across an article that covered OPENQUERY. Basically you create a linked server on the same database you want to access. Then in this way you can call a SQL Server Stored Procedure in a select statement and use it as part of a cursor!

--1. Allow Data Access to this SQL Instance
exec sp_serveroption @server = 'YOURSERVERNAME\YOURINSTANCE' 
                    ,@optname = 'DATA ACCESS' 
                    ,@optvalue = 'TRUE' 


--2. Use navtive OLEDB provider. server name set as localhost. Could be anything
EXEC master.dbo.sp_addlinkedserver @server = N'localhost',
                                   @srvproduct=N'',
                                   @provider=N'SQLNCLI',
                                   @datasrc=N'YOURSERVERNAME\YOURINSTANCE'
      
SELECT  * FROM OPENQUERY(localhost,'exec sp_who')
--http://msdn.microsoft.com/en-us/library/ms190479.aspx

On one SQL Server 2008 the query executes fine, on another I had to include the following:

SELECT  * FROM OPENQUERY(localhost,'SET FMTONLY OFF; SET NOCOUNT ON;exec MyDB.dbo.prc_get_some_data')

Reference this post on StackOverflow