Send Email from ASPX page using VB [closed]
I have a page with textboxes. The codefile sends me the email just fine, but the textboxes are not captured.
mailMessage.Body = "From: " & NameBox.Text & Chr(13) & Chr(11) & "City, State: " & TextBox2.Text & Chr(13) & Chr(11) & "Comments: " & TextBox3.Text
I had code to blank out the textboxes after sending the message, but now removed them, but the code still zeros them out,
mSmtpClient.Send(mailMessage)
So why would it do that? Is that my problem?
You need to post more of the markup on the page, and you should at least post most of your email code stub.
And then you need to explain when, where and how the code gets run.
I would assume you have say something like this markup:
<asp:Label ID="Label1" runat="server" Text="Send To :" Width="80px"></asp:Label>
<asp:TextBox ID="txtEmail" runat="server" Height="17px" Width="378px"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Subject :" Width="80px"></asp:Label>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Subject :" Width="80px"></asp:Label>
<br />
<asp:TextBox ID="txtBody" runat="server" Height="223px" TextMode="MultiLine" Width="516px"></asp:TextBox>
<br />
<asp:CheckBox ID="CheckBox1" runat="server" Text="Send as HTML" />
<br />
<br />
<asp:Button ID="Button1" runat="server" Height="40px" Text="Send" Width="155px" />
</div>
The results now look like this:
Ok, and now your send button code can be this:
' get mail server settings from main company record.
Dim rst As DataRow = Myrst("select * from dbo_Company").Rows(0)
Dim MailServer As New SmtpClient
Dim Email As New MailMessage
MailServer.Host = rst("EmailServerName")
MailServer.Credentials = New Net.NetworkCredential(rst("EmailServerUsername").ToString,
rst("EmailServerPassword").ToString)
MailServer.Port = rst("SMTPPort")
MailServer.Host = rst("EmailServerName")
With Email
.From = New MailAddress(rst("EmailServerUsername"))
.To.Add(txtEmail.Text)
.Subject = txtSubject.Text
.Body = txtBody.Text
.IsBodyHtml = CheckBox1.Checked
End With
Try
MailServer.Send(Email)
Catch ex As Exception
Debug.Print(Err.Description)
End Try
But, I fail to see why or how the text boxes with their values can't be used in your code. They should work.
You can try this:
MailMessage.Body = "From: " & NameBox.Text & vbcrlf & _
"City, State: " & TextBox2.Text & vbcrlf & _
"Comments: " & TextBox3.Text
However, if you formatting as HTML, the you could try this:
MailMessage.Body = "From: " & NameBox.Text & "<br/>" & _
"City, State: " & TextBox2.Text & "<br/>" & _
"Comments: " & TextBox3.Text
However, you might want to look close as to how/when/where you are calling this code? I mean, if this is a ajax call (or maybe a file up load event with ajaxfileUPload, then the events triggered do NOT cause a page post-back, and the same goes for a ajax method call into that web page - the controls on the page will ALWAYS be empty unless a page post-back occurs.
Without a page post-back, then the values of all the controls will be empty. This is WHY I asked how/where/when you are running your code, since a regular plane jane page post-back DOES need to occur for code behind to have use of the text box controls on that web page.