August 2008 - Posts - Hilary Stoupa
in

InfoPath Dev

Hilary Stoupa

August 2008 - Posts

  • So you want to send an email.....

    There are two very common email scenarios with InfoPath. One -- you want to use an email submit connection but modify some things before the email goes out (common reasons: you'd like to use form fields in the Information section which is not currently supported in the user interface, you need more flexibility to determine who the email should be sent to, you have other logic you need to perform prior to sending the form) and two -- you don't want to submit the form via email, you just need to send an email to someone with some form information in it.

    Let's address both of those scenarios. A few caveats first --for scenario 1, I haven't included error handling. If your user cancels the submit when they see the preview of the email to be submitted, they are going to get some ugly messages. I'm sorry. It is Sunday afternoon, and I'm being a slacker. Also, for scenario 2,  your form will have to be full trust to do cool stuff like send an email that isn't based on a submit connection -- at least, mine had to be. 

    Here is the data source I'll be using for all the samples, just so you know what the xpath is pointing to:
    Data Source

    Okay, without further ado, scenario 1:

    Modify email submit properties before submitting:

    In this scenario, I have added an email submit connection to my form. My form submit options are set to use my email connection:

    Submit Options dialog

    Also, I've added a button to my form that I'm *calling* a submit button, but in reality, it is just an ordinary old button I've labeled Submit:

    Button Properties

    C# -- 2007 Object Model

            public void btnSubmit_Clicked(object sender, ClickedEventArgs e)
            {
                //get the value from the text field
                string interestingText = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:textField", NamespaceManager).Value;
                //get the value from the date field
                string date = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:dateField", NamespaceManager).Value;
     
                //get that email submit connection
                EmailSubmitConnection emailSubmit = (EmailSubmitConnection)this.DataConnections["Email Submit"];
     
                //set the introduction with a value from the form
                emailSubmit.Introduction = "This is my shiny new introduction with a date from my form: " + date + "\nAnd an interesting value from a text field as well: " + interestingText;
     
                //submit the form
                this.Submit();
            }

    VB.Net -- 2007 Object Model

            Public Sub btnSubmit_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
                'get the value from the text field
                Dim interestingText As String = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:textField", NamespaceManager).Value
                'get the value from the date field
                Dim dateField As String = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:dateField", NamespaceManager).Value
     
                'get that email submit connection
                Dim emailSubmit As EmailSubmitConnection = Me.DataConnections("Email Submit")
     
                'set the introduction with a value from the form
                emailSubmit.Introduction = "This is my shiny new introduction with a date from my form: " + dateField + System.Environment.NewLine + "And an interesting value from a text field as well: " + interestingText
     
                'submit the form
                Me.Submit()
            End Sub

    And, scenario 2:

    Send an email with some form information in it:

    We are going to use a button click event (I named my button btnEmail -- nothing against the oh-so-intuitive IP default names like 'CTRL15_23' but I am fond of slightly more meaningful names) and send an email to someone with information from the textField and the dateField in our form.

    C# -- 2007 Object Model

            public void btnEmail_Clicked(object sender, ClickedEventArgs e)

            {

                //get the value from the text field

                string subject = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:textField", NamespaceManager).Value;

                //get the value from the date field

                string date = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:dateField", NamespaceManager).Value;

               

                //set the information for the message

                MailAddress from = new MailAddress("someone@someplace.com");

                MailAddress to = new MailAddress("someoneElse@someplace.com");

                MailMessage msgmail = new MailMessage(from, to);

     

                //the body has the date variable we created in it, the subject uses the value from our text box -- mind you, this is all made up for a demo, so it isn't all that interesting

                msgmail.Body = "A new form was created on " + date + ". Please review it.";

                msgmail.Subject = subject;

     

                //this stuff is going to vary depending on your setup

                SmtpClient client = new SmtpClient("smtp.someServer.com");

                client.Credentials = new System.Net.NetworkCredential("someUserName", "somePassword");

     

                //send that baby out

                client.Send(msgmail);

            }

    At the top of your code window, with the other "using" statements, be sure to add this:

    using System.Net.Mail;

    VB.Net -- 2007 Object Model

            Public Sub btnEmail_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
                'get the value from the text field
                Dim subject As String = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:textField", NamespaceManager).Value
                'get the value from the date field
                Dim dateField as String = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:dateField", NamespaceManager).Value
     
                'set the information for the message
                Dim fromAddress As MailAddress = New MailAddress("someone@someplace.com")
                Dim toAddress As MailAddress = New MailAddress("someoneElse@someplace.com")
                Dim msgmail As MailMessage = New MailMessage(fromAddress, toAddress)
     
                'the body has the date variable we created in it, the subject uses the value from our text box -- mind you, this is all made up for a demo, so it isn't all that interesting
                msgmail.Body = "A new form was created on " + dateField + ". Please review it."
                msgmail.Subject = subject
     
                'this stuff is going to vary depending on your setup
                Dim client As SmtpClient = New SmtpClient("smtp.someServer.com")
                client.Credentials = New System.Net.NetworkCredential("someUserName", "somePassword")
     
                'send that baby out
                client.Send(msgmail)
            End Sub

    At the top of your code window, with the other "Imports" statements, be sure to add this:

    Imports System.Net.Mail

    Now promise to only use your newfound powers for good, and have fun!

  • InfoPath 2007 Object Model Map

    Okay, I know I'm always a few days late to the picnic, but I found this great poster while online looking for something else, and thought this would be a great place to share it. Now if I just had a humongous color printer, I'd finally be able to gussy up my office properly....

Copyright © 2003-2017 Qdabra Software. All rights reserved.
View our Terms of Use.