I have managed to tidy up the code but I am still getting a couple of errors:
This is the name field in my form:
<asp:label runat="server" id="Label1" xmlns:asp="#unknown">Name</asp:label>
<asp:textbox runat="server" id="Name" columns="35" text="Name" xmlns:asp="#unknown" />
and, hopefully, some improved code:
Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
Dim myMessage As New MailMessage
Dim Smtpserver As New SmtpClient
Dim Name As String
Dim Email As String
Dim Subject As String
Dim Message As String
'Request.Form - extract data from form fields
Dim Label1 As String = Request.Form("Name")
Dim Label2 As String = Request.Form("Email")
Dim Label3 As String = Request.Form("Subject")
Dim Label4 As String = Request.Form("Message")
'create the mail message
myMessage.Name = Name
myMessage.From = New MailAddress("Email") 'User's email
myMessage.To.Add(New MailAddress("info@mysite.com")) 'Company email
myMessage.Bcc.Add(New MailAddress("me@mysite.com")) 'My personal copy
myMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
myMessage.IsBodyHtml = True
myMessage.Priority = MailPriority.High
myMessage.Subject = Subject
myMessage.Body = Message
Smtpserver.DeliveryMethod = SmtpDeliveryMethod.Network
Smtpserver.Host = ("Stmp.mysite.com")
'Smtpserver.Host = System.Configuration.ConfigurationManager()
Smtpserver.Port = 25
Dim basicAuthenticationInfo As New System.Net.NetworkCredential("email", "PWD")
Smtpserver.Credentials = basicAuthenticationInfo
Try
Smtpserver.Send(myMessage)
MsgBox("sent")
Catch ex As Exception
MsgBox(ex.Message)
myMessage.Dispose()
myMessage = Nothing
Smtpserver = Nothing
End Try
Dim target = String.Format("~/thank.aspx?Name={0}", Name.Text)
'Redirect user to thank you page
Response.Redirect(target, True)
End Sub
End Class
I get an error that says:
This refers to the 'Text' that is part of the page redirection at the end, although I have declared 'Text As String' at the top of the code.
The other error is strange:
System.Net.Mail.MailMessage does not exist anywhere in Solution Explorer (I have done a full Solution 'Find'. This STMP code uses: Imports System.Net.Mail at the top of the file.
Any advice, please?