How To: Submit Data as an E-mail Message

Submitting data as an e-mail message is useful in many instances, especially when the recipient of the message needs to review the completed form. The EmailAdapter and the MailEnvelope objects allow programmatic submitting of forms via e-mail. Additionally, InfoPath e-mail forms, a feature of Microsoft Office InfoPath 2007, integrates the form editing into Outlook e-mail messages.

Using the Mail Envelope object

Using the Mail Envelope object shows the e-mail header above the form. Users must press the Send button in order to send the e-mail. A task pane is shown to allow users to choose to attach the form to the message or send a snapshot, but the HTML representation of the form is always sent in the body of the message.

In the following example, a button with custom code is used to populate and show the Mail Envelope with data from the form. The Mail Envelope is shown when the user clicks the button:

  function CTRL7_5::OnClick(eventObj)
{
  var mail = XDocument.View.Window.MailEnvelope;

var varTo = XDocument.DOM.selectSingleNode("/my:myFields/my:EmailInfo/my:To"); var varCC = XDocument.DOM.selectSingleNode("/my:myFields/my:EmailInfo/my:CC"); var varBCC = XDocument.DOM.selectSingleNode("/my:myFields/my:EmailInfo/my:BCC"); var varIntro = XDocument.DOM.selectSingleNode("/my:myFields/my:EmailInfo/my:Intro"); var varSubject = XDocument.DOM.selectSingleNode("/my:myFields/my:EmailInfo/my:Subject");

// Set field values mail.To = varTo.text; mail.CC = varCC.text; mail.BCC = varBCC.text; mail.Subject = varSubject.text; mail.Intro = varIntro.text; mail.Visible = true; }

Using the Email Adapter

Because the EmailAdapter is like other data adapters, you can use the Submit method to send the e-mail message after setting the appropriate properties for the message. Like using the MailEnvelope object, the EmailAdapter requires the user to press the Send button, however the EmailAdapter displays a separate dialog box instead of showing the e-mail information above the form. The EmailAdapter also requires an e-mail data adapter to be configured at design time. To create an e-mail adapter for submitting the form, follow these steps:

  1. Click Data Connections on the Tools menu.
  2. Click the Add button.
  3. Click Create a new connection to and Submit data, and click Next.
  4. Click As an e-mail message, and click Next.
  5. Type an e-mail address in the To field and delete the text from the Introduction box, then click Next
    Bb250998.vs_note(en-us,office.12).gif  Note
    If you want to create a more dynamic e-mail address, use the function button at the end of the text box, which allows you to create formulas and reference data in the form to construct an e-mail address.
    .
  6. Click Send only the active view of the form and no attachment, and click Next.
    Bb250998.vs_note(en-us,office.12).gif  Note
    If you need to send the form template as an attachment so that the recipient could open it in InfoPath and continue filling it out, check the Attach the form template to ensure that users can open the form.
  7. Type a name for the data adapter and click Finish.
  8. On the Data Connections dialog box, click Close.

You can programmatically manipulate the e-mail data connection to send e-mail messages when the form is submitted, after confirmation by the user. The following example uses values from the form to populate the e-mail message:

  function CTRL8_5::OnClick(eventObj)
{
	var objEmailAdapter;
	var varTo = XDocument.DOM.selectSingleNode("/my:myFields/my:EmailInfo/my:To");
	var varCC = XDocument.DOM.selectSingleNode("/my:myFields/my:EmailInfo/my:CC");
	var varBCC = XDocument.DOM.selectSingleNode("/my:myFields/my:EmailInfo/my:BCC");
	var varIntro = XDocument.DOM.selectSingleNode("/my:myFields/my:EmailInfo/my:Intro");
	var varSubject = XDocument.DOM.selectSingleNode("/my:myFields/my:EmailInfo/my:Subject"); 
	
	objEmailAdapter = XDocument.DataAdapters("Email Adapter Name"); 
	objEmailAdapter.To = varTo.text;
	objEmailAdapter.CC = varCC.text;
	objEmailAdapter.BCC = varBCC.text;
	objEmailAdapter.Intro = varIntro.text;
	objEmailAdapter.Subject = varSubject.text; 
	objEmailAdapter.Submit();
}
Bb250998.vs_note(en-us,office.12).gif  Note
Change the name of the e-mail adapter name from "Email Adapter Name" to the name you gave the e-mail adapter in step 7 of creating the e-mail adapter. Also note that the function names in both functions above are automatically generated by InfoPath when creating custom code for a control.

Tip: If you have configured the form template to always submit to a different data source, you can use an InfoPath rule to conditionally submit the form data using the e-mail adapter, for example, based on the current user's role or when a field contains a particular value. For more information, see How To: Add Basic Workflow with InfoPath Roles and Rules.

Sending and receiving InfoPath forms in Outlook

With Office InfoPath 2007, you can deploy forms as Microsoft Office Outlook 2007 e-mail messages, so colleagues can complete those forms in Outlook. After you’ve collected the information in Office Outlook 2007, you can export it to a Microsoft Office Excel 2007 spreadsheet or merge the data back into a single InfoPath form.

For more information about InfoPath integration with Outlook, see Introduction to sending and receiving forms as Outlook e-mail messages.