Wednesday, September 29, 2010

InfoPath 2010: Send Email on button click ( Custom Code)

I was searching for sending Mail from Infopath form, on click of button through custom code.

Initially I tried this by googling,

EmailAdapterObject myEmailAdapter =

((EmailAdapterObject)thisXDocument.DataAdapters["Email Submit Internal"]);
 myEmailAdapter.To = list@example.com;
 myEmailAdapter.Subject = "Status Report";
 myEmailAdapter.Submit();

But in above code, I was not able to get "thisXDocument" reference.

Then I used below code to send Email

Microsoft.Office.Interop.Outlook.Application objOutlook = new Microsoft.Office.Interop.Outlook.Application();



Microsoft.Office.Interop.Outlook.MailItem objOutlookMsg = (MailItem)objOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);





objOutlookMsg.To = "  To Address ";


//objOutlookMsg.CC = AddressCC;


//objOutlookMsg.BCC = AddressBCC;


objOutlookMsg.Subject = "TestMail";
objOutlookMsg.Body = "Body of email";

// If you want to attach image 

objOutlookMsg.Attachments.Add(@"\image\header.jpg", Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, objOutlookMsg.Body.Length + 1, "My Attachment");


objOutlookMsg.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceNormal;


//objOutlookMsg.Display(true);


objOutlookMsg.Send();


objOutlook = null;
objOutlookMsg = null;

No comments: