Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Can I ask, please, if something like this script would get through an SMTP server - I have pieced it together after reading a few tutorials.

I cannot seem to include 'FullName' from the Fullname field in my Contact.aspx file (it always generates an error), or how those fields - the user's Fullname, Email, Subject, and the Body (Message) - are populated because they are unknown.

The script looks like this:


VB
Private Sub btnSend_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnSend.Click

'use for external Web hosting service

        Dim smtp As New SmtpClient

'SmtpClient to send contact form - use Try/Catch block to trap sending errors

        Try
            Dim SmtpClient As New SmtpClient
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage

            SmtpClient.Host = "mail.server" 'Web hosting co server
            SmtpClient.Port = 25 'Web hosting co port
            smtp.Credentials = New Net.NetworkCredential("email", "password")
            SmtpClient.EnableSsl = False
            mail = New MailMessage()
            mail.From = New MailAddress("") 'user's email address
            mail.To.Add("info@mysite.com") 'Webmaster's email
            mail.Subject = ""
            mail.Body = ""
            SmtpClient.Send(mail)
            'MsgBox("mail send")

        Catch smtpExc As System.Net.Mail.SmtpException
            
        Catch ex As Exception
                     
        End Try

        Dim target = String.Format("~/thank.aspx?Name={0}", FullName.Text)

        'Redirect user to thank you page

        Response.Redirect(target, True)

    End Sub

Thanks for any advice.
Posted
Updated 21-Nov-14 21:45pm
v2
Comments
Sergey Alexandrovich Kryukov 22-Nov-14 2:16am    
Script?!
—SA
Richard MacCutchan 22-Nov-14 3:46am    
Quite possibly, although your server name does not look valid. What happens when you try it?
Afzaal Ahmad Zeeshan 22-Nov-14 3:59am    
That is a server-side Vb.NET code, and not a script. Seems like you're using Web Forms (by adding the Input field in your web page). I would recommend that you use the Request.Form values, for getting the values not this way. That would be more accurate (IMO).

I did a similar script in Classic ASP:

<![CDATA[<%
'Declaring Variables
Dim smtpserver,youremail,yourpassword,myCopy,ContactUs_Name,ContactUs_Email
Dim ContactUs_Body,Mail_Action,IsError
	
' Edit these 3 values accordingly
smtpserver = "mail.server"
youremail = "info@myplace.com"
yourpassword = "mypassword"
myCopy = "myemail@myplace.com"


' Grabbing variables from the form post
ContactUs_Name = Request("ContactUs_Name")
ContactUs_Email = Request("ContactUs_Email")
ContactUs_Body = Request("ContactUs_Body")
Mail_Action = Request("Mail_Action")

' Check email is valid
......

'Check for form field errors

'If no errors send mail

Dim ObjSendMail
	Set ObjSendMail = CreateObject("CDO.Message") 
     
	'Remote SMTP server configuration
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
.......

'End remote SMTP server configuration 

ObjSendMail.To = youremail
        ObjSendMail.CC = ContactUs_Email
        ObjSendMail.BCC = myCopy
	ObjSendMail.From = ContactUs_Email

ObjSendMail.HTMLBody = strBody

ObjSendMail.Send

'Begin HTML form fields


I was hoping to achieve something like that but in ASP VB.NET, and wondered if the script I first posted would achieve it. I can't test it yet because the ASP.NET project is uncompiled.

I have one or two doubts. How would you script the sender's name (it's the first field on the form), and what is the difference between SmtpServer and SmtpHost as used above.

Thanks again.%>]]>
 
Share this answer
 
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:
VB
'Text' is not a member of 'String'


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:
VB
'Name' is not a member of 'System.Net.Mail.MailMessage'.


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?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900