OPERATOR '&' IS NOT DEFINED

D

dancer

Can somebody tell me why I get this message with the following code?

Compiler Error Message: BC30452: Operator '&' is not defined for types
'String' and 'System.Web.UI.WebControls.TextBox'.

<html>

<head>

<% @Import Namespace="System.Web.Mail" %>

<script language="VB" runat="server">



Sub btnSendFeedback_Click(sender as Object, e as EventArgs)

'Create an instance of the MailMessage class

Dim objMM as New MailMessage()

'Set the properties - send the email to the person who filled out the

'feedback form.

objMM.To = "(e-mail address removed)"


objMM.From = "(e-mail address removed)"

'send in html format

objMM.BodyFormat = MailFormat.html

'Set the priority - options are High, Low, and Normal

objMM.Priority = MailPriority.Normal

'Set the subject

objMM.Subject = "Accident Investigation Form"


'Set the body

objMM.Body = DateTime.Now + " HI " & _

"<html>" & vbCrLf & vbCrLf & _

"<head>" & vbCrLf & vbCrLf & _

"</head>"& vbCrLf & vbCrLf & _

"<body>" & vbCrLf & vbCrLf & _

Location where accident occurred:

Location.Text & _

"<br>"& _

Check1.Text & "." & vbCrLf & vbCrLf & _

vbCrLf &vbCrLf & _

Check2.Text & vbCrLf & _

"</td>" & vbCrLf & vbCrLf & _

"</body>" & vbCrLf & vbCrLf & _

"</html>"


'Specify to use the default Smtp Server

SmtpMail.SmtpServer = ""

'Now, to send the message, use the Send method of the SmtpMail class

SmtpMail.Send(objMM)

panelSendEmail.Visible = false

panelMailSent.Visible = true

End Sub



</script>

</head>


<body>

<table width="750" bgcolor="#E9EDF4" table border="1" cellpadding="3">

<h3><center><font face="Verdana">Wheeler's Accident Investigation
Form</font></h3>



<hr>

<%--____________________________________________________________________________--%>

<%--ROW 1--%>


<td width="250" valign="top">


<form runat="server">

<font face="Verdana" Size="2">

Location where accident occurred: <asp:textbox id="Location" runat=server
Width="200"/>

</td>

<td align="right" valign="top" width="225">


<font face="Verdana" Size="2">Employer's Premises



<asp:CheckBox id=Check1 Text="yes" runat="server" />

<asp:CheckBox id=Check2 Text="no" runat="server" />

<br>


Job site

<asp:CheckBox id=Check3 Text="yes" runat="server" />

<asp:CheckBox id=Check4 Text="no" runat="server" />


</td>

<td>

<font face="Verdana" Size="2">Date of accident</font><br>

<asp:textbox id="Date" runat=server Width="100"/>


</td>

<%--____________________________________________________________________________--%>

<%--ROW 2--%>


<tr> <td>

<font face="Verdana" Size="2">Who was injured?</font><br>

<asp:textbox id="Who" runat=server width="200"/>

</td>

<td align= "left">

<font face="Verdana" Size="2">Employee <asp:CheckBox id=Check5
runat="server" />

<br>

Non-employee <asp:CheckBox id=Check6 runat="server" />


</td>

<td align="right"><font face="Verdana" Size="2">

Time of accident a.m. <asp:textbox id="am" runat=server Width="90"/>

p.m. <asp:textbox id="pm" runat=server Width="90"/>

</TD>

<%--____________________________________________________________________________--%>

<%--ROW 3--%>

<tr>

<td>

<font face="Verdana" Size="2">Date of Hire <br> <asp:textbox id="Hiredate"
runat=server/>

</td>

<td>

<font face="Verdana" Size="2">Job Title or Occupation <br><asp:textbox
id="occupation" runat=server width="200"/>

</td>

<td>

<font face="Verdana" Size="2">How long has employee worked at job where
injury occurred?<br><asp:textbox id="lengthofjob" runat=server/>

</td>

<%--____________________________________________________________________________--%>

<%--Row 4--%>

&nbsp&nbsp



<p>


<asp:Label id=Label1 font-name="arial" font-size="10pt" runat="server"/>

<!Copied from /smirnof>

<asp:panel id="panelSendEmail" runat="server">





<%--asp:textbox id= runat="server" /--%>

<br>

<b>Your Message:</b><br>

<%--asp:textbox id="txtMessage" TextMode="MultiLine"

Columns="40" Rows="10" runat="server" /--%>

<p>



<asp:button runat="server" id="btnSendFeedback" Text="Send

Feedback!"

OnClick="btnSendFeedback_Click" />



</asp:panel>

<asp:panel id="panelMailSent" runat="server" Visible="False">

An email has been sent to the email address you specified.

Thanks!

</asp:panel>

</form>

</body>

</html>
 
M

Mark Rae

Can somebody tell me why I get this message with the following code?

Compiler Error Message: BC30452: Operator '&' is not defined for types
'String' and 'System.Web.UI.WebControls.TextBox'.

Normally that means you're trying to use the contents of an <asp:TextBox>
without specifying its .Text property

e.g. Location instead of Location.Text

However, I'm wondering if it might be because you've forgotten the double
quotes here:
 
P

Patrice

Location where accident occurred: should be "Location where accident
occured:" & _

And as you have a Location textbox the compiler see the location variable
and as you can't add a string and a textbox....
 
J

Juan T. Llibre

You don't need to set the html/head/body tags within the HTML body
....and, in the VbCrLf thread, we established that you can't use VbCrLf in HTML.

Something else : you *must* put all the HTML content in ONE line.
You can't break the lines with code.

Get rid of all the "VbCrLf" and all the "&" and write all your HTML in a single line,
line-breaking the body's content with "<br />" or "<P> ... </P>" as needed.

Also, you can't insert Textbox controls into your HTML.

So, Check1.Text is out, too.

If you want to reference their content, after a user fills them out,
capture it to a variable and use that, before writing to the body.

insert :

Dim Check1 as String = Check1.Text
Dim Check2 as String = Check2.Text
Dim Location as String = Location.Text
before "Dim objMM as New MailMessage()"

and, when composing the body, use :

mail.Body="Hi, <p>The location where the accident occurred is :" & Location & "<br/>" & Check1 & "."
' etc.

You *can* use the & to reference text variables.
Notice the location of the quote marks.





Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
dancer said:
Can somebody tell me why I get this message with the following code?

Compiler Error Message: BC30452: Operator '&' is not defined for types 'String' and
'System.Web.UI.WebControls.TextBox'.

<html>

<head>

<% @Import Namespace="System.Web.Mail" %>

<script language="VB" runat="server">



Sub btnSendFeedback_Click(sender as Object, e as EventArgs)

'Create an instance of the MailMessage class

Dim objMM as New MailMessage()

'Set the properties - send the email to the person who filled out the

'feedback form.

objMM.To = "(e-mail address removed)"


objMM.From = "(e-mail address removed)"

'send in html format

objMM.BodyFormat = MailFormat.html

'Set the priority - options are High, Low, and Normal

objMM.Priority = MailPriority.Normal

'Set the subject

objMM.Subject = "Accident Investigation Form"


'Set the body

objMM.Body = DateTime.Now + " HI " & _

"<html>" & vbCrLf & vbCrLf & _

"<head>" & vbCrLf & vbCrLf & _

"</head>"& vbCrLf & vbCrLf & _

"<body>" & vbCrLf & vbCrLf & _

Location where accident occurred:

Location.Text & _

"<br>"& _
7> Check1.Text & "." & vbCrLf & vbCrLf & _
 
D

dancer

Hi Juan,

What is the difference in what I am trying to do and this? Why can THEY
break up the HTML content into different lines?

email_html.aspx
--------------------------------------------------------------------------------

<%@ Page Language="VB" ClientTarget="downlevel" %>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat="server">
Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
Dim myMessage As New MailMessage
Dim myMail As SmtpMail
Dim strEmail As String

If Page.IsValid() Then
strEmail = txtEmail.Text

'myMessage.From = "webmaster@" & Request.ServerVariables("SERVER_NAME")
myMessage.From = "(e-mail address removed)"
myMessage.To = strEmail
myMessage.Subject = "E-mail Sample (HTML) from ASP 101!"

' This is the magic line. Without this the message will just appear
' as plain HTML and won't be rendered by the recipient's email client.
' It'd be as if they did "View Source" on a web page.
MyMessage.BodyFormat = MailFormat.Html

' This is multi-lined simply for readability.
' Notice that it is a properly formatted HTML
' message and not just plain text like most email.
' A lot of people have asked how to use form data
' in the emails so I added an example of including
' the entered address in the body of the email.
myMessage.Body = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0
Transitional//EN"">" & vbCrLf _
& "<html>" & vbCrLf _
& "<head>" & vbCrLf _
& " <title>Sample Message From ASP 101</title>" & vbCrLf _
& " <meta http-equiv=Content-Type content=""text/html;
charset=iso-8859-1"">" & vbCrLf _
& "</head>" & vbCrLf _
& "<body bgcolor=""#FFFFCC"">" & vbCrLf _
& " <h2>Sample Message From ASP 101</h2>" & vbCrLf _
& " <p>" & vbCrLf _
& " This message was sent from a sample at" & vbCrLf _
& " <a href=""http://www.asp101.com"">ASP 101</a>." & vbCrLf _
& " It is used to show people how to send HTML" & vbCrLf _
& " formatted email from an ASP.NET page." & vbCrLf _
& " If you did not request this email yourself," & vbCrLf _
& " your address was entered by one of our" & vbCrLf _
& " visitors." & vbCrLf _
& " <strong>" & vbCrLf _
& " We do not store these e-mail addresses." & vbCrLf _
& " </strong>" & vbCrLf _
& " </p>" & vbCrLf _
& " <p><font size=""-1"">Please address all concerns to
(e-mail address removed).</font></p>" & vbCrLf _
& " <p><font size=""-1"">This message was sent to: " & strEmail &
"</font></p>" & vbCrLf _
& "</body>" & vbCrLf _
& "</html>" & vbCrLf

' Doesn't have to be local... just enter your
' SMTP server's name or ip address!
myMail.SmtpServer = "localhost"
myMail.Send(myMessage)

frmEmail.Visible = False
lblUserMessage.Text = "An HTML-formatted email message has been sent to "
& strEmail & "."
End If
End Sub
</script>

<html>
<head>
<title>ASP.NET Email (HTML Format) Sample</title>
</head>
<body>

<asp:Label id="lblUserMessage" text="Enter your e-mail address:"
runat="server" />
<form method="post" id="frmEmail" runat="server">
<asp:TextBox id="txtEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validEmailRequired" ControlToValidate="txtEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validEmailRegExp" ControlToValidate="txtEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />

<asp:Button id="btnSendMail" text="Send Mail!"
OnClick="btnSendMail_OnClick" runat="server" />
</form>

<hr />

<p>
Click <a href="http://www.asp101.com/samples/email_html_aspx.asp">here</a>
to read about and download the source code.
</p>

</body>
</html>


--------------------------------------------------------------------------------
 
M

Mark Rae

As an aside, you appear to have attached a binary file to your post - my
newsreader is configured not to download posts with binary attachments,
which means that I (and anyone else who has also set this configuration
option) won't see your post...
 
D

dancer

Mark,
Can you see this? I didn't include the logo on the page.

I am asking Juan what is the difference in what I am trying to accomplish
and what they are doing. Why can THEY break the HTML content into different
lines?



<%@ Page Language="VB" ClientTarget="downlevel" %>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat="server">
Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
Dim myMessage As New MailMessage
Dim myMail As SmtpMail
Dim strEmail As String

If Page.IsValid() Then
strEmail = txtEmail.Text

'myMessage.From = "webmaster@" & Request.ServerVariables("SERVER_NAME")
myMessage.From = "(e-mail address removed)"
myMessage.To = strEmail
myMessage.Subject = "E-mail Sample (HTML) from ASP 101!"

' This is the magic line. Without this the message will just appear
' as plain HTML and won't be rendered by the recipient's email client.
' It'd be as if they did "View Source" on a web page.
MyMessage.BodyFormat = MailFormat.Html

