Answered by:
send mail THEN check if sucsess with message

Question
-
User876561910 posted
Hi I have this small form I'm creating - this has been forced on me by an update of the host's servers.
Is it possible to check the message was sent and:
If not give a warning
If yes send to another page.
The code works fine but I'm just trying to get it smooth. I'll add validation and anti-spam later
Thanks
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html dir="ltr" xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Some title</title> <script language="vbscript" type="text/vb" runat="server"> Sub MyButton_Click(ByVal sender As Object, ByVal e As EventArgs) Try SendEmail() Catch ex As HttpException End Try End Sub Sub SendEmail() Dim SendResultsTo As String = "MyEmail@some.com" Dim smtpMailServer As String = "mySMTP settings" Dim smtpUsername As String = SendResultsTo Dim smtpPassword As String = "my SMTP password" Dim MailSubject As String = "Some title" Dim MailMessage As string = "From: " & YourName.Text & vbCrLf & vbCrLf & "EMail: " & YourEMail.Text & vbCrLf & vbCrLf & "Message: " & Comments.Text Dim FromEmail As String = "MyEmail@some.com" Dim myMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage() myMessage.To.Add(SendResultsTo) myMessage.From = New System.Net.Mail.MailAddress(FromEmail) myMessage.Subject = MailSubject myMessage.Body = MailMessage myMessage.IsBodyHtml = False Dim MailObj As New System.Net.Mail.SmtpClient MailObj.Host = smtpMailServer MailObj.Credentials = New Net.NetworkCredential(smtpUsername, smtpPassword) MailObj.Send(myMessage) End Sub </script> </head> <body> <form id="form1" runat="server"> <asp:Label runat="server" Text="Your name" id="LabName"></asp:Label> <br /> <asp:TextBox ID="YourName" runat="server" Width="150px" /> <br /> <br /> <asp:Label runat="server" Text="Your E Mail" id="LabMail"></asp:Label> <br /> <asp:TextBox ID="YourEMail" runat="server" Width="150px" /> <br /> <br /> <asp:Label runat="server" Text="Message" id="LabMessage"></asp:Label> <br /> <asp:TextBox ID="Comments" runat="server" TextMode="MultiLine" Rows="10" Width="400px" /> <br /> <br /> <asp:Button runat="server" Text="Send message" OnClick="MyButton_Click" id="Button1" ></asp:Button> </form> </body> </html>
Monday, March 11, 2019 5:36 PM
Answers
-
User-1174608757 posted
Hi janehollin,
According to your description, I have made a sample here.If you want to jump to another page when you send email successfully,I suggest you to use window.location.herf in js,then you could use Page.ClientScript.RegisterStartupScript call the js function in you click event. Then ,if you want to show the exception as you want ,you could set and write code in catch. Here is the demo, I hope it could help you.
aspx:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Some title</title> <script> function aa() { window.location.href("pageyouwant.aspx") /*this will jump to the page you want*/ } function error() { alert("something goes wrong"); /*you could show different message For different exception*/ } </script> <script language="vbscript" type="text/vb" runat="server"> Sub MyButton_Click(ByVal sender As Object, ByVal e As EventArgs) 'You could set the different message based on type of exception Try SendEmail() Page.ClientScript.RegisterStartupScript(Page.GetType(), " ", "aa()", True) Catch ex As Exception Page.ClientScript.RegisterStartupScript(Page.GetType(), " ", "error()", True) End Try End Sub Sub SendEmail() Dim SendResultsTo As String = "MyEmail@some.com" Dim smtpMailServer As String = "mySMTP settings" Dim smtpUsername As String = SendResultsTo Dim smtpPassword As String = "my SMTP password" Dim MailSubject As String = "Some title" Dim MailMessage As String = "From: " & YourName.Text & vbCrLf & vbCrLf & "EMail: " & YourEMail.Text & vbCrLf & vbCrLf & "Message: " & Comments.Text Dim FromEmail As String = "MyEmail@some.com" Dim myMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage() myMessage.To.Add(SendResultsTo) myMessage.From = New System.Net.Mail.MailAddress(FromEmail) myMessage.Subject = MailSubject myMessage.Body = MailMessage myMessage.IsBodyHtml = False Dim MailObj As New System.Net.Mail.SmtpClient MailObj.Host = smtpMailServer MailObj.Credentials = New Net.NetworkCredential(smtpUsername, smtpPassword) MailObj.Send(myMessage) End Sub </script> </head> <body> <form id="form1" runat="server"> <asp:Label runat="server" Text="Your name" id="LabName"></asp:Label> <br /> <asp:TextBox ID="YourName" runat="server" Width="150px" /> <br /> <br /> <asp:Label runat="server" Text="Your E Mail" id="LabMail"></asp:Label> <br /> <asp:TextBox ID="YourEMail" runat="server" Width="150px" /> <br /> <br /> <asp:Label runat="server" Text="Message" id="LabMessage"></asp:Label> <br /> <asp:TextBox ID="Comments" runat="server" TextMode="MultiLine" Rows="10" Width="400px" /> <br /> <br /> <asp:Button runat="server" Text="Send message" OnClick="MyButton_Click" id="Button1" ></asp:Button> </form> </body> </html>
To shows message of different exception, you could follow the link as below:
https://stackoverflow.com/questions/5088842/using-alert-in-response-write-function-in-asp-net
Best Regards
Wei
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 12, 2019 2:23 AM
All replies
-
User475983607 posted
The Try...Catch is the best you can do with SMTP. There is no way to determine what what the SMTP server did with your email message unless the SMTP server specifically tells you during the session/connection. Issue are reflected in a error/exception which the SMTP API handles.
Monday, March 11, 2019 6:13 PM -
User876561910 posted
As a test I loaded the page and then disconnected the wifi.
I got this error messageServer Error in '/SiteName' Application.
The remote name could not be resolved: ' mySMTP settings 'So it’s pointing to this line
Dim smtpMailServer As String = "mySMTP settings"
Is it possible to alter this error message with one of my own?
Monday, March 11, 2019 8:48 PM -
User475983607 posted
That's what try...catch is for.Monday, March 11, 2019 9:52 PM -
User-1174608757 posted
Hi janehollin,
According to your description, I have made a sample here.If you want to jump to another page when you send email successfully,I suggest you to use window.location.herf in js,then you could use Page.ClientScript.RegisterStartupScript call the js function in you click event. Then ,if you want to show the exception as you want ,you could set and write code in catch. Here is the demo, I hope it could help you.
aspx:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Some title</title> <script> function aa() { window.location.href("pageyouwant.aspx") /*this will jump to the page you want*/ } function error() { alert("something goes wrong"); /*you could show different message For different exception*/ } </script> <script language="vbscript" type="text/vb" runat="server"> Sub MyButton_Click(ByVal sender As Object, ByVal e As EventArgs) 'You could set the different message based on type of exception Try SendEmail() Page.ClientScript.RegisterStartupScript(Page.GetType(), " ", "aa()", True) Catch ex As Exception Page.ClientScript.RegisterStartupScript(Page.GetType(), " ", "error()", True) End Try End Sub Sub SendEmail() Dim SendResultsTo As String = "MyEmail@some.com" Dim smtpMailServer As String = "mySMTP settings" Dim smtpUsername As String = SendResultsTo Dim smtpPassword As String = "my SMTP password" Dim MailSubject As String = "Some title" Dim MailMessage As String = "From: " & YourName.Text & vbCrLf & vbCrLf & "EMail: " & YourEMail.Text & vbCrLf & vbCrLf & "Message: " & Comments.Text Dim FromEmail As String = "MyEmail@some.com" Dim myMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage() myMessage.To.Add(SendResultsTo) myMessage.From = New System.Net.Mail.MailAddress(FromEmail) myMessage.Subject = MailSubject myMessage.Body = MailMessage myMessage.IsBodyHtml = False Dim MailObj As New System.Net.Mail.SmtpClient MailObj.Host = smtpMailServer MailObj.Credentials = New Net.NetworkCredential(smtpUsername, smtpPassword) MailObj.Send(myMessage) End Sub </script> </head> <body> <form id="form1" runat="server"> <asp:Label runat="server" Text="Your name" id="LabName"></asp:Label> <br /> <asp:TextBox ID="YourName" runat="server" Width="150px" /> <br /> <br /> <asp:Label runat="server" Text="Your E Mail" id="LabMail"></asp:Label> <br /> <asp:TextBox ID="YourEMail" runat="server" Width="150px" /> <br /> <br /> <asp:Label runat="server" Text="Message" id="LabMessage"></asp:Label> <br /> <asp:TextBox ID="Comments" runat="server" TextMode="MultiLine" Rows="10" Width="400px" /> <br /> <br /> <asp:Button runat="server" Text="Send message" OnClick="MyButton_Click" id="Button1" ></asp:Button> </form> </body> </html>
To shows message of different exception, you could follow the link as below:
https://stackoverflow.com/questions/5088842/using-alert-in-response-write-function-in-asp-net
Best Regards
Wei
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 12, 2019 2:23 AM