How to send email from an InfoPath browser form in SharePoint

Learn which options are available for InfoPath to send email through an InfoPath form that is running in InfoPath Forms Services in SharePoint.

ADVERTISEMENTS

4 Ways to send data through email from an InfoPath form

Excluding methods which are available within SharePoint (but not within InfoPath) to send email such as sending email through an ItemAdded event handler or through a workflow, there are 4 ways to send email from an InfoPath form, which is running in InfoPath Forms Services in SharePoint:

  1. Use Rules with an email submit data connection.
  2. Programmatically send an email through an EmailSubmitConnection object.
  3. Use the SendEmail method of the SPUtility object in SharePoint.
  4. Use the MailMessage and SmtpClient classes in the System.Net.Mail namespace.

The first 3 methods require you to install and configure an SMTP server for SharePoint. The last method only requires an SMTP server that has anonymous access enabled.

All of the methods described in this article make use of an InfoPath form template that has 4 Text Box controls named toAddress, fromAddress, subject, and body, and one Button control which is used to send email.

1. Use Rules to send email in InfoPath

Add an email submit data connection named Email Submit to the InfoPath form template, and then add a Rule on the button that says:

Submit using a data connection: Email Submit

Advantages:

  • You do not have to give the InfoPath form template full trust.

Disadvantages:

  • You have to configure outgoing email for SharePoint.
  • You cannot specify a From email address; the email address you configured for outgoing email in SharePoint will be used as the From email address.
  • You cannot attach files other than the InfoPath form itself to the email.

Example:

InfoPath 2013 with SharePoint 2013 video tutorial for how to send email in SharePoint via an InfoPath email submit connection.

2. Use an EmailSubmitConnection to send email in InfoPath

Add an email submit data connection named Email Submit to the InfoPath form template, and then add the following C# code to the Clicked event handler for the button:

XPathNavigator root = MainDataSource.CreateNavigator();

string toAddress = root.SelectSingleNode("//my:toAddress",
  NamespaceManager).Value;
string fromAddress = root.SelectSingleNode("//my:fromAddress",
  NamespaceManager).Value;
string subject = root.SelectSingleNode("//my:subject",
  NamespaceManager).Value;
string body = root.SelectSingleNode("//my:body",
  NamespaceManager).Value;

EmailSubmitConnection conn =
  (EmailSubmitConnection)DataConnections["Email Submit"];
conn.To.SetStringValue(toAddress);
conn.Subject.SetStringValue(subject);
conn.Introduction = body;
conn.EmailAttachmentType = EmailAttachmentType.None;
conn.Execute();

Advantages:

  • You do not have to give the InfoPath form template full trust.

Disadvantages:

  • You have to configure outgoing email for SharePoint.
  • You cannot specify a From email address; the email address you configured for outgoing email in SharePoint will be used as the From email address.
  • You cannot attach files other than the InfoPath form itself to the email.

3. Use SPUtility.SendMail to send email in InfoPath

Add a reference to the SharePoint DLL to your InfoPath form template project and then add the following using statements to the FormCode.cs file:

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

Add the following C# code to the Clicked event handler for the button:

XPathNavigator root = MainDataSource.CreateNavigator();

string toAddress = root.SelectSingleNode("//my:toAddress",
  NamespaceManager).Value;
string fromAddress = root.SelectSingleNode("//my:fromAddress",
  NamespaceManager).Value;
string subject = root.SelectSingleNode("//my:subject",
  NamespaceManager).Value;
string body = root.SelectSingleNode("//my:body",
  NamespaceManager).Value;

SPWeb web = SPContext.Current.Web;
bool result = SPUtility.SendEmail(
  web, false, false, toAddress, subject, body);

Disadvantages:

  • You must give the InfoPath form template full trust and sign it with a digital certificate.
  • You have to configure outgoing email for SharePoint.
  • You cannot specify a From email address; the email address you configured for outgoing email in SharePoint will be used as the From email address.
  • You cannot attach files to the email.

4. Use System.Net.Mail classes to send email in InfoPath

Add the following using statement to the FormCode.cs file:

using System.Net.Mail;

Add the following C# code to the Clicked event handler for the button:

XPathNavigator root = MainDataSource.CreateNavigator();

string toAddress = root.SelectSingleNode("//my:toAddress",
  NamespaceManager).Value;
string fromAddress = root.SelectSingleNode("//my:fromAddress",
  NamespaceManager).Value;
string subject = root.SelectSingleNode("//my:subject",
  NamespaceManager).Value;
string body = root.SelectSingleNode("//my:body",
  NamespaceManager).Value;

using (MailMessage msg = new MailMessage(fromAddress, toAddress))
{
  msg.Subject = subject;
  msg.Body = body;
  msg.IsBodyHtml = false;

  SmtpClient smtp = new SmtpClient("<your_smtp_server_name>");
  smtp.Send(msg);
}

Advantages:

  • You do not have to configure outgoing email for SharePoint.
  • You can specify a From email address.
  • You can attach files other than the InfoPath form to the email.

Disadvantages:

  • You must give the InfoPath form template full trust and sign it with a digital certificate.

Example:

Recipe 100 of InfoPath 2010 Cookbook 3 (for C#) and InfoPath 2010 Cookbook 4 (for VB) shows you how you can use the MailMessage class in a custom SharePoint Designer workflow activity that can be called from a SharePoint Designer 2010 workflow to loop through attachments in an InfoPath form to be able to send an email with attachments via a workflow. Because the sending of the email takes place outside of the form, you would not have to give the form template Full Trust nor sign it with a digital certificate.

Conclusion

You can use Rules, an EmailSubmitConnection, the SPUtility.SendEmail method, or classes in the System.Net.Mail namespace to submit or send email in InfoPath, but only the last method provides you with total control over the From email address and email attachments.

The method you choose to use InfoPath to send email will depend largely on whether you want to or can write code or not, what you would like to include in emails you send, and whether you require to include attachments and/or have control over the From email address.

 


Related InfoPath Articles:

 

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services