' This is multi-lined simply for readability.
' Notice that it is a properly formatted HTML
' message and not just plain text like most email.
' A lot of people have asked how to use form data
' in the emails so I added an example of including
' the entered address in the body of the email.
myMessage.Body = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0
Transitional//EN"">" & vbCrLf _
& "<html>" & vbCrLf _
& "<head>" & vbCrLf _
& " <title>Sample Message From ASP 101</title>" & vbCrLf _
& " <meta http-equiv=Content-Type content=""text/html;
charset=iso-8859-1"">" & vbCrLf _
& "</head>" & vbCrLf _
& "<body bgcolor=""#FFFFCC"">" & vbCrLf _
& " <h2>Sample Message From ASP 101</h2>" & vbCrLf _
& " <p>" & vbCrLf _
& " This message was sent from a sample at" & vbCrLf _
& " <a href=""http://www.asp101.com"">ASP 101</a>." & vbCrLf _
& " It is used to show people how to send HTML" & vbCrLf _
& " formatted email from an ASP.NET page." & vbCrLf _
& " If you did not request this email yourself," & vbCrLf _
& " your address was entered by one of our" & vbCrLf _
& " visitors." & vbCrLf _
& " <strong>" & vbCrLf _
& " We do not store these e-mail addresses." & vbCrLf _
& " </strong>" & vbCrLf _
& " </p>" & vbCrLf _
& " <p><font size=""-1"">Please address all concerns to
(e-mail address removed).</font></p>" & vbCrLf _
& " <p><font size=""-1"">This message was sent to: " & strEmail &
"</font></p>" & vbCrLf _
& "</body>" & vbCrLf _
& "</html>" & vbCrLf

' Doesn't have to be local... just enter your
' SMTP server's name or ip address!
myMail.SmtpServer = "localhost"
myMail.Send(myMessage)

frmEmail.Visible = False
lblUserMessage.Text = "An HTML-formatted email message has been sent to "
& strEmail & "."
End If
End Sub
</script>

<html>
<head>
<title>ASP.NET Email (HTML Format) Sample</title>
</head>
<body>

<asp:Label id="lblUserMessage" text="Enter your e-mail address:"
runat="server" />
<form method="post" id="frmEmail" runat="server">
<asp:TextBox id="txtEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validEmailRequired" ControlToValidate="txtEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validEmailRegExp" ControlToValidate="txtEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />

<asp:Button id="btnSendMail" text="Send Mail!"
OnClick="btnSendMail_OnClick" runat="server" />
</form>

<hr />

<p>
Click <a href="http://www.asp101.com/samples/email_html_aspx.asp">here</a>
to read about and download the source code.
</p>

</body>
</html>
 
D

David

The vbCrLf are purely to ensure that the line is not too long when
generated. There is a problem with over long lines that when they do
eventually wrap, it can disrupt your formatted email.

You will note that there are no br tags, but there are p tags <p> and </p>.
These are what cause <P>aragraph breaks. (Actually, they are for new
paragraphs rather than breaking paragraphs).

Does this answer your question?

