Monday, January 3, 2005

Sending Email From InfoPath Using Managed Code

It is possible to send an email from InfoPath when a form is submitted. Just add a new data connection and select "Email message" as your data submission method. You can also send email from InfoPath using managed code. You must have InfoPath toolkit installed on your machine in order to write managed code for InfoPath forms.

Ref: /ssa/archive/2006/05/01/6186.aspx

Direct link to download toolkit: http://www.microsoft.com/downloads/details.aspx?familyid=7E9EBC57-E115-4CAC-9986-A712E22879BB&displaylang=en

1. Open your form in design mode and add a button to it.
2. Double click the button and select "Edit Form Code..." from the dialog box that pops up. This will open a Visual Studio editor. Add following code in button's OnClick() event:
[InfoPathEventHandler(MatchPath="CTRL1_1", EventType=InfoPathEventType.OnClick)]
public void CTRL1_1_OnClick(DocActionEvent e)

{
MailMessage MailMsg = new MailMessage();
MailMsg.Body = "This is a test message and currently logged in user is " + System.Environment.UserName;
MailMsg.From = "share.point@yahoo.com";
MailMsg.To = "user@sharepoint.com";
MailMsg.Subject = "InfoPath Test Message";
SmtpMail.SmtpServer = "smtp.yourdomain.com";
SmtpMail.Send(MailMsg);
}

Don't forget to add System.Web.Mail namespace in your code. Add following line in the start of the page:
using System.Web;
using System.Web.Mail;
Hint: Many people complain that they can not add System.Web.Mail to the code. You must add this library in the references before you can use it in your code.
1. Right click "References" in solution explorer and select "Add Reference".
2. Select "System.Web.dll" (.NET Pane) and click OK.

-SSA