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;

InfoPath 2010 : How to make Text fields Visible False

I was just trying to figure our, how can we make the fields visible false in Infopath designer 2010.

I didnot find any direct way, but what we can do is, while designing, create textfield, appropriate field name will get create under "MYfields". Later delete the Text field from UI, but stil you can work with the fields declared under "MyFields".