• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

ASP.Net mail.... Large form...

hey there guys, i have another question... Hope no body minds me posting so much....

Anyway, i have a VERY large form, close to hundreds of different form objects that need to be filled out....

Now i was going to use a example i found online on how to send a email with ASP.NET but it is regarding a very small form... and uses System.Web.Mail...

How do i take my entire form and mail it off? How do i pull all my form objects into on hidden field that i can place into my Body of the email?

Nugs
0
Nugs
Asked:
Nugs
  • 14
  • 14
1 Solution
 
dfaithenCommented:
You probably should just concatenate your body on the server side i.e.
string mailbody = "field1: "+field1.Text + "\n";
mailbody += "field2: "+field2.Text + "\n";
mailbody += "field3: "+field3.Text + "\n";
mailbody += "field4: "+field4.Text + "\n";
etc.
I know this sounds like a lot of work pulling values for your numerous fields, but you will have to do it somewhere, either here or in client side javascript.
This will also enable you to assemble your email with a useful format, be it for reading by man or machine.
If this isnt what you were after I will try to help more.
Duane
0
 
NugsAuthor Commented:
Ok, that sucks but i guess i just have to do it...

Can you show me a complete example of this code...

I'm going to be using something like this code to do the mailing.... Can you also show me how to include the built string into the code for the body

----------------------------------------------------------------------------------------------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
Dim mailMessage As New MailMessage()
'creating an instance of the MailMessage class
mailMessage.From = "xyz@mydomain.com"
'senders email address
mailMessage.To = "abc@sendersemail.com"
'recipient's email address
mailMessage.Cc = "carboncopy@sendersemail.com"
'email address of the Cc recipient
mailMessage.Bcc = "blindcarboncopy@sendersemail.com"
'email address of the Bcc recipient
mailMessage.Subject = "Hello"
'subject of the email message
mailMessage.BodyFormat = MailFormat.Text
'message text format. Can be text or html
mailMessage.Body = "This tutorial is sending email with an ASP.NET app."
'message body
mailMessage.Priority = MailPriority.Normal
'email priority. Can be low, normal or high
SmtpMail.SmtpServer = "mail.yourserver.com"
'mail server used to send this email. modify this line based on your mail server
SmtpMail.Send(mailMessage)
'using the static method "Send" of the SmtpMail class to send the mail
Response.Write("Mail sent")
'message stating the mail is sent
End Sub
----------------------------------------------------------------------------------------------------------------------

Nugs
0
 
dfaithenCommented:
You pretty much have everything you need in the above example
Note that only the items below are necessary to send an email. The rest are all extras.

Dim mailMessage As New MailMessage()
mailMessage.From = "xyz@mydomain.com"
mailMessage.To = "abc@sendersemail.com"
mailMessage.Subject = "Hello"
mailMessage.BodyFormat = MailFormat.Text
mailMessage.Body = "This tutorial is sending email with an ASP.NET app."
SmtpMail.SmtpServer = "mail.yourserver.com"
SmtpMail.Send(mailMessage)

please note that you will need to change "mail.yourserver.com" to the name of your server. If you are not routing it through an external mail server (i.e. you are sending through iis smtp on your web server.) you can probably just set it to the name of your server. If you are routing through another email server you will need to configure your username and password as well as the server address.

As for getting the concatenated string into the email

just concatenate like so...

C#
=====
string mailbody = "field1: "+field1.Text + "\n";
mailbody += "field2: "+field2.Text + "\n";
mailbody += "field3: "+field3.Text + "\n";
mailbody += "field4: "+field4.Text + "\n";

VB
=====
Dim mailbody As String =  "field1: "+field1.Text + "\n"
mailbody += "field2: "+field2.Text + "\n"
mailbody += "field3: "+field3.Text + "\n"
mailbody += "field4: "+field4.Text + "\n"

and then make your string the value for the mails body like so...

C#
=====
mailMessage.Body = mailbody;

VB
=====
mailMessage.Body = mailbody;

Let me know if this clears it up for you or if you need more detail.

Duane
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
dfaithenCommented:
Sorry,
that should be
VB
=====
mailMessage.Body = mailbody
0
 
NugsAuthor Commented:
Something like:

<script runat="server">
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim mailbody As String =  "field1: "+field1.Text + "\n"
            mailbody += "name: "+name.Text + "\n"
            mailbody += "home_address: "+home_address.Text + "\n"
            mailbody += "home_city: "+home_city.Text + "\n"
            
      Dim mailMessage As New MailMessage()
      'creating an instance of the MailMessage class
            mailMessage.From = "xyz@mydomain.com"
            'senders email address
            mailMessage.To = "duncan@tvldyn.com"
            'recipient's email address
            mailMessage.Subject = "Hello"
            'subject of the email message
            mailMessage.BodyFormat = MailFormat.Text
            'message text format. Can be text or html
            mailMessage.Body = mailbody
            'message body
            SmtpMail.SmtpServer = "mail.333.com"
            'mail server used to send this email. modify this line based on your mail server
            SmtpMail.Send(mailMessage)
            'using the static method "Send" of the SmtpMail class to send the mail
            Response.Write("Mail sent")
            'message stating the mail is sent
            

End Sub
</script>

????


I get this error:

Compiler Error Message: BC30506: Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

Source Error:

 

Line 4:  
Line 5:  <script runat="server">
Line 6:  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 
0
 
dfaithenCommented:
You have to declare the the button with the withevents variable. like such...
 Protected WithEvents Button1 As System.Web.UI.WebControls.Button
0
 
NugsAuthor Commented:
Nope still get the error:

-----------------------------------------------
<script runat="server">
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Protected WithEvents Button1 As System.Web.UI.WebControls.Button

      Dim mailbody As String =  "field1: "+field1.Text + "\n"
            mailbody += "field2: "+field2.Text + "\n"
            mailbody += "field3: "+field3.Text + "\n"
...
------------------------------------------------

0
 
dfaithenCommented:
the button should be declared outside of Sub Button1_Click()
like so...

Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


     Dim mailbody As String =  "field1: "+field1.Text + "\n"
          mailbody += "field2: "+field2.Text + "\n"
          mailbody += "field3: "+field3.Text + "\n"
0
 
NugsAuthor Commented:
Am i doing something wrong here...?

I don't get the error anymore but i clikc the button and nothing happens....

--------------------------------------------------------------------------------------------------------------------------------------------------------------
<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System.Web.Mail" %>


<script runat="server">
Protected WithEvents Button1 As System.Web.UI.WebControls.Button

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

      Dim mailbody As String =  "field1: "+field1.Text + "\n"
            mailbody += "field2: "+field2.Text + "\n"
            mailbody += "field3: "+field3.Text + "\n"
            
      Dim mailMessage As New MailMessage()
      'creating an instance of the MailMessage class
            mailMessage.From = "xyz@mydomain.com"
            'senders email address
            mailMessage.To = "duncan@tvldyn.com"
            'recipient's email address
            mailMessage.Subject = "Hello"
            'subject of the email message
            mailMessage.BodyFormat = MailFormat.Text
            'message text format. Can be text or html
            mailMessage.Body = mailbody
            'message body
            SmtpMail.SmtpServer = "mail.tvldyn.com"
            'mail server used to send this email. modify this line based on your mail server
            SmtpMail.Send(mailMessage)
            'using the static method "Send" of the SmtpMail class to send the mail
            Response.Write("Mail sent")
            'message stating the mail is sent
            

End Sub
</script>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="form1" method="post" action="" runat="server">
  <p>
    <asp:TextBox ID="field1" runat="server" />
</p>
  <p>
    <asp:TextBox ID="field2" runat="server" />
  </p>
  <p>
    <asp:TextBox ID="field3" runat="server" />
</p>
  <p>
    <input type="submit" name="Submit" value="Submit" onServerClick="Button1_Click">
  </p>
</form>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------------------------------------------
0
 
dfaithenCommented:
You should be using an button server control  <asp:Button id="Button1" runat="server" Text="Click Me"></asp:Button>
0
 
NugsAuthor Commented:
What does this mean?

Exception Details: System.Runtime.InteropServices.COMException: The message could not be sent to the SMTP server. The transport error code was 0x800ccc61. The server response was 430 You are not authorized to use this protocol
0
 
dfaithenCommented:
Here is a full working example of your code above. I have provided the code using a code behind model, because that is what I am accustomed to. I think this should clear everything up for you.

Page
=================
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="experiment4.WebForm2"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>WebForm2</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
  </head>
  <body MS_POSITIONING="GridLayout">

<form name="form1" method="post" action="" runat="server" ID="Form1">
  <p>
    <asp:textbox ID="field1" runat="server" />
</p>
  <p>
    <asp:textbox ID="field2" runat="server" />
  </p>
  <p>
    <asp:textbox ID="field3" runat="server" />
</p>
  <p>
<asp:button id="Button1" runat="server" Text="Button"></asp:button>
  </p>
</form>

  </body>
</html>

Codebehind
==============

Imports System.Web.Mail
Public Class WebForm2
    Inherits System.Web.UI.Page



#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents field1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents field2 As System.Web.UI.WebControls.TextBox
    Protected WithEvents field3 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim mailbody As String = "field1: " + field1.Text + "\n"
        mailbody += "field2: " + field2.Text + "\n"
        mailbody += "field3: " + field3.Text + "\n"

        Dim mailMessage As New MailMessage
        'creating an instance of the MailMessage class
        mailMessage.From = "xyz@mydomain.com"
        'senders email address
        mailMessage.To = "duncan@tvldyn.com"
        'recipient's email address
        mailMessage.Subject = "Hello"
        'subject of the email message
        mailMessage.BodyFormat = MailFormat.Text
        'message text format. Can be text or html
        mailMessage.Body = mailbody
        'message body
        SmtpMail.SmtpServer = "mail.tvldyn.com"
        'mail server used to send this email. modify this line based on your mail server
        SmtpMail.Send(mailMessage)
        'using the static method "Send" of the SmtpMail class to send the mail
        Response.Write("Mail sent")
        'message stating the mail is sent



    End Sub
End Class
0
 
dfaithenCommented:
Your email server account probably requires authorization for routing. Unfortunately the SmtpMail object does not provide this ability. Your options are to either send the email through iis of the machine your app is on or to purchase a third party mail component that would allow secure mail routing. Sorry I know that isnt what you want to hear.
0
 
NugsAuthor Commented:
doing it through IIS? how does that work?
0
 
dfaithenCommented:
Make sure that you have smtp installed and running in IIS.
then just change
"mail.tvldyn.com" to your server name
in the following snip
SmtpMail.SmtpServer = "mail.tvldyn.com"
0
 
NugsAuthor Commented:
Ok i think this has come down to server issues so i hope the code is working....

Thank you for your help....
0
 
dfaithenCommented:
Good luck with the rest.
Duane
0
 
NugsAuthor Commented:
Duane i wonder if you can answer one more question before i sign off on this one...

How do i add line breaks to my mailbody??
-----------------------------------------------------------------------------
      Dim mailbody As String =  "field1: "+field1.Text + "\n"
            mailbody += "field2: "+field2.Text + "\n"
            mailbody += "field3: "+field3.Text + "\n"
-----------------------------------------------------------------------------

I figured out the permissions problem, i sat down with the network admin and worked it out... But i am not recieving the body as one long string....
0
 
dfaithenCommented:
Thats what the "\n" is. This is an escaped line break.
if you are getting still getting the long string and you can see the literal \n in the string then check to make sure that you are formatting the email as text and not html. do that like this:
mailMessage.BodyFormat = MailFormat.Text


