webform1.aspx.vb文件如下:————————————————————————————
Imports System.Web.Mail
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents btnsend As System.Web.UI.WebControls.Button
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private Sub btnsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsend.Click
Dim email As New MailMessage
email.From = "sombody@some.com"
email.To = "somebody@some.com"
email.Subject = "hello!!!"
email.Body = "测试邮件"
email.BodyFormat = MailFormat.Html
SmtpMail.Send(email)
End Sub
End Class
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
' Perform validation on the To and From email address fields, which are
' both required for sending an email.
Try
ValidateEmailAddress(txtFrom)
Catch ex As Exception
txtFrom.Select(0, txtFrom.Text.Length)
' Set the ErrorProvider error with the text to display.
erpEmailAddresses.SetError(txtFrom, ex.Message)
Exit Sub
End Try
Try
ValidateEmailAddress(txtTo)
Catch exp As Exception
txtTo.Select(0, txtTo.Text.Length)
' Set the ErrorProvider error with the text to display.
erpEmailAddresses.SetError(txtTo, exp.Message)
Exit Sub
End Try
' Use the StringBuilder class instead of traditional string concatenation.
' It is optimized for building strings because it is capable of modifying
' the underlying buffer instead of having to make a copy of the string for
' each concatenation. Consult the SDK documentation for more information on
' this new class.
Dim sb As New StringBuilder()
' Build the email message body.
sb.Append("The following email was sent to you from the Send Mail How-To " & _
"sample application:")
sb.Append(vbCrLf)
sb.Append(vbCrLf)
sb.Append("SUBJECT: ")
sb.Append(Trim(txtSubject.Text))
sb.Append(vbCrLf)
sb.Append(vbCrLf)
sb.Append("MESSAGE: ")
sb.Append(Trim(txtBody.Text))
sb.Append(vbCrLf)
' Creating a mail message is as simple as instantiating a class and
' setting a few properties.
Dim mailMsg As New MailMessage()
With mailMsg
.From = txtFrom.Text.Trim
.To = txtTo.Text.Trim
.Cc = txtCC.Text.Trim
.Bcc = txtBCC.Text.Trim
.Subject = txtSubject.Text.Trim
.Body = sb.ToString
.Priority = CType(cboPriority.SelectedIndex, MailPriority)
If Not IsNothing(arlAttachments) Then
Dim mailAttachment As Object
For Each mailAttachment In arlAttachments
.Attachments.Add(mailAttachment)
Next
End If
End With
' Set the SmtpServer name. This can be any of the following depending on
' your local security settings:
' a) Local IP Address (assuming your local machine's SMTP server has the
' right to send messages through a local firewall (if present).
' b) 127.0.0.1 the loopback of the local machine.
' c) "smarthost" or the name or the IP address of the exchange server you
' utilize for messaging. This is usually what is needed if you are behind
' a corporate firewall.
' See the Readme file for more information.
SmtpMail.SmtpServer = "smarthost"
' Use structured error handling to attempt to send the email message and
' provide feedback to the user about the success or failure of their
' attempt.
Try
SmtpMail.Send(mailMsg)
lstAttachments.Items.Clear()
lstAttachments.Items.Add("(No Attachments)")
MessageBox.Show("Your email has been successfully sent!", _
"Email Send Status", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Catch exp As Exception
MessageBox.Show("The following problem occurred when attempting to " & _
"send your email: " & exp.Message, _
Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub