📄 form1.vb
字号:
Imports System.Web.Mail
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents txtFrom As System.Windows.Forms.TextBox
Friend WithEvents txtTo As System.Windows.Forms.TextBox
Friend WithEvents txtCC As System.Windows.Forms.TextBox
Friend WithEvents txtSubject As System.Windows.Forms.TextBox
Friend WithEvents txtBody As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents btnSend As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.txtFrom = New System.Windows.Forms.TextBox()
Me.txtTo = New System.Windows.Forms.TextBox()
Me.txtCC = New System.Windows.Forms.TextBox()
Me.txtSubject = New System.Windows.Forms.TextBox()
Me.txtBody = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.Label4 = New System.Windows.Forms.Label()
Me.btnSend = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'txtFrom
'
Me.txtFrom.Location = New System.Drawing.Point(72, 8)
Me.txtFrom.Name = "txtFrom"
Me.txtFrom.Size = New System.Drawing.Size(400, 21)
Me.txtFrom.TabIndex = 0
Me.txtFrom.Text = ""
'
'txtTo
'
Me.txtTo.Location = New System.Drawing.Point(72, 40)
Me.txtTo.Name = "txtTo"
Me.txtTo.Size = New System.Drawing.Size(400, 21)
Me.txtTo.TabIndex = 0
Me.txtTo.Text = ""
'
'txtCC
'
Me.txtCC.Location = New System.Drawing.Point(72, 72)
Me.txtCC.Name = "txtCC"
Me.txtCC.Size = New System.Drawing.Size(400, 21)
Me.txtCC.TabIndex = 0
Me.txtCC.Text = ""
'
'txtSubject
'
Me.txtSubject.Location = New System.Drawing.Point(72, 104)
Me.txtSubject.Name = "txtSubject"
Me.txtSubject.Size = New System.Drawing.Size(400, 21)
Me.txtSubject.TabIndex = 0
Me.txtSubject.Text = ""
'
'txtBody
'
Me.txtBody.Location = New System.Drawing.Point(8, 136)
Me.txtBody.Multiline = True
Me.txtBody.Name = "txtBody"
Me.txtBody.Size = New System.Drawing.Size(464, 152)
Me.txtBody.TabIndex = 0
Me.txtBody.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(48, 16)
Me.Label1.TabIndex = 1
Me.Label1.Text = "发件人"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(16, 40)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(48, 16)
Me.Label2.TabIndex = 1
Me.Label2.Text = "收件人"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(16, 72)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(48, 16)
Me.Label3.TabIndex = 1
Me.Label3.Text = "抄送"
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(16, 104)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(48, 16)
Me.Label4.TabIndex = 1
Me.Label4.Text = "主题"
'
'btnSend
'
Me.btnSend.Location = New System.Drawing.Point(40, 296)
Me.btnSend.Name = "btnSend"
Me.btnSend.Size = New System.Drawing.Size(72, 24)
Me.btnSend.TabIndex = 2
Me.btnSend.Text = "发送"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(480, 326)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSend, Me.Label1, Me.txtFrom, Me.txtTo, Me.txtCC, Me.txtSubject, Me.txtBody, Me.Label2, Me.Label3, Me.Label4})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
'把邮件内容转换成所需格式
Dim sBody, sLine As String
For Each sLine In txtBody.Lines
sBody += sLine
sBody += vbCrLf
Next
'构造电子邮件消息
Dim mailMsg As New MailMessage()
With mailMsg
.From = txtFrom.Text.Trim
.To = txtTo.Text.Trim
.Cc = txtCC.Text.Trim
.Subject = txtSubject.Text.Trim
.Body = sBody
End With
'设置电子邮件服务器名
SmtpMail.SmtpServer = "localhost"
'发送电子邮件
Try
SmtpMail.Send(mailMsg)
MessageBox.Show("恭喜,您的电子邮件已经成功发送!", _
"邮件发送状态", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("邮件未能成功发送!", "邮件发送状态", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -