Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 16th, 2008, 09:52 AM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default Contact form XML attachments.

Hi,

I am developing a new site for a client that does not use any fixed datastore. This is not a problem as they do not want a CMS as the content will remain fairly static.

They wanted to use a data collection form for customers but also find a way of importing this into their own internal CRM.

I found the best way was to extend the contact form used in the TBH to generate an XML document on the fly and attach it to the MailMessage object used to send a message to the Contact form mail address specified in web.config.

They can then import this into other data stores if required.

Thought i would post the code here in case it's of any use to anyone:

Code:
Imports System.Net.Mail
Imports System.IO
Imports System.Xml
Namespace Retro.Web.UI
    Partial Class Contact
        Inherits BasePage
        'write in memory
        Dim msXml As MemoryStream = New MemoryStream()
        Dim objWriter As XmlTextWriter
        Protected Sub txtSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSubmit.Click
            Try
                ' send the mail
                Dim msg As New MailMessage
                msg.IsBodyHtml = False
                msg.From = New MailAddress(txtEmail.Text, txtName.Text)
                msg.To.Add(New MailAddress(Globals.Settings.ContactForm.MailTo))
                If Not String.IsNullOrEmpty(Globals.Settings.ContactForm.MailCC) Then
                    msg.CC.Add(New MailAddress(Globals.Settings.ContactForm.MailCC))
                End If
                msg.Subject = String.Format( _
                    Globals.Settings.ContactForm.MailSubject, txtSubject.Text)
                msg.Body = txtBody.Text

                'generate XML file and add as an attachment
                GenerateXml(msg, txtName.Text, txtEmail.Text, txtSubject.Text, txtBody.Text)

                Dim client As New SmtpClient()
                client.Send(msg)
                ' show a confirmation message, and reset the fields
                lblFeedbackOK.Visible = True
                lblFeedbackKO.Visible = False
                txtName.Text = ""
                txtEmail.Text = ""
                txtSubject.Text = ""
                txtBody.Text = ""
                Exit Try

            Catch ex As Exception
                lblFeedbackOK.Visible = False
                lblFeedbackKO.Visible = True
            Finally
                'clear up memory
                objWriter.Close()
                msXml.Close()
            End Try

        End Sub
        Protected Sub GenerateXml(ByVal mymessage As MailMessage, ByVal Name As String, ByVal Email As String, ByVal Subject As String, ByVal Body As String)
            'create an instance of the XmlTextWriter object
            objWriter = New XmlTextWriter(msXml, Encoding.UTF8)
            objWriter.Formatting = Formatting.Indented

            'start writing xml document
            objWriter.WriteStartDocument()

            'write a comment in our XML File
            objWriter.WriteComment("Contact Form Output")

            'starting with the root element
            objWriter.WriteStartElement("ContactDetails")



            'output the first contact detail element
            objWriter.WriteStartElement("ContactDetail", Nothing)

            objWriter.WriteElementString("Name", Name)
            objWriter.WriteElementString("Email", Email)
            objWriter.WriteElementString("Subject", Subject)
            objWriter.WriteElementString("Body", Body)
            objWriter.WriteElementString("Date", Date.Today())

            'close contact detail element
            objWriter.WriteEndElement()

            'end contactdetails element
            objWriter.WriteEndElement()


            'flush and write XML data to file
            objWriter.Flush()

            msXml.Flush()

            msXml.Position = 0
            Dim att As New Attachment(msXml, DateTime.Now.ToFileTime.ToString() & ".xml")
            mymessage.Attachments.Add(att)
        End Sub
    End Class
End Namespace
This is just a draft implementation. As they actually require a more detailed data collection form I will probably create a new contact form class so I am not duplicating the data passed back to the various methods.

Cheers
Retro





Similar Threads
Thread Thread Starter Forum Replies Last Post
Error With Contact Form ASP.NET 3.5 CSharpCoder BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 July 28th, 2008 04:08 PM
Contact Form and Gmail Jamessaep BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 5 April 16th, 2008 07:58 AM
Contact Form myself Classic ASP Basics 1 November 18th, 2006 11:16 AM
PHP contact form probe4 PHP How-To 1 September 14th, 2005 12:52 PM
First contact with XML/Schema and MSXML lpinho XML 0 November 27th, 2003 09:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.