0
 
NugsAuthor Commented:
----------------------------------------------------------------------------------------------------------------------------------
mailbody += "DropDownList: "+DropDownList.SelectedValue + "\n"
            mailbody += "field3: "+field3.Text + "\n"
            
      Dim mailMessage As New MailMessage()
      'creating an instance of the MailMessage class
            mailMessage.From = ".com"
            'senders email address
            mailMessage.To = ".com"
            'recipient's email address
            mailMessage.Subject = "Helloddf"
            'subject of the email message
            mailMessage.BodyFormat = MailFormat.Text
            'message text format. Can be text or html
-----------------------------------------------------------------------------------------------------------------------------------

Yeah i am, still getting a long string with a literal \n...???
0
 
NugsAuthor Commented:
That is ok, i switched it to HTML and used "<BR>" That seems to work!
0
 
NugsAuthor Commented:
What about mailing Checkboxes? How can i get a yes or no....
0
 
dfaithenCommented:
CheckBox1.Checked
will give you true or false value. you can set your string based on that.
0
 
NugsAuthor Commented:
I get this error when the form is submitted:


------------------------------------------------------------------------------------------------------------------
Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 9:  
Line 10:       Dim mailbody As String =  "field1: "+field1.Text + "<BR>"
Line 11:             mailbody += "oTripHotel1: "+oTripHotel1.Checked + "<BR>"
Line 12:             mailbody += "field2: "+field2.Text + "<BR>"
Line 13:             mailbody += "DropDownList: "+DropDownList.SelectedValue + "<BR>"
 -------------------------------------------------------------------------------------------------------------------
0
 
dfaithenCommented:
Oops. Sorry you are working in vb so you should be using an & not a + for concatenation
0
 
NugsAuthor Commented:
I get an Syntax error with that:
---------------------------------------------------------------------------------------------------------------------------------------
Compiler Error Message: BC30035: Syntax error.

Source Error:

 Line 9:  
Line 10:       Dim mailbody As String =  "field1: "&field1.Text & "<BR>"
Line 11:             mailbody += "oTripHotel1: "&oTripHotel1.Text & "<BR>"
---------------------------------------------------------------------------------------------------------------------------------------
 
---------------------------------------------------------------------------------------------------------------------------------------
      Dim mailbody As String =  "field1: "&field1.Text & "<BR>"
            mailbody += "oTripHotel1: "&oTripHotel1.Text & "<BR>"
            mailbody += "field2: "&field2.Text & "<BR>"
            mailbody += "DropDownList: "&DropDownList.SelectedValue & "<BR>"
            mailbody += "field3: "&field3.Text & "<BR>"
---------------------------------------------------------------------------------------------------------------------------------------
0
 
dfaithenCommented:
try this...

Dim mailbody As String =  "field1: "&field1.Text & "<BR>"
          mailbody = mailbody & "oTripHotel1: "&oTripHotel1.Text & "<BR>"
          mailbody = mailbody & "field2: "&field2.Text & "<BR>"
          mailbody = mailbody & "DropDownList: "&DropDownList.SelectedValue & "<BR>"
          mailbody = mailbody & "field3: "&field3.Text & "<BR>"
0
 
NugsAuthor Commented:
Compiler Error Message: BC30035: Syntax error.

Source Error:

Line 9:  
Line 10:       Dim mailbody As String =  "field1: "&field1.Text & "<BR>"
-->Line 11:         mailbody = mailbody & "oTripHotel1: "&oTripHotel1.Text & "<BR>"
Line 12:         mailbody = mailbody & "field2: "&field2.Text & "<BR>"
Line 13:         mailbody = mailbody & "DropDownList: "&DropDownList.SelectedValue & "<BR>"
 
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 14
  • 14
Tackle projects and never again get stuck behind a technical roadblock.
Join Now