Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

This resource has not currently been approved, and is not currently linked to from our directory of resources. It is being displayed here for preview by the author and moderators only.
Rated
Read 753 times

Contents

Related Categories

Send e-mail messages with ASP.NET - SendMail.aspx.vb

SendMail.aspx.vb

<div style="font-family: Verdana, Sans-Serif; font-size: 8pt; color: black; background-color: #eeeeee;">
Imports System.Net.Mail
Imports System.Net.Mail.SmtpClient
Imports System.IO
 
 
 Partial Class SendMail
 Inherits System.Web.UI.Page
 
 Private Sub ClearForm()
 Me.SenderTextBox.Text = ""
 Me.SubjectTextBox.Text = ""
 Me.MessageTextBox.Text = ""
 End Sub
 
 Protected Overrides Sub OnLoadComplete(ByVal e As System.EventArgs)
 MyBase.OnLoadComplete(e)
 '// Clear the form
 ClearForm()
 End Sub
 
 Protected Sub sendMessageButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendMessageButton.Click
 
 '// Grab the data for your message
 Dim _theSender As String = Me.SenderTextBox.Text
 Dim _theReceiver As String = "yourname@provider.com" '// "insert your e-mail address here"
 Dim _theSubject As String = Me.SubjectTextBox.Text
 Dim _theMessage As String = Me.MessageTextBox.Text
 
 '// Compose the message
 Dim theMessage As New MailMessage(_theSender, _theReceiver, _theSubject, _theMessage)
 theMessage.Priority = MailPriority.Normal
 
 '// Insert your internet provider mail server address
 '// this is my isp mail server address; you should change it with your own
 Dim theHost As New SmtpClient("mail.upcnet.ro")
 
 '// Create the attachment
 Dim attachedFile As Attachment = New Attachment(Me.AttachmentFileUpload.PostedFile.InputStream, Me.AttachmentFileUpload.FileName)
 
 Try
 If (Me.SendAsHtmlRadioButton.Checked = True) Then
 Try
 '// Verify if file attached
 If Not (String.IsNullOrEmpty(Me.AttachmentFileUpload.FileName) OrElse (Me.AttachmentFileUpload.PostedFile Is Nothing)) Then
 '// Attach the file
 theMessage.Attachments.Add(attachedFile)
 Else
 '// There is no need to treat this error here.
 '// The RequiredFieldValidator will do the job
 End If
 
 '// Get your message content and declare it as HTML content
 theMessage.Body = _theMessage
 theMessage.IsBodyHtml = True
 
 '// Send the message
 theHost.Send(theMessage)
 
 '// Show success message
 Me.ErrorMessageLabel.Text = "Success: The message was sent"
 
 Catch ex As Exception
 '// Show error message
 Me.ErrorMessageLabel.Text = "Error: The message was not sent"
 
 End Try
 
 ElseIf (Me.SendAsTextRadioButton.Checked = True) Then
 Try
 '// Verify if file attached
 If Not (String.IsNullOrEmpty(Me.AttachmentFileUpload.FileName) OrElse (Me.AttachmentFileUpload.PostedFile Is Nothing)) Then
 '// Attach the file
 theMessage.Attachments.Add(attachedFile)
 Else
 '// There is no need to treat this error here.
 '// The RequiredFieldValidator will do the job
 End If
 
 '// Get your message content and declare it as plain Text content
 theMessage.Body = _theMessage
 theMessage.IsBodyHtml = False
 
 '// Send the message
 theHost.Send(theMessage)
 
 '// Show success message
 Me.ErrorMessageLabel.Text = "Success: The message was sent"
 
 Catch ex As Exception
 '// Show error message
 Me.ErrorMessageLabel.Text = "Error: The message was not sent"
 End Try
 End If
 
 Catch ex As Exception
 '// Show error message
 Me.ErrorMessageLabel.Text = "Error: The message was not sent"
 
 End Try
 
 End Sub
 
 End Class
</div>

Comments