(Note, you probably can't press "Yes" below as you probably can't see it. If
you can, then you have better eyes than me, as I can't see it. :)

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

dancer said:
Hi Juan,

What is the difference in what I am trying to do and this? Why can THEY
break up the HTML content into different lines?

email_html.aspx
--------------------------------------------------------------------------------

<%@ Page Language="VB" ClientTarget="downlevel" %>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat="server">
Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
Dim myMessage As New MailMessage
Dim myMail As SmtpMail
Dim strEmail As String

If Page.IsValid() Then
strEmail = txtEmail.Text

'myMessage.From = "webmaster@" & Request.ServerVariables("SERVER_NAME")
myMessage.From = "(e-mail address removed)"
myMessage.To = strEmail
myMessage.Subject = "E-mail Sample (HTML) from ASP 101!"

' This is the magic line. Without this the message will just appear
' as plain HTML and won't be rendered by the recipient's email client.
' It'd be as if they did "View Source" on a web page.
MyMessage.BodyFormat = MailFormat.Html

' This is multi-lined simply for readability.
' Notice that it is a properly formatted HTML
' message and not just plain text like most email.
' A lot of people have asked how to use form data
' in the emails so I added an example of including
' the entered address in the body of the email.
myMessage.Body = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0
Transitional//EN"">" & vbCrLf _
& "<html>" & vbCrLf _
& "<head>" & vbCrLf _
& " <title>Sample Message From ASP 101</title>" & vbCrLf _
& " <meta http-equiv=Content-Type content=""text/html;
charset=iso-8859-1"">" & vbCrLf _
& "</head>" & vbCrLf _
& "<body bgcolor=""#FFFFCC"">" & vbCrLf _
& " <h2>Sample Message From ASP 101</h2>" & vbCrLf _
& " <p>" & vbCrLf _
& " This message was sent from a sample at" & vbCrLf _
& " <a href=""http://www.asp101.com"">ASP 101</a>." & vbCrLf _
& " It is used to show people how to send HTML" & vbCrLf _
& " formatted email from an ASP.NET page." & vbCrLf _
& " If you did not request this email yourself," & vbCrLf _
& " your address was entered by one of our" & vbCrLf _
& " visitors." & vbCrLf _
& " <strong>" & vbCrLf _
& " We do not store these e-mail addresses." & vbCrLf _
& " </strong>" & vbCrLf _
& " </p>" & vbCrLf _
& " <p><font size=""-1"">Please address all concerns to
(e-mail address removed).</font></p>" & vbCrLf _
& " <p><font size=""-1"">This message was sent to: " & strEmail &
"</font></p>" & vbCrLf _
& "</body>" & vbCrLf _
& "</html>" & vbCrLf

' Doesn't have to be local... just enter your
' SMTP server's name or ip address!
myMail.SmtpServer = "localhost"
myMail.Send(myMessage)

frmEmail.Visible = False
lblUserMessage.Text = "An HTML-formatted email message has been sent to "
& strEmail & "."
End If
End Sub
</script>

<html>
<head>
<title>ASP.NET Email (HTML Format) Sample</title>
</head>
<body>

<asp:Label id="lblUserMessage" text="Enter your e-mail address:"
runat="server" />
<form method="post" id="frmEmail" runat="server">
<asp:TextBox id="txtEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validEmailRequired" ControlToValidate="txtEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validEmailRegExp" ControlToValidate="txtEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />

<asp:Button id="btnSendMail" text="Send Mail!"
OnClick="btnSendMail_OnClick" runat="server" />
</form>

<hr />

<p>
Click <a href="http://www.asp101.com/samples/email_html_aspx.asp">here</a>
to read about and download the source code.
</p>

</body>
</html>


--------------------------------------------------------------------------------

Juan T. Llibre said:
You don't need to set the html/head/body tags within the HTML body
...and, in the VbCrLf thread, we established that you can't use VbCrLf in
HTML.

Something else : you *must* put all the HTML content in ONE line.
You can't break the lines with code.

Get rid of all the "VbCrLf" and all the "&" and write all your HTML in a
single line,
line-breaking the body's content with "<br />" or "<P> ... </P>" as
needed.

Also, you can't insert Textbox controls into your HTML.

So, Check1.Text is out, too.

If you want to reference their content, after a user fills them out,
capture it to a variable and use that, before writing to the body.

insert :

Dim Check1 as String = Check1.Text
Dim Check2 as String = Check2.Text
Dim Location as String = Location.Text
before "Dim objMM as New MailMessage()"

and, when composing the body, use :

mail.Body="Hi, <p>The location where the accident occurred is :" &
Location & "<br/>" & Check1 & "."
' etc.

You *can* use the & to reference text variables.
Notice the location of the quote marks.





Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================

7> Check1.Text & "." & vbCrLf & vbCrLf & _
 
D

dancer

David,

Not exactly does this answer my question. Could you please check out Juan's
answer to my first post on this subject and help me understand, since I was
copying my code from this code.

Thank you.


David said:
The vbCrLf are purely to ensure that the line is not too long when
generated. There is a problem with over long lines that when they do
eventually wrap, it can disrupt your formatted email.

You will note that there are no br tags, but there are p tags <p> and
</p>. These are what cause <P>aragraph breaks. (Actually, they are for new
paragraphs rather than breaking paragraphs).

Does this answer your question?

(Note, you probably can't press "Yes" below as you probably can't see it.
If you can, then you have better eyes than me, as I can't see it. :)

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

dancer said:
Hi Juan,

What is the difference in what I am trying to do and this? Why can THEY
break up the HTML content into different lines?

email_html.aspx
--------------------------------------------------------------------------------

<%@ Page Language="VB" ClientTarget="downlevel" %>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat="server">
Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
Dim myMessage As New MailMessage
Dim myMail As SmtpMail
Dim strEmail As String

If Page.IsValid() Then
strEmail = txtEmail.Text

'myMessage.From = "webmaster@" & Request.ServerVariables("SERVER_NAME")
myMessage.From = "(e-mail address removed)"
myMessage.To = strEmail
myMessage.Subject = "E-mail Sample (HTML) from ASP 101!"

' This is the magic line. Without this the message will just appear
' as plain HTML and won't be rendered by the recipient's email client.
' It'd be as if they did "View Source" on a web page.
MyMessage.BodyFormat = MailFormat.Html

' This is multi-lined simply for readability.
' Notice that it is a properly formatted HTML
' message and not just plain text like most email.
' A lot of people have asked how to use form data
' in the emails so I added an example of including
' the entered address in the body of the email.
myMessage.Body = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0
Transitional//EN"">" & vbCrLf _
& "<html>" & vbCrLf _
& "<head>" & vbCrLf _
& " <title>Sample Message From ASP 101</title>" & vbCrLf _
& " <meta http-equiv=Content-Type content=""text/html;
charset=iso-8859-1"">" & vbCrLf _
& "</head>" & vbCrLf _
& "<body bgcolor=""#FFFFCC"">" & vbCrLf _
& " <h2>Sample Message From ASP 101</h2>" & vbCrLf _
& " <p>" & vbCrLf _
& " This message was sent from a sample at" & vbCrLf _
& " <a href=""http://www.asp101.com"">ASP 101</a>." & vbCrLf _
& " It is used to show people how to send HTML" & vbCrLf _
& " formatted email from an ASP.NET page." & vbCrLf _
& " If you did not request this email yourself," & vbCrLf _
& " your address was entered by one of our" & vbCrLf _
& " visitors." & vbCrLf _
& " <strong>" & vbCrLf _
& " We do not store these e-mail addresses." & vbCrLf _
& " </strong>" & vbCrLf _
& " </p>" & vbCrLf _
& " <p><font size=""-1"">Please address all concerns to
(e-mail address removed).</font></p>" & vbCrLf _
& " <p><font size=""-1"">This message was sent to: " & strEmail &
"</font></p>" & vbCrLf _
& "</body>" & vbCrLf _
& "</html>" & vbCrLf

' Doesn't have to be local... just enter your
' SMTP server's name or ip address!
myMail.SmtpServer = "localhost"
myMail.Send(myMessage)

frmEmail.Visible = False
lblUserMessage.Text = "An HTML-formatted email message has been sent to "
& strEmail & "."
End If
End Sub
</script>

<html>
<head>
<title>ASP.NET Email (HTML Format) Sample</title>
</head>
<body>

<asp:Label id="lblUserMessage" text="Enter your e-mail address:"
runat="server" />
<form method="post" id="frmEmail" runat="server">
<asp:TextBox id="txtEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validEmailRequired" ControlToValidate="txtEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validEmailRegExp" ControlToValidate="txtEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />

<asp:Button id="btnSendMail" text="Send Mail!"
OnClick="btnSendMail_OnClick" runat="server" />
</form>

<hr />

<p>
Click <a
href="http://www.asp101.com/samples/email_html_aspx.asp">here</a> to read
about and download the source code.
</p>

</body>
</html>


--------------------------------------------------------------------------------

Juan T. Llibre said:
You don't need to set the html/head/body tags within the HTML body
...and, in the VbCrLf thread, we established that you can't use VbCrLf
in HTML.

Something else : you *must* put all the HTML content in ONE line.
You can't break the lines with code.

Get rid of all the "VbCrLf" and all the "&" and write all your HTML in a
single line,
line-breaking the body's content with "<br />" or "<P> ... </P>" as
needed.

Also, you can't insert Textbox controls into your HTML.

So, Check1.Text is out, too.

If you want to reference their content, after a user fills them out,
capture it to a variable and use that, before writing to the body.

insert :

Dim Check1 as String = Check1.Text
Dim Check2 as String = Check2.Text
Dim Location as String = Location.Text
before "Dim objMM as New MailMessage()"

and, when composing the body, use :

mail.Body="Hi, <p>The location where the accident occurred is :" &
Location & "<br/>" & Check1 & "."
' etc.

You *can* use the & to reference text variables.
Notice the location of the quote marks.





Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
Can somebody tell me why I get this message with the following code?

Compiler Error Message: BC30452: Operator '&' is not defined for types
'String' and
'System.Web.UI.WebControls.TextBox'.

<html>

<head>

<% @Import Namespace="System.Web.Mail" %>

<script language="VB" runat="server">



Sub btnSendFeedback_Click(sender as Object, e as EventArgs)

'Create an instance of the MailMessage class

Dim objMM as New MailMessage()

'Set the properties - send the email to the person who filled out the

'feedback form.

objMM.To = "(e-mail address removed)"


objMM.From = "(e-mail address removed)"

'send in html format

objMM.BodyFormat = MailFormat.html

'Set the priority - options are High, Low, and Normal

objMM.Priority = MailPriority.Normal

'Set the subject

objMM.Subject = "Accident Investigation Form"


'Set the body

objMM.Body = DateTime.Now + " HI " & _

"<html>" & vbCrLf & vbCrLf & _

"<head>" & vbCrLf & vbCrLf & _

"</head>"& vbCrLf & vbCrLf & _

"<body>" & vbCrLf & vbCrLf & _

Location where accident occurred:

Location.Text & _

"<br>"& _

7> Check1.Text & "." & vbCrLf & vbCrLf & _

vbCrLf &vbCrLf & _

Check2.Text & vbCrLf & _

"</td>" & vbCrLf & vbCrLf & _

"</body>" & vbCrLf & vbCrLf & _

"</html>"


'Specify to use the default Smtp Server

SmtpMail.SmtpServer = ""

'Now, to send the message, use the Send method of the SmtpMail class

SmtpMail.Send(objMM)

panelSendEmail.Visible = false

panelMailSent.Visible = true

End Sub



</script>

</head>


<body>

<table width="750" bgcolor="#E9EDF4" table border="1" cellpadding="3">

<h3><center><font face="Verdana">Wheeler's Accident Investigation
Form</font></h3>



<hr>

<%--____________________________________________________________________________--%>

<%--ROW 1--%>


<td width="250" valign="top">


<form runat="server">

<font face="Verdana" Size="2">

Location where accident occurred: <asp:textbox id="Location"
runat=server Width="200"/>

</td>

<td align="right" valign="top" width="225">


<font face="Verdana" Size="2">Employer's Premises



<asp:CheckBox id=Check1 Text="yes" runat="server" />

<asp:CheckBox id=Check2 Text="no" runat="server" />

<br>


Job site

<asp:CheckBox id=Check3 Text="yes" runat="server" />

<asp:CheckBox id=Check4 Text="no" runat="server" />


</td>

<td>

<font face="Verdana" Size="2">Date of accident</font><br>

<asp:textbox id="Date" runat=server Width="100"/>


</td>

<%--____________________________________________________________________________--%>

<%--ROW 2--%>


<tr> <td>

<font face="Verdana" Size="2">Who was injured?</font><br>

<asp:textbox id="Who" runat=server width="200"/>

</td>

<td align= "left">

<font face="Verdana" Size="2">Employee <asp:CheckBox id=Check5
runat="server" />

<br>

Non-employee <asp:CheckBox id=Check6 runat="server" />


</td>

<td align="right"><font face="Verdana" Size="2">

Time of accident a.m. <asp:textbox id="am" runat=server Width="90"/>

p.m. <asp:textbox id="pm" runat=server Width="90"/>

</TD>

<%--____________________________________________________________________________--%>

<%--ROW 3--%>

<tr>

<td>

<font face="Verdana" Size="2">Date of Hire <br> <asp:textbox
id="Hiredate" runat=server/>

</td>

<td>

<font face="Verdana" Size="2">Job Title or Occupation <br><asp:textbox
id="occupation"
runat=server width="200"/>

</td>

<td>

<font face="Verdana" Size="2">How long has employee worked at job where
injury
occurred?<br><asp:textbox id="lengthofjob" runat=server/>

</td>

<%--____________________________________________________________________________--%>

<%--Row 4--%>

&nbsp&nbsp



<p>


<asp:Label id=Label1 font-name="arial" font-size="10pt"
runat="server"/>

<!Copied from /smirnof>

<asp:panel id="panelSendEmail" runat="server">





<%--asp:textbox id= runat="server" /--%>

<br>

<b>Your Message:</b><br>

<%--asp:textbox id="txtMessage" TextMode="MultiLine"

Columns="40" Rows="10" runat="server" /--%>

<p>



<asp:button runat="server" id="btnSendFeedback" Text="Send

Feedback!"

OnClick="btnSendFeedback_Click" />



</asp:panel>

<asp:panel id="panelMailSent" runat="server" Visible="False">

An email has been sent to the email address you specified.

Thanks!

</asp:panel>

</form>

</body>

</html>
 
D

David

Ah,

Sorry, I have my newsreader on to ignore read messages.

The P tags break the lines in the rendered page. BR tags also break lines
(work as a hard wrap).

vbCrLf is a Visual Basic Carriage Return Line Feed. All this does is allow
the source html wrap onto a new line, so that your line is not too long.

If you want to use the same thing in C#, then it is something like...

email.Body = "This is my email body\nNow I am on a new line\nNote the
backslash and n to create a new line\n. I can't remember though, you might
need \r\n to create a new line";

backslash n.

Is that what you looking for? This however doesn't affect your rendered
output. use \n where there is vbcrlf, however, vbcrlf is a defined constant,
and \n has to be in quotes.

What you could do though is...

string vbCrLf = "\n";

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
dancer said:
David,

Not exactly does this answer my question. Could you please check out
Juan's answer to my first post on this subject and help me understand,
since I was copying my code from this code.

Thank you.


David said:
The vbCrLf are purely to ensure that the line is not too long when
generated. There is a problem with over long lines that when they do
eventually wrap, it can disrupt your formatted email.

You will note that there are no br tags, but there are p tags <p> and
</p>. These are what cause <P>aragraph breaks. (Actually, they are for
new paragraphs rather than breaking paragraphs).

Does this answer your question?

(Note, you probably can't press "Yes" below as you probably can't see it.
If you can, then you have better eyes than me, as I can't see it. :)

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

dancer said:
Hi Juan,

What is the difference in what I am trying to do and this? Why can THEY
break up the HTML content into different lines?

email_html.aspx
--------------------------------------------------------------------------------

<%@ Page Language="VB" ClientTarget="downlevel" %>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat="server">
Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
Dim myMessage As New MailMessage
Dim myMail As SmtpMail
Dim strEmail As String

If Page.IsValid() Then
strEmail = txtEmail.Text

'myMessage.From = "webmaster@" &
Request.ServerVariables("SERVER_NAME")
myMessage.From = "(e-mail address removed)"
myMessage.To = strEmail
myMessage.Subject = "E-mail Sample (HTML) from ASP 101!"

' This is the magic line. Without this the message will just appear
' as plain HTML and won't be rendered by the recipient's email client.
' It'd be as if they did "View Source" on a web page.
MyMessage.BodyFormat = MailFormat.Html

' This is multi-lined simply for readability.
' Notice that it is a properly formatted HTML
' message and not just plain text like most email.
' A lot of people have asked how to use form data
' in the emails so I added an example of including
' the entered address in the body of the email.
myMessage.Body = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0
Transitional//EN"">" & vbCrLf _
& "<html>" & vbCrLf _
& "<head>" & vbCrLf _
& " <title>Sample Message From ASP 101</title>" & vbCrLf _
& " <meta http-equiv=Content-Type content=""text/html;
charset=iso-8859-1"">" & vbCrLf _
& "</head>" & vbCrLf _
& "<body bgcolor=""#FFFFCC"">" & vbCrLf _
& " <h2>Sample Message From ASP 101</h2>" & vbCrLf _
& " <p>" & vbCrLf _
& " This message was sent from a sample at" & vbCrLf _
& " <a href=""http://www.asp101.com"">ASP 101</a>." & vbCrLf _
& " It is used to show people how to send HTML" & vbCrLf _
& " formatted email from an ASP.NET page." & vbCrLf _
& " If you did not request this email yourself," & vbCrLf _
& " your address was entered by one of our" & vbCrLf _
& " visitors." & vbCrLf _
& " <strong>" & vbCrLf _
& " We do not store these e-mail addresses." & vbCrLf _
& " </strong>" & vbCrLf _
& " </p>" & vbCrLf _
& " <p><font size=""-1"">Please address all concerns to
(e-mail address removed).</font></p>" & vbCrLf _
& " <p><font size=""-1"">This message was sent to: " & strEmail &
"</font></p>" & vbCrLf _
& "</body>" & vbCrLf _
& "</html>" & vbCrLf

' Doesn't have to be local... just enter your
' SMTP server's name or ip address!
myMail.SmtpServer = "localhost"
myMail.Send(myMessage)

frmEmail.Visible = False
lblUserMessage.Text = "An HTML-formatted email message has been sent to
" & strEmail & "."
End If
End Sub
</script>

<html>
<head>
<title>ASP.NET Email (HTML Format) Sample</title>
</head>
<body>

<asp:Label id="lblUserMessage" text="Enter your e-mail address:"
runat="server" />
<form method="post" id="frmEmail" runat="server">
<asp:TextBox id="txtEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validEmailRequired" ControlToValidate="txtEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validEmailRegExp" ControlToValidate="txtEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />

<asp:Button id="btnSendMail" text="Send Mail!"
OnClick="btnSendMail_OnClick" runat="server" />
</form>

<hr />

<p>
Click <a
href="http://www.asp101.com/samples/email_html_aspx.asp">here</a> to
read about and download the source code.
</p>

</body>
</html>


--------------------------------------------------------------------------------

You don't need to set the html/head/body tags within the HTML body
...and, in the VbCrLf thread, we established that you can't use VbCrLf
in HTML.

Something else : you *must* put all the HTML content in ONE line.
You can't break the lines with code.

Get rid of all the "VbCrLf" and all the "&" and write all your HTML in
a single line,
line-breaking the body's content with "<br />" or "<P> ... </P>" as
needed.

Also, you can't insert Textbox controls into your HTML.

So, Check1.Text is out, too.

If you want to reference their content, after a user fills them out,
capture it to a variable and use that, before writing to the body.

insert :

Dim Check1 as String = Check1.Text
Dim Check2 as String = Check2.Text
Dim Location as String = Location.Text
before "Dim objMM as New MailMessage()"

and, when composing the body, use :

mail.Body="Hi, <p>The location where the accident occurred is :" &
Location & "<br/>" & Check1 & "."
' etc.

You *can* use the & to reference text variables.
Notice the location of the quote marks.





Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
Can somebody tell me why I get this message with the following code?

Compiler Error Message: BC30452: Operator '&' is not defined for types
'String' and
'System.Web.UI.WebControls.TextBox'.

<html>

<head>

<% @Import Namespace="System.Web.Mail" %>

<script language="VB" runat="server">



Sub btnSendFeedback_Click(sender as Object, e as EventArgs)

'Create an instance of the MailMessage class

Dim objMM as New MailMessage()

'Set the properties - send the email to the person who filled out the

'feedback form.

objMM.To = "(e-mail address removed)"


objMM.From = "(e-mail address removed)"

'send in html format

objMM.BodyFormat = MailFormat.html

'Set the priority - options are High, Low, and Normal

objMM.Priority = MailPriority.Normal

'Set the subject

objMM.Subject = "Accident Investigation Form"


'Set the body

objMM.Body = DateTime.Now + " HI " & _

"<html>" & vbCrLf & vbCrLf & _

"<head>" & vbCrLf & vbCrLf & _

"</head>"& vbCrLf & vbCrLf & _

"<body>" & vbCrLf & vbCrLf & _

Location where accident occurred:

Location.Text & _

"<br>"& _

7> Check1.Text & "." & vbCrLf & vbCrLf & _

vbCrLf &vbCrLf & _

Check2.Text & vbCrLf & _

"</td>" & vbCrLf & vbCrLf & _

"</body>" & vbCrLf & vbCrLf & _

"</html>"


'Specify to use the default Smtp Server

SmtpMail.SmtpServer = ""

'Now, to send the message, use the Send method of the SmtpMail class

SmtpMail.Send(objMM)

panelSendEmail.Visible = false

panelMailSent.Visible = true

End Sub



</script>

</head>


<body>

<table width="750" bgcolor="#E9EDF4" table border="1" cellpadding="3">

<h3><center><font face="Verdana">Wheeler's Accident Investigation
Form</font></h3>



<hr>

<%--____________________________________________________________________________--%>

<%--ROW 1--%>


<td width="250" valign="top">


<form runat="server">

<font face="Verdana" Size="2">

Location where accident occurred: <asp:textbox id="Location"
runat=server Width="200"/>

</td>

<td align="right" valign="top" width="225">


<font face="Verdana" Size="2">Employer's Premises



<asp:CheckBox id=Check1 Text="yes" runat="server" />

<asp:CheckBox id=Check2 Text="no" runat="server" />

<br>


Job site

<asp:CheckBox id=Check3 Text="yes" runat="server" />

<asp:CheckBox id=Check4 Text="no" runat="server" />


</td>

<td>

<font face="Verdana" Size="2">Date of accident</font><br>

<asp:textbox id="Date" runat=server Width="100"/>


</td>

<%--____________________________________________________________________________--%>

<%--ROW 2--%>


<tr> <td>

<font face="Verdana" Size="2">Who was injured?</font><br>

<asp:textbox id="Who" runat=server width="200"/>

</td>

<td align= "left">

<font face="Verdana" Size="2">Employee <asp:CheckBox id=Check5
runat="server" />

<br>

Non-employee <asp:CheckBox id=Check6 runat="server" />


</td>

<td align="right"><font face="Verdana" Size="2">

Time of accident a.m. <asp:textbox id="am" runat=server Width="90"/>

p.m. <asp:textbox id="pm" runat=server Width="90"/>

</TD>

<%--____________________________________________________________________________--%>

<%--ROW 3--%>

<tr>

<td>

<font face="Verdana" Size="2">Date of Hire <br> <asp:textbox
id="Hiredate" runat=server/>

</td>

<td>

<font face="Verdana" Size="2">Job Title or Occupation <br><asp:textbox
id="occupation"
runat=server width="200"/>

</td>

<td>

<font face="Verdana" Size="2">How long has employee worked at job
where injury
occurred?<br><asp:textbox id="lengthofjob" runat=server/>

</td>

<%--____________________________________________________________________________--%>

<%--Row 4--%>

&nbsp&nbsp



<p>


<asp:Label id=Label1 font-name="arial" font-size="10pt"
runat="server"/>

<!Copied from /smirnof>

<asp:panel id="panelSendEmail" runat="server">





<%--asp:textbox id= runat="server" /--%>

<br>

<b>Your Message:</b><br>

<%--asp:textbox id="txtMessage" TextMode="MultiLine"

Columns="40" Rows="10" runat="server" /--%>

<p>



<asp:button runat="server" id="btnSendFeedback" Text="Send

Feedback!"

OnClick="btnSendFeedback_Click" />



</asp:panel>

<asp:panel id="panelMailSent" runat="server" Visible="False">

An email has been sent to the email address you specified.

Thanks!

</asp:panel>

</form>

</body>

</html>
 
C

Cowboy \(Gregory A. Beamer\)

You have the br run right up against an ampersand, which could be your
issue.

My question is why you are building the page like an ASP page instead of an
ASP.NET page? In other words, why build when you can bind?

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*********************************************
Think outside the box!
*********************************************
 
D

dancer

I understand what is <br> and what is & vbCrLf

But Juan said "You *must* put all the HTML content in ONE line. You can't
break the lines with code."
????

Thanks



David said:
Ah,

Sorry, I have my newsreader on to ignore read messages.

The P tags break the lines in the rendered page. BR tags also break lines
(work as a hard wrap).

vbCrLf is a Visual Basic Carriage Return Line Feed. All this does is allow
the source html wrap onto a new line, so that your line is not too long.

If you want to use the same thing in C#, then it is something like...

email.Body = "This is my email body\nNow I am on a new line\nNote the
backslash and n to create a new line\n. I can't remember though, you might
need \r\n to create a new line";

backslash n.

Is that what you looking for? This however doesn't affect your rendered
output. use \n where there is vbcrlf, however, vbcrlf is a defined
constant, and \n has to be in quotes.

What you could do though is...

string vbCrLf = "\n";

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
dancer said:
David,

Not exactly does this answer my question. Could you please check out
Juan's answer to my first post on this subject and help me understand,
since I was copying my code from this code.

Thank you.


David said:
The vbCrLf are purely to ensure that the line is not too long when
generated. There is a problem with over long lines that when they do
eventually wrap, it can disrupt your formatted email.

You will note that there are no br tags, but there are p tags <p> and
</p>. These are what cause <P>aragraph breaks. (Actually, they are for
new paragraphs rather than breaking paragraphs).

Does this answer your question?

(Note, you probably can't press "Yes" below as you probably can't see
it. If you can, then you have better eyes than me, as I can't see it.
:)

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

Hi Juan,

What is the difference in what I am trying to do and this? Why can
THEY break up the HTML content into different lines?

email_html.aspx
--------------------------------------------------------------------------------

<%@ Page Language="VB" ClientTarget="downlevel" %>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat="server">
Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
Dim myMessage As New MailMessage
Dim myMail As SmtpMail
Dim strEmail As String

If Page.IsValid() Then
strEmail = txtEmail.Text

'myMessage.From = "webmaster@" &
Request.ServerVariables("SERVER_NAME")
myMessage.From = "(e-mail address removed)"
myMessage.To = strEmail
myMessage.Subject = "E-mail Sample (HTML) from ASP 101!"

' This is the magic line. Without this the message will just appear
' as plain HTML and won't be rendered by the recipient's email client.
' It'd be as if they did "View Source" on a web page.
MyMessage.BodyFormat = MailFormat.Html

' This is multi-lined simply for readability.
' Notice that it is a properly formatted HTML
' message and not just plain text like most email.
' A lot of people have asked how to use form data
' in the emails so I added an example of including
' the entered address in the body of the email.
myMessage.Body = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0
Transitional//EN"">" & vbCrLf _
& "<html>" & vbCrLf _
& "<head>" & vbCrLf _
& " <title>Sample Message From ASP 101</title>" & vbCrLf _
& " <meta http-equiv=Content-Type content=""text/html;
charset=iso-8859-1"">" & vbCrLf _
& "</head>" & vbCrLf _
& "<body bgcolor=""#FFFFCC"">" & vbCrLf _
& " <h2>Sample Message From ASP 101</h2>" & vbCrLf _
& " <p>" & vbCrLf _
& " This message was sent from a sample at" & vbCrLf _
& " <a href=""http://www.asp101.com"">ASP 101</a>." & vbCrLf _
& " It is used to show people how to send HTML" & vbCrLf _
& " formatted email from an ASP.NET page." & vbCrLf _
& " If you did not request this email yourself," & vbCrLf _
& " your address was entered by one of our" & vbCrLf _
& " visitors." & vbCrLf _
& " <strong>" & vbCrLf _
& " We do not store these e-mail addresses." & vbCrLf _
& " </strong>" & vbCrLf _
& " </p>" & vbCrLf _
& " <p><font size=""-1"">Please address all concerns to
(e-mail address removed).</font></p>" & vbCrLf _
& " <p><font size=""-1"">This message was sent to: " & strEmail &
"</font></p>" & vbCrLf _
& "</body>" & vbCrLf _
& "</html>" & vbCrLf

' Doesn't have to be local... just enter your
' SMTP server's name or ip address!
myMail.SmtpServer = "localhost"
myMail.Send(myMessage)

frmEmail.Visible = False
lblUserMessage.Text = "An HTML-formatted email message has been sent to
" & strEmail & "."
End If
End Sub
</script>

<html>
<head>
<title>ASP.NET Email (HTML Format) Sample</title>
</head>
<body>

<asp:Label id="lblUserMessage" text="Enter your e-mail address:"
runat="server" />
<form method="post" id="frmEmail" runat="server">
<asp:TextBox id="txtEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validEmailRequired" ControlToValidate="txtEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validEmailRegExp" ControlToValidate="txtEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />

<asp:Button id="btnSendMail" text="Send Mail!"
OnClick="btnSendMail_OnClick" runat="server" />
</form>

<hr />

<p>
Click <a
href="http://www.asp101.com/samples/email_html_aspx.asp">here</a> to
read about and download the source code.
</p>

</body>
</html>


--------------------------------------------------------------------------------

You don't need to set the html/head/body tags within the HTML body
...and, in the VbCrLf thread, we established that you can't use VbCrLf
in HTML.

Something else : you *must* put all the HTML content in ONE line.
You can't break the lines with code.

Get rid of all the "VbCrLf" and all the "&" and write all your HTML in
a single line,
line-breaking the body's content with "<br />" or "<P> ... </P>" as
needed.

Also, you can't insert Textbox controls into your HTML.

So, Check1.Text is out, too.

If you want to reference their content, after a user fills them out,
capture it to a variable and use that, before writing to the body.

insert :

Dim Check1 as String = Check1.Text
Dim Check2 as String = Check2.Text
Dim Location as String = Location.Text
before "Dim objMM as New MailMessage()"

and, when composing the body, use :

mail.Body="Hi, <p>The location where the accident occurred is :" &
Location & "<br/>" & Check1 & "."
' etc.

You *can* use the & to reference text variables.
Notice the location of the quote marks.





Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
Can somebody tell me why I get this message with the following code?

Compiler Error Message: BC30452: Operator '&' is not defined for
types 'String' and
'System.Web.UI.WebControls.TextBox'.

<html>

<head>

<% @Import Namespace="System.Web.Mail" %>

<script language="VB" runat="server">



Sub btnSendFeedback_Click(sender as Object, e as EventArgs)

'Create an instance of the MailMessage class

Dim objMM as New MailMessage()

'Set the properties - send the email to the person who filled out the

'feedback form.

objMM.To = "(e-mail address removed)"


objMM.From = "(e-mail address removed)"

'send in html format

objMM.BodyFormat = MailFormat.html

'Set the priority - options are High, Low, and Normal

objMM.Priority = MailPriority.Normal

'Set the subject

objMM.Subject = "Accident Investigation Form"


'Set the body

objMM.Body = DateTime.Now + " HI " & _

"<html>" & vbCrLf & vbCrLf & _

"<head>" & vbCrLf & vbCrLf & _

"</head>"& vbCrLf & vbCrLf & _

"<body>" & vbCrLf & vbCrLf & _

Location where accident occurred:

Location.Text & _

"<br>"& _

7> Check1.Text & "." & vbCrLf & vbCrLf & _

vbCrLf &vbCrLf & _

Check2.Text & vbCrLf & _

"</td>" & vbCrLf & vbCrLf & _

"</body>" & vbCrLf & vbCrLf & _

"</html>"


'Specify to use the default Smtp Server

SmtpMail.SmtpServer = ""

'Now, to send the message, use the Send method of the SmtpMail class

SmtpMail.Send(objMM)

panelSendEmail.Visible = false

panelMailSent.Visible = true

End Sub



</script>

</head>


<body>

<table width="750" bgcolor="#E9EDF4" table border="1"
cellpadding="3">

<h3><center><font face="Verdana">Wheeler's Accident Investigation
Form</font></h3>



<hr>

<%--____________________________________________________________________________--%>

<%--ROW 1--%>


<td width="250" valign="top">


<form runat="server">

<font face="Verdana" Size="2">

Location where accident occurred: <asp:textbox id="Location"
runat=server Width="200"/>

</td>

<td align="right" valign="top" width="225">


<font face="Verdana" Size="2">Employer's Premises



<asp:CheckBox id=Check1 Text="yes" runat="server" />

<asp:CheckBox id=Check2 Text="no" runat="server" />

<br>


Job site

<asp:CheckBox id=Check3 Text="yes" runat="server" />

<asp:CheckBox id=Check4 Text="no" runat="server" />


</td>

<td>

<font face="Verdana" Size="2">Date of accident</font><br>

<asp:textbox id="Date" runat=server Width="100"/>


</td>

<%--____________________________________________________________________________--%>

<%--ROW 2--%>


<tr> <td>

<font face="Verdana" Size="2">Who was injured?</font><br>

<asp:textbox id="Who" runat=server width="200"/>

</td>

<td align= "left">

<font face="Verdana" Size="2">Employee <asp:CheckBox id=Check5
runat="server" />

<br>

Non-employee <asp:CheckBox id=Check6 runat="server" />


</td>

<td align="right"><font face="Verdana" Size="2">

Time of accident a.m. <asp:textbox id="am" runat=server Width="90"/>

p.m. <asp:textbox id="pm" runat=server Width="90"/>

</TD>

<%--____________________________________________________________________________--%>

<%--ROW 3--%>

<tr>

<td>

<font face="Verdana" Size="2">Date of Hire <br> <asp:textbox
id="Hiredate" runat=server/>

</td>

<td>

<font face="Verdana" Size="2">Job Title or Occupation
<br><asp:textbox id="occupation"
runat=server width="200"/>

</td>

<td>

<font face="Verdana" Size="2">How long has employee worked at job
where injury
occurred?<br><asp:textbox id="lengthofjob" runat=server/>

</td>

<%--____________________________________________________________________________--%>

<%--Row 4--%>

&nbsp&nbsp



<p>


<asp:Label id=Label1 font-name="arial" font-size="10pt"
runat="server"/>

<!Copied from /smirnof>

<asp:panel id="panelSendEmail" runat="server">





<%--asp:textbox id= runat="server" /--%>

<br>

<b>Your Message:</b><br>

<%--asp:textbox id="txtMessage" TextMode="MultiLine"

Columns="40" Rows="10" runat="server" /--%>

<p>



<asp:button runat="server" id="btnSendFeedback" Text="Send

Feedback!"

OnClick="btnSendFeedback_Click" />



</asp:panel>

<asp:panel id="panelMailSent" runat="server" Visible="False">

An email has been sent to the email address you specified.

Thanks!

</asp:panel>

</form>

</body>

</html>
 
D

David

Right...

What you can do though is...

string MailBody = string.empty;

MailBody += "<p>First Line\n";
MailBody += "Second Line\n";
MailBody += "Third Line</p>\n"; // Note, all these will appear on same
line in message, but on seperate lines in the source.

email.Body = MailBody;

I think you are getting a little confused.

I have used string here. StringBuilder would actually be more efficient if
doing many lines like this.

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available


dancer said:
I understand what is <br> and what is & vbCrLf

But Juan said "You *must* put all the HTML content in ONE line. You can't
break the lines with code."
????

Thanks



David said:
Ah,

Sorry, I have my newsreader on to ignore read messages.

The P tags break the lines in the rendered page. BR tags also break lines
(work as a hard wrap).

vbCrLf is a Visual Basic Carriage Return Line Feed. All this does is
allow the source html wrap onto a new line, so that your line is not too
long.

If you want to use the same thing in C#, then it is something like...

email.Body = "This is my email body\nNow I am on a new line\nNote the
backslash and n to create a new line\n. I can't remember though, you
might need \r\n to create a new line";

backslash n.

Is that what you looking for? This however doesn't affect your rendered
output. use \n where there is vbcrlf, however, vbcrlf is a defined
constant, and \n has to be in quotes.

What you could do though is...

string vbCrLf = "\n";

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
dancer said:
David,

Not exactly does this answer my question. Could you please check out
Juan's answer to my first post on this subject and help me understand,
since I was copying my code from this code.

Thank you.


The vbCrLf are purely to ensure that the line is not too long when
generated. There is a problem with over long lines that when they do
eventually wrap, it can disrupt your formatted email.

You will note that there are no br tags, but there are p tags <p> and
</p>. These are what cause <P>aragraph breaks. (Actually, they are for
new paragraphs rather than breaking paragraphs).

Does this answer your question?

(Note, you probably can't press "Yes" below as you probably can't see
it. If you can, then you have better eyes than me, as I can't see it.
:)

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

Hi Juan,

What is the difference in what I am trying to do and this? Why can
THEY break up the HTML content into different lines?

email_html.aspx
--------------------------------------------------------------------------------

<%@ Page Language="VB" ClientTarget="downlevel" %>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat="server">
Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
Dim myMessage As New MailMessage
Dim myMail As SmtpMail
Dim strEmail As String

If Page.IsValid() Then
strEmail = txtEmail.Text

'myMessage.From = "webmaster@" &
Request.ServerVariables("SERVER_NAME")
myMessage.From = "(e-mail address removed)"
myMessage.To = strEmail
myMessage.Subject = "E-mail Sample (HTML) from ASP 101!"

' This is the magic line. Without this the message will just appear
' as plain HTML and won't be rendered by the recipient's email client.
' It'd be as if they did "View Source" on a web page.
MyMessage.BodyFormat = MailFormat.Html

' This is multi-lined simply for readability.
' Notice that it is a properly formatted HTML
' message and not just plain text like most email.
' A lot of people have asked how to use form data
' in the emails so I added an example of including
' the entered address in the body of the email.
myMessage.Body = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0
Transitional//EN"">" & vbCrLf _
& "<html>" & vbCrLf _
& "<head>" & vbCrLf _
& " <title>Sample Message From ASP 101</title>" & vbCrLf _
& " <meta http-equiv=Content-Type content=""text/html;
charset=iso-8859-1"">" & vbCrLf _
& "</head>" & vbCrLf _
& "<body bgcolor=""#FFFFCC"">" & vbCrLf _
& " <h2>Sample Message From ASP 101</h2>" & vbCrLf _
& " <p>" & vbCrLf _
& " This message was sent from a sample at" & vbCrLf _
& " <a href=""http://www.asp101.com"">ASP 101</a>." & vbCrLf _
& " It is used to show people how to send HTML" & vbCrLf _
& " formatted email from an ASP.NET page." & vbCrLf _
& " If you did not request this email yourself," & vbCrLf _
& " your address was entered by one of our" & vbCrLf _
& " visitors." & vbCrLf _
& " <strong>" & vbCrLf _
& " We do not store these e-mail addresses." & vbCrLf _
& " </strong>" & vbCrLf _
& " </p>" & vbCrLf _
& " <p><font size=""-1"">Please address all concerns to
(e-mail address removed).</font></p>" & vbCrLf _
& " <p><font size=""-1"">This message was sent to: " & strEmail &
"</font></p>" & vbCrLf _
& "</body>" & vbCrLf _
& "</html>" & vbCrLf

' Doesn't have to be local... just enter your
' SMTP server's name or ip address!
myMail.SmtpServer = "localhost"
myMail.Send(myMessage)

frmEmail.Visible = False
lblUserMessage.Text = "An HTML-formatted email message has been sent
to " & strEmail & "."
End If
End Sub
</script>

<html>
<head>
<title>ASP.NET Email (HTML Format) Sample</title>
</head>
<body>

<asp:Label id="lblUserMessage" text="Enter your e-mail address:"
runat="server" />
<form method="post" id="frmEmail" runat="server">
<asp:TextBox id="txtEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validEmailRequired" ControlToValidate="txtEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validEmailRegExp" ControlToValidate="txtEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />

<asp:Button id="btnSendMail" text="Send Mail!"
OnClick="btnSendMail_OnClick" runat="server" />
</form>

<hr />

<p>
Click <a
href="http://www.asp101.com/samples/email_html_aspx.asp">here</a> to
read about and download the source code.
</p>

</body>
</html>


--------------------------------------------------------------------------------

You don't need to set the html/head/body tags within the HTML body
...and, in the VbCrLf thread, we established that you can't use
VbCrLf in HTML.

Something else : you *must* put all the HTML content in ONE line.
You can't break the lines with code.

Get rid of all the "VbCrLf" and all the "&" and write all your HTML
in a single line,
line-breaking the body's content with "<br />" or "<P> ... </P>" as
needed.

Also, you can't insert Textbox controls into your HTML.

So, Check1.Text is out, too.

If you want to reference their content, after a user fills them out,
capture it to a variable and use that, before writing to the body.

insert :

Dim Check1 as String = Check1.Text
Dim Check2 as String = Check2.Text
Dim Location as String = Location.Text
before "Dim objMM as New MailMessage()"

and, when composing the body, use :

mail.Body="Hi, <p>The location where the accident occurred is :" &
Location & "<br/>" & Check1 & "."
' etc.

You *can* use the & to reference text variables.
Notice the location of the quote marks.





Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
Can somebody tell me why I get this message with the following code?

Compiler Error Message: BC30452: Operator '&' is not defined for
types 'String' and
'System.Web.UI.WebControls.TextBox'.

<html>

<head>

<% @Import Namespace="System.Web.Mail" %>

<script language="VB" runat="server">



Sub btnSendFeedback_Click(sender as Object, e as EventArgs)

'Create an instance of the MailMessage class

Dim objMM as New MailMessage()

'Set the properties - send the email to the person who filled out
the

'feedback form.

objMM.To = "(e-mail address removed)"


objMM.From = "(e-mail address removed)"

'send in html format

objMM.BodyFormat = MailFormat.html

'Set the priority - options are High, Low, and Normal

objMM.Priority = MailPriority.Normal

'Set the subject

objMM.Subject = "Accident Investigation Form"


'Set the body

objMM.Body = DateTime.Now + " HI " & _

"<html>" & vbCrLf & vbCrLf & _

"<head>" & vbCrLf & vbCrLf & _

"</head>"& vbCrLf & vbCrLf & _

"<body>" & vbCrLf & vbCrLf & _

Location where accident occurred:

Location.Text & _

"<br>"& _

7> Check1.Text & "." & vbCrLf & vbCrLf & _

vbCrLf &vbCrLf & _

Check2.Text & vbCrLf & _

"</td>" & vbCrLf & vbCrLf & _

"</body>" & vbCrLf & vbCrLf & _

"</html>"


'Specify to use the default Smtp Server

SmtpMail.SmtpServer = ""

'Now, to send the message, use the Send method of the SmtpMail class

SmtpMail.Send(objMM)

panelSendEmail.Visible = false

panelMailSent.Visible = true

End Sub



</script>

</head>


<body>

<table width="750" bgcolor="#E9EDF4" table border="1"
cellpadding="3">

<h3><center><font face="Verdana">Wheeler's Accident Investigation
Form</font></h3>



<hr>

<%--____________________________________________________________________________--%>

<%--ROW 1--%>


<td width="250" valign="top">


<form runat="server">

<font face="Verdana" Size="2">

Location where accident occurred: <asp:textbox id="Location"
runat=server Width="200"/>

</td>

<td align="right" valign="top" width="225">


<font face="Verdana" Size="2">Employer's Premises



<asp:CheckBox id=Check1 Text="yes" runat="server" />

<asp:CheckBox id=Check2 Text="no" runat="server" />

<br>


Job site

<asp:CheckBox id=Check3 Text="yes" runat="server" />

<asp:CheckBox id=Check4 Text="no" runat="server" />


</td>

<td>

<font face="Verdana" Size="2">Date of accident</font><br>

<asp:textbox id="Date" runat=server Width="100"/>


</td>

<%--____________________________________________________________________________--%>

<%--ROW 2--%>


<tr> <td>

<font face="Verdana" Size="2">Who was injured?</font><br>

<asp:textbox id="Who" runat=server width="200"/>

</td>

<td align= "left">

<font face="Verdana" Size="2">Employee <asp:CheckBox id=Check5
runat="server" />

<br>

Non-employee <asp:CheckBox id=Check6 runat="server" />


</td>

<td align="right"><font face="Verdana" Size="2">

Time of accident a.m. <asp:textbox id="am" runat=server Width="90"/>

p.m. <asp:textbox id="pm" runat=server Width="90"/>

</TD>

<%--____________________________________________________________________________--%>

<%--ROW 3--%>

<tr>

<td>

<font face="Verdana" Size="2">Date of Hire <br> <asp:textbox
id="Hiredate" runat=server/>

</td>

<td>

<font face="Verdana" Size="2">Job Title or Occupation
<br><asp:textbox id="occupation"
runat=server width="200"/>

</td>

<td>

<font face="Verdana" Size="2">How long has employee worked at job
where injury
occurred?<br><asp:textbox id="lengthofjob" runat=server/>

</td>

<%--____________________________________________________________________________--%>

<%--Row 4--%>

&nbsp&nbsp



<p>


<asp:Label id=Label1 font-name="arial" font-size="10pt"
runat="server"/>

<!Copied from /smirnof>

<asp:panel id="panelSendEmail" runat="server">





<%--asp:textbox id= runat="server" /--%>

<br>

<b>Your Message:</b><br>

<%--asp:textbox id="txtMessage" TextMode="MultiLine"

Columns="40" Rows="10" runat="server" /--%>

<p>



<asp:button runat="server" id="btnSendFeedback" Text="Send

Feedback!"

OnClick="btnSendFeedback_Click" />



</asp:panel>

<asp:panel id="panelMailSent" runat="server" Visible="False">

An email has been sent to the email address you specified.

Thanks!

</asp:panel>

</form>

</body>

</html>
 
D

dancer

Also, David,
I had asked why do I get this error message:
Compiler Error Message: BC30452: Operator '&' is not defined for types
String' and 'System.Web.UI.WebControls.TextBox'.

A part of Juan's reply was Something else : you *must* put all the HTML
content in ONE line.

Do you have any thoughts on that error message?


David said:
Ah,

Sorry, I have my newsreader on to ignore read messages.

The P tags break the lines in the rendered page. BR tags also break lines
(work as a hard wrap).

vbCrLf is a Visual Basic Carriage Return Line Feed. All this does is allow
the source html wrap onto a new line, so that your line is not too long.

If you want to use the same thing in C#, then it is something like...

email.Body = "This is my email body\nNow I am on a new line\nNote the
backslash and n to create a new line\n. I can't remember though, you might
need \r\n to create a new line";

backslash n.

Is that what you looking for? This however doesn't affect your rendered
output. use \n where there is vbcrlf, however, vbcrlf is a defined
constant, and \n has to be in quotes.

What you could do though is...

string vbCrLf = "\n";

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
dancer said:
David,

Not exactly does this answer my question. Could you please check out
Juan's answer to my first post on this subject and help me understand,
since I was copying my code from this code.

Thank you.


David said:
The vbCrLf are purely to ensure that the line is not too long when
generated. There is a problem with over long lines that when they do
eventually wrap, it can disrupt your formatted email.

You will note that there are no br tags, but there are p tags <p> and
</p>. These are what cause <P>aragraph breaks. (Actually, they are for
new paragraphs rather than breaking paragraphs).

Does this answer your question?

(Note, you probably can't press "Yes" below as you probably can't see
it. If you can, then you have better eyes than me, as I can't see it.
:)

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

Hi Juan,

What is the difference in what I am trying to do and this? Why can
THEY break up the HTML content into different lines?

email_html.aspx
--------------------------------------------------------------------------------

<%@ Page Language="VB" ClientTarget="downlevel" %>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat="server">
Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
Dim myMessage As New MailMessage
Dim myMail As SmtpMail
Dim strEmail As String

If Page.IsValid() Then
strEmail = txtEmail.Text

'myMessage.From = "webmaster@" &
Request.ServerVariables("SERVER_NAME")
myMessage.From = "(e-mail address removed)"
myMessage.To = strEmail
myMessage.Subject = "E-mail Sample (HTML) from ASP 101!"

' This is the magic line. Without this the message will just appear
' as plain HTML and won't be rendered by the recipient's email client.
' It'd be as if they did "View Source" on a web page.
MyMessage.BodyFormat = MailFormat.Html

' This is multi-lined simply for readability.
' Notice that it is a properly formatted HTML
' message and not just plain text like most email.
' A lot of people have asked how to use form data
' in the emails so I added an example of including
' the entered address in the body of the email.
myMessage.Body = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0
Transitional//EN"">" & vbCrLf _
& "<html>" & vbCrLf _
& "<head>" & vbCrLf _
& " <title>Sample Message From ASP 101</title>" & vbCrLf _
& " <meta http-equiv=Content-Type content=""text/html;
charset=iso-8859-1"">" & vbCrLf _
& "</head>" & vbCrLf _
& "<body bgcolor=""#FFFFCC"">" & vbCrLf _
& " <h2>Sample Message From ASP 101</h2>" & vbCrLf _
& " <p>" & vbCrLf _
& " This message was sent from a sample at" & vbCrLf _
& " <a href=""http://www.asp101.com"">ASP 101</a>." & vbCrLf _
& " It is used to show people how to send HTML" & vbCrLf _
& " formatted email from an ASP.NET page." & vbCrLf _
& " If you did not request this email yourself," & vbCrLf _
& " your address was entered by one of our" & vbCrLf _
& " visitors." & vbCrLf _
& " <strong>" & vbCrLf _
& " We do not store these e-mail addresses." & vbCrLf _
& " </strong>" & vbCrLf _
& " </p>" & vbCrLf _
& " <p><font size=""-1"">Please address all concerns to
(e-mail address removed).</font></p>" & vbCrLf _
& " <p><font size=""-1"">This message was sent to: " & strEmail &
"</font></p>" & vbCrLf _
& "</body>" & vbCrLf _
& "</html>" & vbCrLf

' Doesn't have to be local... just enter your
' SMTP server's name or ip address!
myMail.SmtpServer = "localhost"
myMail.Send(myMessage)

frmEmail.Visible = False
lblUserMessage.Text = "An HTML-formatted email message has been sent to
" & strEmail & "."
End If
End Sub
</script>

<html>
<head>
<title>ASP.NET Email (HTML Format) Sample</title>
</head>
<body>

<asp:Label id="lblUserMessage" text="Enter your e-mail address:"
runat="server" />
<form method="post" id="frmEmail" runat="server">
<asp:TextBox id="txtEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validEmailRequired" ControlToValidate="txtEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validEmailRegExp" ControlToValidate="txtEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />

<asp:Button id="btnSendMail" text="Send Mail!"
OnClick="btnSendMail_OnClick" runat="server" />
</form>

<hr />

<p>
Click <a
href="http://www.asp101.com/samples/email_html_aspx.asp">here</a> to
read about and download the source code.
</p>

</body>
</html>


--------------------------------------------------------------------------------

You don't need to set the html/head/body tags within the HTML body
...and, in the VbCrLf thread, we established that you can't use VbCrLf
in HTML.

Something else : you *must* put all the HTML content in ONE line.
You can't break the lines with code.

Get rid of all the "VbCrLf" and all the "&" and write all your HTML in
a single line,
line-breaking the body's content with "<br />" or "<P> ... </P>" as
needed.

Also, you can't insert Textbox controls into your HTML.

So, Check1.Text is out, too.

If you want to reference their content, after a user fills them out,
capture it to a variable and use that, before writing to the body.

insert :

Dim Check1 as String = Check1.Text
Dim Check2 as String = Check2.Text
Dim Location as String = Location.Text
before "Dim objMM as New MailMessage()"

and, when composing the body, use :

mail.Body="Hi, <p>The location where the accident occurred is :" &
Location & "<br/>" & Check1 & "."
' etc.

You *can* use the & to reference text variables.
Notice the location of the quote marks.





Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
Can somebody tell me why I get this message with the following code?

Compiler Error Message: BC30452: Operator '&' is not defined for
types 'String' and
'System.Web.UI.WebControls.TextBox'.

<html>

<head>

<% @Import Namespace="System.Web.Mail" %>

<script language="VB" runat="server">



Sub btnSendFeedback_Click(sender as Object, e as EventArgs)

'Create an instance of the MailMessage class

Dim objMM as New MailMessage()

'Set the properties - send the email to the person who filled out the

'feedback form.

objMM.To = "(e-mail address removed)"


objMM.From = "(e-mail address removed)"

'send in html format

objMM.BodyFormat = MailFormat.html

'Set the priority - options are High, Low, and Normal

objMM.Priority = MailPriority.Normal

'Set the subject

objMM.Subject = "Accident Investigation Form"


'Set the body

objMM.Body = DateTime.Now + " HI " & _

"<html>" & vbCrLf & vbCrLf & _

"<head>" & vbCrLf & vbCrLf & _

"</head>"& vbCrLf & vbCrLf & _

"<body>" & vbCrLf & vbCrLf & _

Location where accident occurred:

Location.Text & _

"<br>"& _

7> Check1.Text & "." & vbCrLf & vbCrLf & _

vbCrLf &vbCrLf & _

Check2.Text & vbCrLf & _

"</td>" & vbCrLf & vbCrLf & _

"</body>" & vbCrLf & vbCrLf & _

"</html>"


'Specify to use the default Smtp Server

SmtpMail.SmtpServer = ""

'Now, to send the message, use the Send method of the SmtpMail class

SmtpMail.Send(objMM)

panelSendEmail.Visible = false

panelMailSent.Visible = true

End Sub



</script>

</head>


<body>

<table width="750" bgcolor="#E9EDF4" table border="1"
cellpadding="3">

<h3><center><font face="Verdana">Wheeler's Accident Investigation
Form</font></h3>



<hr>

<%--____________________________________________________________________________--%>

<%--ROW 1--%>


<td width="250" valign="top">


<form runat="server">

<font face="Verdana" Size="2">

Location where accident occurred: <asp:textbox id="Location"
runat=server Width="200"/>

</td>

<td align="right" valign="top" width="225">


<font face="Verdana" Size="2">Employer's Premises



<asp:CheckBox id=Check1 Text="yes" runat="server" />

<asp:CheckBox id=Check2 Text="no" runat="server" />

<br>


Job site

<asp:CheckBox id=Check3 Text="yes" runat="server" />

<asp:CheckBox id=Check4 Text="no" runat="server" />


</td>

<td>

<font face="Verdana" Size="2">Date of accident</font><br>

<asp:textbox id="Date" runat=server Width="100"/>


</td>

<%--____________________________________________________________________________--%>

<%--ROW 2--%>


<tr> <td>

<font face="Verdana" Size="2">Who was injured?</font><br>

<asp:textbox id="Who" runat=server width="200"/>

</td>

<td align= "left">

<font face="Verdana" Size="2">Employee <asp:CheckBox id=Check5
runat="server" />

<br>

Non-employee <asp:CheckBox id=Check6 runat="server" />


</td>

<td align="right"><font face="Verdana" Size="2">

Time of accident a.m. <asp:textbox id="am" runat=server Width="90"/>

p.m. <asp:textbox id="pm" runat=server Width="90"/>

</TD>

<%--____________________________________________________________________________--%>

<%--ROW 3--%>

<tr>

<td>

<font face="Verdana" Size="2">Date of Hire <br> <asp:textbox
id="Hiredate" runat=server/>

</td>

<td>

<font face="Verdana" Size="2">Job Title or Occupation
<br><asp:textbox id="occupation"
runat=server width="200"/>

</td>

<td>

<font face="Verdana" Size="2">How long has employee worked at job
where injury
occurred?<br><asp:textbox id="lengthofjob" runat=server/>

</td>

<%--____________________________________________________________________________--%>

<%--Row 4--%>

&nbsp&nbsp



<p>


<asp:Label id=Label1 font-name="arial" font-size="10pt"
runat="server"/>

<!Copied from /smirnof>

<asp:panel id="panelSendEmail" runat="server">





<%--asp:textbox id= runat="server" /--%>

<br>

<b>Your Message:</b><br>

<%--asp:textbox id="txtMessage" TextMode="MultiLine"

Columns="40" Rows="10" runat="server" /--%>

<p>



<asp:button runat="server" id="btnSendFeedback" Text="Send

Feedback!"

OnClick="btnSendFeedback_Click" />



</asp:panel>

<asp:panel id="panelMailSent" runat="server" Visible="False">

An email has been sent to the email address you specified.

Thanks!

</asp:panel>

</form>

</body>

</html>
 
D

dancer

Cowboy,

Because I don't know what I'm doing.
I had created a form in ASP.NET that worked well.
I wanted to email the results, and that worked also.
But I wanted the email results to be formatted nicely, just like the
original form in which somebody would enter the data.
Somebody sent me to ASP101.com. There I clicked source code in Asp.net. I
studied their code and copied their method (I thought).
So here I am, confused.
How would YOU go about making the email nicely formatted?
 
D

David

Juan is correct, but I can understand why it is confusing you.

The & is a VB concatenation operator. In C#, the concatenation operator is +

Where there is &, put a +

Where there is vbCrLf put "\n" (or create a string called vbCrLf and assign
"\n" to that string)

When sending the message in email, your mail.Body has to have the string you
want to send effectively in one line. In VB, you achieve that by putting &
to join the strings togethor.

In C#, you use the + (as mentioned above.)

However, you can build the string prior to sticking it in the mail.Body.
This is where the += helps.

e.g.
// NOTE, This is C#
string MyString = "Dave ";
MyString += "Colliver ";

Response.Write(MyString); // Writes "Dave Colliver" to the screen.

What that has effectively done is to create one line. That line is called
MyString.

Does this help?

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available


dancer said:
Also, David,
I had asked why do I get this error message:
Compiler Error Message: BC30452: Operator '&' is not defined for types
String' and 'System.Web.UI.WebControls.TextBox'.

A part of Juan's reply was Something else : you *must* put all the HTML
content in ONE line.

Do you have any thoughts on that error message?


David said:
Ah,

Sorry, I have my newsreader on to ignore read messages.

The P tags break the lines in the rendered page. BR tags also break lines
(work as a hard wrap).

vbCrLf is a Visual Basic Carriage Return Line Feed. All this does is
allow the source html wrap onto a new line, so that your line is not too
long.

If you want to use the same thing in C#, then it is something like...

email.Body = "This is my email body\nNow I am on a new line\nNote the
backslash and n to create a new line\n. I can't remember though, you
might need \r\n to create a new line";

backslash n.

Is that what you looking for? This however doesn't affect your rendered
output. use \n where there is vbcrlf, however, vbcrlf is a defined
constant, and \n has to be in quotes.

What you could do though is...

string vbCrLf = "\n";

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
dancer said:
David,

Not exactly does this answer my question. Could you please check out
Juan's answer to my first post on this subject and help me understand,
since I was copying my code from this code.

Thank you.


The vbCrLf are purely to ensure that the line is not too long when
generated. There is a problem with over long lines that when they do
eventually wrap, it can disrupt your formatted email.

You will note that there are no br tags, but there are p tags <p> and
</p>. These are what cause <P>aragraph breaks. (Actually, they are for
new paragraphs rather than breaking paragraphs).

Does this answer your question?

(Note, you probably can't press "Yes" below as you probably can't see
it. If you can, then you have better eyes than me, as I can't see it.
:)

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

Hi Juan,

What is the difference in what I am trying to do and this? Why can
THEY break up the HTML content into different lines?

email_html.aspx
--------------------------------------------------------------------------------

<%@ Page Language="VB" ClientTarget="downlevel" %>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat="server">
Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
Dim myMessage As New MailMessage
Dim myMail As SmtpMail
Dim strEmail As String

If Page.IsValid() Then
strEmail = txtEmail.Text

'myMessage.From = "webmaster@" &
Request.ServerVariables("SERVER_NAME")
myMessage.From = "(e-mail address removed)"
myMessage.To = strEmail
myMessage.Subject = "E-mail Sample (HTML) from ASP 101!"

' This is the magic line. Without this the message will just appear
' as plain HTML and won't be rendered by the recipient's email client.
' It'd be as if they did "View Source" on a web page.
MyMessage.BodyFormat = MailFormat.Html

' This is multi-lined simply for readability.
' Notice that it is a properly formatted HTML
' message and not just plain text like most email.
' A lot of people have asked how to use form data
' in the emails so I added an example of including
' the entered address in the body of the email.
myMessage.Body = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0
Transitional//EN"">" & vbCrLf _
& "<html>" & vbCrLf _
& "<head>" & vbCrLf _
& " <title>Sample Message From ASP 101</title>" & vbCrLf _
& " <meta http-equiv=Content-Type content=""text/html;
charset=iso-8859-1"">" & vbCrLf _
& "</head>" & vbCrLf _
& "<body bgcolor=""#FFFFCC"">" & vbCrLf _
& " <h2>Sample Message From ASP 101</h2>" & vbCrLf _
& " <p>" & vbCrLf _
& " This message was sent from a sample at" & vbCrLf _
& " <a href=""http://www.asp101.com"">ASP 101</a>." & vbCrLf _
& " It is used to show people how to send HTML" & vbCrLf _
& " formatted email from an ASP.NET page." & vbCrLf _
& " If you did not request this email yourself," & vbCrLf _
& " your address was entered by one of our" & vbCrLf _
& " visitors." & vbCrLf _
& " <strong>" & vbCrLf _
& " We do not store these e-mail addresses." & vbCrLf _
& " </strong>" & vbCrLf _
& " </p>" & vbCrLf _
& " <p><font size=""-1"">Please address all concerns to
(e-mail address removed).</font></p>" & vbCrLf _
& " <p><font size=""-1"">This message was sent to: " & strEmail &
"</font></p>" & vbCrLf _
& "</body>" & vbCrLf _
& "</html>" & vbCrLf

' Doesn't have to be local... just enter your
' SMTP server's name or ip address!
myMail.SmtpServer = "localhost"
myMail.Send(myMessage)

frmEmail.Visible = False
lblUserMessage.Text = "An HTML-formatted email message has been sent
to " & strEmail & "."
End If
End Sub
</script>

<html>
<head>
<title>ASP.NET Email (HTML Format) Sample</title>
</head>
<body>

<asp:Label id="lblUserMessage" text="Enter your e-mail address:"
runat="server" />
<form method="post" id="frmEmail" runat="server">
<asp:TextBox id="txtEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validEmailRequired" ControlToValidate="txtEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validEmailRegExp" ControlToValidate="txtEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />

<asp:Button id="btnSendMail" text="Send Mail!"
OnClick="btnSendMail_OnClick" runat="server" />
</form>

<hr />

<p>
Click <a
href="http://www.asp101.com/samples/email_html_aspx.asp">here</a> to
read about and download the source code.
</p>

</body>
</html>


--------------------------------------------------------------------------------

You don't need to set the html/head/body tags within the HTML body
...and, in the VbCrLf thread, we established that you can't use
VbCrLf in HTML.

Something else : you *must* put all the HTML content in ONE line.
You can't break the lines with code.

Get rid of all the "VbCrLf" and all the "&" and write all your HTML
in a single line,
line-breaking the body's content with "<br />" or "<P> ... </P>" as
needed.

Also, you can't insert Textbox controls into your HTML.

So, Check1.Text is out, too.

If you want to reference their content, after a user fills them out,
capture it to a variable and use that, before writing to the body.

insert :

Dim Check1 as String = Check1.Text
Dim Check2 as String = Check2.Text
Dim Location as String = Location.Text
before "Dim objMM as New MailMessage()"

and, when composing the body, use :

mail.Body="Hi, <p>The location where the accident occurred is :" &
Location & "<br/>" & Check1 & "."
' etc.

You *can* use the & to reference text variables.
Notice the location of the quote marks.





Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
Can somebody tell me why I get this message with the following code?

Compiler Error Message: BC30452: Operator '&' is not defined for
types 'String' and
'System.Web.UI.WebControls.TextBox'.

<html>

<head>

<% @Import Namespace="System.Web.Mail" %>

<script language="VB" runat="server">



Sub btnSendFeedback_Click(sender as Object, e as EventArgs)

'Create an instance of the MailMessage class

Dim objMM as New MailMessage()

'Set the properties - send the email to the person who filled out
the

'feedback form.

objMM.To = "(e-mail address removed)"


objMM.From = "(e-mail address removed)"

'send in html format

objMM.BodyFormat = MailFormat.html

'Set the priority - options are High, Low, and Normal

objMM.Priority = MailPriority.Normal

'Set the subject

objMM.Subject = "Accident Investigation Form"


'Set the body

objMM.Body = DateTime.Now + " HI " & _

"<html>" & vbCrLf & vbCrLf & _

"<head>" & vbCrLf & vbCrLf & _

"</head>"& vbCrLf & vbCrLf & _

"<body>" & vbCrLf & vbCrLf & _

Location where accident occurred:

Location.Text & _

"<br>"& _

7> Check1.Text & "." & vbCrLf & vbCrLf & _

vbCrLf &vbCrLf & _

Check2.Text & vbCrLf & _

"</td>" & vbCrLf & vbCrLf & _

"</body>" & vbCrLf & vbCrLf & _

"</html>"


'Specify to use the default Smtp Server

SmtpMail.SmtpServer = ""

'Now, to send the message, use the Send method of the SmtpMail class

SmtpMail.Send(objMM)

panelSendEmail.Visible = false

panelMailSent.Visible = true

End Sub



</script>

</head>


<body>

<table width="750" bgcolor="#E9EDF4" table border="1"
cellpadding="3">

<h3><center><font face="Verdana">Wheeler's Accident Investigation
Form</font></h3>



<hr>

<%--____________________________________________________________________________--%>

<%--ROW 1--%>


<td width="250" valign="top">


<form runat="server">

<font face="Verdana" Size="2">

Location where accident occurred: <asp:textbox id="Location"
runat=server Width="200"/>

</td>

<td align="right" valign="top" width="225">


<font face="Verdana" Size="2">Employer's Premises



<asp:CheckBox id=Check1 Text="yes" runat="server" />

<asp:CheckBox id=Check2 Text="no" runat="server" />

<br>


Job site

<asp:CheckBox id=Check3 Text="yes" runat="server" />

<asp:CheckBox id=Check4 Text="no" runat="server" />


</td>

<td>

<font face="Verdana" Size="2">Date of accident</font><br>

<asp:textbox id="Date" runat=server Width="100"/>


</td>

<%--____________________________________________________________________________--%>

<%--ROW 2--%>


<tr> <td>

<font face="Verdana" Size="2">Who was injured?</font><br>

<asp:textbox id="Who" runat=server width="200"/>

</td>

<td align= "left">

<font face="Verdana" Size="2">Employee <asp:CheckBox id=Check5
runat="server" />

<br>

Non-employee <asp:CheckBox id=Check6 runat="server" />


</td>

<td align="right"><font face="Verdana" Size="2">

Time of accident a.m. <asp:textbox id="am" runat=server Width="90"/>

p.m. <asp:textbox id="pm" runat=server Width="90"/>

</TD>

<%--____________________________________________________________________________--%>

<%--ROW 3--%>

<tr>

<td>

<font face="Verdana" Size="2">Date of Hire <br> <asp:textbox
id="Hiredate" runat=server/>

</td>

<td>

<font face="Verdana" Size="2">Job Title or Occupation
<br><asp:textbox id="occupation"
runat=server width="200"/>

</td>

<td>

<font face="Verdana" Size="2">How long has employee worked at job
where injury
occurred?<br><asp:textbox id="lengthofjob" runat=server/>

</td>

<%--____________________________________________________________________________--%>

<%--Row 4--%>

&nbsp&nbsp



<p>


<asp:Label id=Label1 font-name="arial" font-size="10pt"
runat="server"/>

<!Copied from /smirnof>

<asp:panel id="panelSendEmail" runat="server">





<%--asp:textbox id= runat="server" /--%>

<br>

<b>Your Message:</b><br>

<%--asp:textbox id="txtMessage" TextMode="MultiLine"

Columns="40" Rows="10" runat="server" /--%>

<p>



<asp:button runat="server" id="btnSendFeedback" Text="Send

Feedback!"

OnClick="btnSendFeedback_Click" />



</asp:panel>

<asp:panel id="panelMailSent" runat="server" Visible="False">

An email has been sent to the email address you specified.

Thanks!

</asp:panel>

</form>

</body>

</html>
 
J

Juan T. Llibre

re:
!> A part of Juan's reply was Something else : you *must* put all the HTML
!> content in ONE line.

I meant : "all the HTML content in ONE line".

The sample at ASP101 places all the HTML content in ONE line,
even though it composes that single line in several lines.

That's why you need to use VbCrLf and the underscore ( _ ):
to COMPOSE a single line over several lines of code.

Confusing ? You betcha.

Also, you're inverting the order of the VbCrLf and the underscore,
which is why the sample is blowing up on you.

i.e., you have :

objMM.Body = DateTime.Now + " HI " & _
"<html>" & vbCrLf & vbCrLf & _
"<head>" & vbCrLf & vbCrLf & _
"</head>"& vbCrLf & vbCrLf & _
"<body>" & vbCrLf & vbCrLf & _

etc....

When what you should have is :

objMM.Body = DateTime.Now + " HI " _
& "<html>" & vbCrLf & vbCrLf _
& "<head>" & vbCrLf & vbCrLf _
& "</head>"& vbCrLf & vbCrLf _
& "<body>" & vbCrLf & vbCrLf _
etc...

i.e., the underscore goes at the end of the line
you want to break, BEFORE the ampersand on the next line.

Even more confusing is adding unneeded HTML tags to an HTML body
which doesn't need them, unless you need to have a background color.

Here's a version of the ASP101 sample without the
unneeded HTML tags and *with* HTML format :

<%@ Page Language="VB" ClientTarget="downlevel" %>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat="server">
Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
Dim myMessage As New MailMessage
Dim myMail As SmtpMail
Dim strEmail As String

If Page.IsValid() Then
strEmail = txtEmail.Text

myMessage.From = "admin@" & Request.ServerVariables("SERVER_NAME")
myMessage.To = strEmail
myMessage.Subject = "E-mail Sample (HTML)"

' This is the magic line. Without this the message will just appear
' as plain HTML and won't be rendered by the recipient's email client.
' It'd be as if they did "View Source" on a web page.
MyMessage.BodyFormat = MailFormat.Html

' This is multi-lined simply for readability.
' Notice that it is a properly formatted HTML
' message and not just plain text like most email.
' A lot of people have asked how to use form data
' in the emails so I added an example of including
' the entered address in the body of the email.
myMessage.Body = "<h2>Sample Message</h2>" & vbCrLf _
& " <p>" & vbCrLf _
& " This message was sent from a sample at" & vbCrLf _
& " <a href=""http://yourserver.com"">Your Server 101</a>." & vbCrLf _
& " It is used to show people how to send HTML" & vbCrLf _
& " formatted email from an ASP.NET page." & vbCrLf _
& " If you did not request this email yourself," & vbCrLf _
& " your address was entered by one of our" & vbCrLf _
& " visitors." & vbCrLf _
& " <strong>" & vbCrLf _
& " We do not store these e-mail addresses." & vbCrLf _
& " </strong>" & vbCrLf _
& " </p>" & vbCrLf _
& " <p><font size=""-1"">Please address all concerns to (e-mail address removed).</font></p>" &
vbCrLf _
& " <p><font size=""-1"">This message was sent to: " & strEmail & "</font></p>"

' Doesn't have to be local... just enter your
' SMTP server's name or ip address!
myMail.SmtpServer = "YourSMTPServer"
myMail.Send(myMessage)

frmEmail.Visible = False
lblUserMessage.Text = "An HTML-formatted email message has been sent to " & strEmail & "."
End If
End Sub
</script>

<html>
<head>
<title>ASP.NET Email (HTML Format) Sample</title>
</head>
<body>

<asp:Label id="lblUserMessage" text="Enter your e-mail address:" runat="server" />
<form method="post" id="frmEmail" runat="server">
<asp:TextBox id="txtEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validEmailRequired" ControlToValidate="txtEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validEmailRegExp" ControlToValidate="txtEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />

<asp:Button id="btnSendMail" text="Send Mail!" OnClick="btnSendMail_OnClick" runat="server" />
</form>

<hr />

</body>
</html>

------------------

You'll see that it compiles just fine and sends perfectly formatted HTML
without the need for those extra HTML tags ( <html>, <head>, <body> <meta...>.

Of course, if you want the yellow background, or any other color,
or a background image, include the HTML tags.

I, personally, hate email sent to me as it's too gaudy, but to each his own.

Make sure you substitute your SMTP server's name before running the page:

myMail.SmtpServer = "YourSMTPServer"




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
dancer said:
Also, David,
I had asked why do I get this error message:
Compiler Error Message: BC30452: Operator '&' is not defined for types String' and
'System.Web.UI.WebControls.TextBox'.

A part of Juan's reply was Something else : you *must* put all the HTML content in ONE line.

Do you have any thoughts on that error message?


David said:
Ah,

Sorry, I have my newsreader on to ignore read messages.

The P tags break the lines in the rendered page. BR tags also break lines (work as a hard wrap).

vbCrLf is a Visual Basic Carriage Return Line Feed. All this does is allow the source html wrap
onto a new line, so that your line is not too long.

If you want to use the same thing in C#, then it is something like...

email.Body = "This is my email body\nNow I am on a new line\nNote the backslash and n to create a
new line\n. I can't remember though, you might need \r\n to create a new line";

backslash n.

Is that what you looking for? This however doesn't affect your rendered output. use \n where
there is vbcrlf, however, vbcrlf is a defined constant, and \n has to be in quotes.

What you could do though is...

string vbCrLf = "\n";

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
dancer said:
David,

Not exactly does this answer my question. Could you please check out Juan's answer to my first
post on this subject and help me understand, since I was copying my code from this code.

Thank you.


The vbCrLf are purely to ensure that the line is not too long when generated. There is a
problem with over long lines that when they do eventually wrap, it can disrupt your formatted
email.

You will note that there are no br tags, but there are p tags <p> and </p>. These are what
cause <P>aragraph breaks. (Actually, they are for new paragraphs rather than breaking
paragraphs).

Does this answer your question?

(Note, you probably can't press "Yes" below as you probably can't see it. If you can, then you
have better eyes than me, as I can't see it. :)

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

Hi Juan,

What is the difference in what I am trying to do and this? Why can THEY break up the HTML
content into different lines?

email_html.aspx
--------------------------------------------------------------------------------

<%@ Page Language="VB" ClientTarget="downlevel" %>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat="server">
Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
Dim myMessage As New MailMessage
Dim myMail As SmtpMail
Dim strEmail As String

If Page.IsValid() Then
strEmail = txtEmail.Text

'myMessage.From = "webmaster@" & Request.ServerVariables("SERVER_NAME")
myMessage.From = "(e-mail address removed)"
myMessage.To = strEmail
myMessage.Subject = "E-mail Sample (HTML) from ASP 101!"

' This is the magic line. Without this the message will just appear
' as plain HTML and won't be rendered by the recipient's email client.
' It'd be as if they did "View Source" on a web page.
MyMessage.BodyFormat = MailFormat.Html

' This is multi-lined simply for readability.
' Notice that it is a properly formatted HTML
' message and not just plain text like most email.
' A lot of people have asked how to use form data
' in the emails so I added an example of including
' the entered address in the body of the email.
myMessage.Body = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">" &
vbCrLf _
& "<html>" & vbCrLf _
& "<head>" & vbCrLf _
& " <title>Sample Message From ASP 101</title>" & vbCrLf _
& " <meta http-equiv=Content-Type content=""text/html; charset=iso-8859-1"">" & vbCrLf _
& "</head>" & vbCrLf _
& "<body bgcolor=""#FFFFCC"">" & vbCrLf _
& " <h2>Sample Message From ASP 101</h2>" & vbCrLf _
& " <p>" & vbCrLf _
& " This message was sent from a sample at" & vbCrLf _
& " <a href=""http://www.asp101.com"">ASP 101</a>." & vbCrLf _
& " It is used to show people how to send HTML" & vbCrLf _
& " formatted email from an ASP.NET page." & vbCrLf _
& " If you did not request this email yourself," & vbCrLf _
& " your address was entered by one of our" & vbCrLf _
& " visitors." & vbCrLf _
& " <strong>" & vbCrLf _
& " We do not store these e-mail addresses." & vbCrLf _
& " </strong>" & vbCrLf _
& " </p>" & vbCrLf _
& " <p><font size=""-1"">Please address all concerns to (e-mail address removed).</font></p>" &
vbCrLf _
& " <p><font size=""-1"">This message was sent to: " & strEmail & "</font></p>" & vbCrLf _
& "</body>" & vbCrLf _
& "</html>" & vbCrLf

' Doesn't have to be local... just enter your
' SMTP server's name or ip address!
myMail.SmtpServer = "localhost"
myMail.Send(myMessage)

frmEmail.Visible = False
lblUserMessage.Text = "An HTML-formatted email message has been sent to " & strEmail & "."
End If
End Sub
</script>

<html>
<head>
<title>ASP.NET Email (HTML Format) Sample</title>
</head>
<body>

<asp:Label id="lblUserMessage" text="Enter your e-mail address:" runat="server" />
<form method="post" id="frmEmail" runat="server">
<asp:TextBox id="txtEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validEmailRequired" ControlToValidate="txtEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validEmailRegExp" ControlToValidate="txtEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />

<asp:Button id="btnSendMail" text="Send Mail!" OnClick="btnSendMail_OnClick" runat="server" />
</form>

<hr />

<p>
Click <a href="http://www.asp101.com/samples/email_html_aspx.asp">here</a> to read about and
download the source code.
</p>

</body>
</html>


--------------------------------------------------------------------------------

You don't need to set the html/head/body tags within the HTML body
...and, in the VbCrLf thread, we established that you can't use VbCrLf in HTML.

Something else : you *must* put all the HTML content in ONE line.
You can't break the lines with code.

Get rid of all the "VbCrLf" and all the "&" and write all your HTML in a single line,
line-breaking the body's content with "<br />" or "<P> ... </P>" as needed.

Also, you can't insert Textbox controls into your HTML.

So, Check1.Text is out, too.

If you want to reference their content, after a user fills them out,
capture it to a variable and use that, before writing to the body.

insert :

Dim Check1 as String = Check1.Text
Dim Check2 as String = Check2.Text
Dim Location as String = Location.Text
before "Dim objMM as New MailMessage()"

and, when composing the body, use :

mail.Body="Hi, <p>The location where the accident occurred is :" & Location & "<br/>" &
Check1 & "."
' etc.

You *can* use the & to reference text variables.
Notice the location of the quote marks.





Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
Can somebody tell me why I get this message with the following code?

Compiler Error Message: BC30452: Operator '&' is not defined for types 'String' and
'System.Web.UI.WebControls.TextBox'.

<html>

<head>

<% @Import Namespace="System.Web.Mail" %>

<script language="VB" runat="server">



Sub btnSendFeedback_Click(sender as Object, e as EventArgs)

'Create an instance of the MailMessage class

Dim objMM as New MailMessage()

'Set the properties - send the email to the person who filled out the

'feedback form.

objMM.To = "(e-mail address removed)"


objMM.From = "(e-mail address removed)"

'send in html format

objMM.BodyFormat = MailFormat.html

'Set the priority - options are High, Low, and Normal

objMM.Priority = MailPriority.Normal

'Set the subject

objMM.Subject = "Accident Investigation Form"


'Set the body

objMM.Body = DateTime.Now + " HI " & _

"<html>" & vbCrLf & vbCrLf & _

"<head>" & vbCrLf & vbCrLf & _

"</head>"& vbCrLf & vbCrLf & _

"<body>" & vbCrLf & vbCrLf & _

Location where accident occurred:

Location.Text & _

"<br>"& _

7> Check1.Text & "." & vbCrLf & vbCrLf & _

vbCrLf &vbCrLf & _

Check2.Text & vbCrLf & _

"</td>" & vbCrLf & vbCrLf & _

"</body>" & vbCrLf & vbCrLf & _

"</html>"


'Specify to use the default Smtp Server

SmtpMail.SmtpServer = ""

'Now, to send the message, use the Send method of the SmtpMail class

SmtpMail.Send(objMM)

panelSendEmail.Visible = false

panelMailSent.Visible = true

End Sub



</script>

</head>


<body>

<table width="750" bgcolor="#E9EDF4" table border="1" cellpadding="3">

<h3><center><font face="Verdana">Wheeler's Accident Investigation Form</font></h3>



<hr>

<%--____________________________________________________________________________--%>

<%--ROW 1--%>


<td width="250" valign="top">


<form runat="server">

<font face="Verdana" Size="2">

Location where accident occurred: <asp:textbox id="Location" runat=server Width="200"/>

</td>

<td align="right" valign="top" width="225">


<font face="Verdana" Size="2">Employer's Premises



<asp:CheckBox id=Check1 Text="yes" runat="server" />

<asp:CheckBox id=Check2 Text="no" runat="server" />

<br>


Job site

<asp:CheckBox id=Check3 Text="yes" runat="server" />

<asp:CheckBox id=Check4 Text="no" runat="server" />


</td>

<td>

<font face="Verdana" Size="2">Date of accident</font><br>

<asp:textbox id="Date" runat=server Width="100"/>


</td>

<%--____________________________________________________________________________--%>

<%--ROW 2--%>


<tr> <td>

<font face="Verdana" Size="2">Who was injured?</font><br>

<asp:textbox id="Who" runat=server width="200"/>

</td>

<td align= "left">

<font face="Verdana" Size="2">Employee <asp:CheckBox id=Check5 runat="server" />

<br>

Non-employee <asp:CheckBox id=Check6 runat="server" />


</td>

<td align="right"><font face="Verdana" Size="2">

Time of accident a.m. <asp:textbox id="am" runat=server Width="90"/>

p.m. <asp:textbox id="pm" runat=server Width="90"/>

</TD>

<%--____________________________________________________________________________--%>

<%--ROW 3--%>

<tr>

<td>

<font face="Verdana" Size="2">Date of Hire <br> <asp:textbox id="Hiredate" runat=server/>

</td>

<td>

<font face="Verdana" Size="2">Job Title or Occupation <br><asp:textbox id="occupation"
runat=server width="200"/>

</td>

<td>

<font face="Verdana" Size="2">How long has employee worked at job where injury
occurred?<br><asp:textbox id="lengthofjob" runat=server/>

</td>

<%--____________________________________________________________________________--%>

<%--Row 4--%>

&nbsp&nbsp



<p>


<asp:Label id=Label1 font-name="arial" font-size="10pt" runat="server"/>

<!Copied from /smirnof>

<asp:panel id="panelSendEmail" runat="server">





<%--asp:textbox id= runat="server" /--%>

<br>

<b>Your Message:</b><br>

<%--asp:textbox id="txtMessage" TextMode="MultiLine"

Columns="40" Rows="10" runat="server" /--%>

<p>



<asp:button runat="server" id="btnSendFeedback" Text="Send

Feedback!"

OnClick="btnSendFeedback_Click" />



</asp:panel>

<asp:panel id="panelMailSent" runat="server" Visible="False">

An email has been sent to the email address you specified.

Thanks!

</asp:panel>

</form>

</body>

</html>
 
M

Mark Rae

You said, "I, personally, hate email sent to me as it's too gaudy, but to
each his own."
I assume you mean you hate HTML email.

More and more people have their email software configured not to display the
HTML content of email.

Some people even have their email software configured to automatically
reject HTML email.

Don't use it if you can avoid it...
 
J

Juan T. Llibre

re:
I assume you mean you hate HTML email.

Yes, I meant that, although I don't hate it for the HTML itself.
I hate it when background colors, garish font colors and many fonts are used.

re:
Perhaps you have a better idea for returning the information to the Safety Director?

My suggestion stands : get rid of the useless HTML tags and background color.

Business mail should be business-like in appearance.

You *can* send HTML mail with a white background, using a single standard font like Arial,
bolding the headlines and/or paragraph headings, using a larger font size for headlines,
and keeping the use of color to a minimum...or even not using it at all.

Your Safety Director will appreciate that in your business report, I'm sure.

I formatted this response as HTML to show you what the ASP101
sample looks like when formatted with business mail in mind:

-------------

Sample Message
This message was sent from a sample at ASP101.

It is used to show people how to send HTML formatted email from an ASP.NET page.
If you did not request this email yourself, your address was entered by one of our visitors.

We do not store these e-mail addresses

--------

Notice the difference...
Instead of the garish yellow background in the original sample, you have a clean, crisp, business format.

Good luck and, if you run into more problems, post back.




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top