📄 form1.vb
字号:
Imports System.Web.Mail
Imports System.Text
Imports System.ServiceProcess
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form 设计工具产生的程序代码 "
Public Sub New()
MyBase.New()
'此呼叫为 Windows Form 设计工具的必要项。
InitializeComponent()
'在 InitializeComponent() 呼叫之后加入所有的初始设定
End Sub
'Form 覆写 Dispose 以清除组件清单。
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 Form 设计工具的必要项
Private components As System.ComponentModel.IContainer
'注意: 以下为 Windows Form 设计工具所需的程序
'您可以使用 Windows Form 设计工具进行修改。
'请勿使用程序代码编辑器来修改这些程序。
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 Label5 As System.Windows.Forms.Label
Friend WithEvents txtTo As System.Windows.Forms.TextBox
Friend WithEvents txtCc As System.Windows.Forms.TextBox
Friend WithEvents txtBcc As System.Windows.Forms.TextBox
Friend WithEvents txtFrom As System.Windows.Forms.TextBox
Friend WithEvents txtSubject As System.Windows.Forms.TextBox
Friend WithEvents txtBody As System.Windows.Forms.TextBox
Friend WithEvents lboxAttach As System.Windows.Forms.ListBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents cboxPriority As System.Windows.Forms.ComboBox
Friend WithEvents filedialogAttach As System.Windows.Forms.OpenFileDialog
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
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.Label5 = New System.Windows.Forms.Label()
Me.txtTo = New System.Windows.Forms.TextBox()
Me.txtCc = New System.Windows.Forms.TextBox()
Me.txtBcc = New System.Windows.Forms.TextBox()
Me.txtFrom = New System.Windows.Forms.TextBox()
Me.txtSubject = New System.Windows.Forms.TextBox()
Me.txtBody = New System.Windows.Forms.TextBox()
Me.lboxAttach = New System.Windows.Forms.ListBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.Label6 = New System.Windows.Forms.Label()
Me.cboxPriority = New System.Windows.Forms.ComboBox()
Me.filedialogAttach = New System.Windows.Forms.OpenFileDialog()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(32, 24)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 0
Me.Label1.Text = "收件者"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(32, 96)
Me.Label2.Name = "Label2"
Me.Label2.TabIndex = 1
Me.Label2.Text = "寄件者"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(32, 48)
Me.Label3.Name = "Label3"
Me.Label3.TabIndex = 2
Me.Label3.Text = "副本"
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(32, 72)
Me.Label4.Name = "Label4"
Me.Label4.TabIndex = 3
Me.Label4.Text = "密副本"
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(32, 120)
Me.Label5.Name = "Label5"
Me.Label5.TabIndex = 4
Me.Label5.Text = "主旨"
'
'txtTo
'
Me.txtTo.Location = New System.Drawing.Point(88, 16)
Me.txtTo.Name = "txtTo"
Me.txtTo.Size = New System.Drawing.Size(264, 22)
Me.txtTo.TabIndex = 6
Me.txtTo.Text = ""
'
'txtCc
'
Me.txtCc.Location = New System.Drawing.Point(88, 40)
Me.txtCc.Name = "txtCc"
Me.txtCc.Size = New System.Drawing.Size(264, 22)
Me.txtCc.TabIndex = 7
Me.txtCc.Text = ""
'
'txtBcc
'
Me.txtBcc.Location = New System.Drawing.Point(88, 64)
Me.txtBcc.Name = "txtBcc"
Me.txtBcc.Size = New System.Drawing.Size(264, 22)
Me.txtBcc.TabIndex = 8
Me.txtBcc.Text = ""
'
'txtFrom
'
Me.txtFrom.Location = New System.Drawing.Point(88, 88)
Me.txtFrom.Name = "txtFrom"
Me.txtFrom.Size = New System.Drawing.Size(264, 22)
Me.txtFrom.TabIndex = 9
Me.txtFrom.Text = ""
'
'txtSubject
'
Me.txtSubject.Location = New System.Drawing.Point(88, 111)
Me.txtSubject.Name = "txtSubject"
Me.txtSubject.Size = New System.Drawing.Size(264, 22)
Me.txtSubject.TabIndex = 10
Me.txtSubject.Text = ""
'
'txtBody
'
Me.txtBody.Location = New System.Drawing.Point(32, 144)
Me.txtBody.Multiline = True
Me.txtBody.Name = "txtBody"
Me.txtBody.Size = New System.Drawing.Size(504, 192)
Me.txtBody.TabIndex = 11
Me.txtBody.Text = ""
'
'lboxAttach
'
Me.lboxAttach.ItemHeight = 12
Me.lboxAttach.Location = New System.Drawing.Point(432, 48)
Me.lboxAttach.Name = "lboxAttach"
Me.lboxAttach.Size = New System.Drawing.Size(104, 88)
Me.lboxAttach.TabIndex = 12
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(368, 48)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(56, 24)
Me.Button1.TabIndex = 13
Me.Button1.Text = "附件"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(456, 344)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(75, 24)
Me.Button2.TabIndex = 14
Me.Button2.Text = "传送"
'
'Label6
'
Me.Label6.Location = New System.Drawing.Point(368, 16)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(56, 23)
Me.Label6.TabIndex = 15
Me.Label6.Text = "重要性"
'
'cboxPriority
'
Me.cboxPriority.Location = New System.Drawing.Point(432, 16)
Me.cboxPriority.Name = "cboxPriority"
Me.cboxPriority.Size = New System.Drawing.Size(104, 20)
Me.cboxPriority.TabIndex = 16
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15)
Me.ClientSize = New System.Drawing.Size(568, 381)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cboxPriority, Me.Label6, Me.Button2, Me.Button1, Me.lboxAttach, Me.txtBody, Me.txtSubject, Me.txtFrom, Me.txtBcc, Me.txtCc, Me.txtTo, Me.Label5, Me.Label4, Me.Label3, Me.Label2, Me.Label1})
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "新邮件"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cboxPriority.Items.AddRange(New String() {"中", "低", "高"})
cboxPriority.SelectedIndex = 0
'确认已经安装 SMTP Service
Dim services() As ServiceController = ServiceController.GetServices
Dim service As ServiceController
Dim blnHasSmtpService As Boolean = False
'寻找 SMTP Service.
For Each service In services
If service.ServiceName.ToLower = "smtpsvc" Then
blnHasSmtpService = True
Exit For
End If
Next
If Not blnHasSmtpService Then
MsgBox("尚未安装 SMTP Service")
End If
'确认 SMTP Service 已经启动, 若尚未启动则启动之
If Not service.Status = ServiceControllerStatus.Running Then
Try
service.Start()
Catch
MessageBox.Show("无法启动 SMTP Service.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
Dim alAttach As ArrayList
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
filedialogAttach.InitialDirectory = "C:\"
filedialogAttach.Filter = "All Files (*.*)|*.*|HTML Files (*.htm;*.html)|*.htm|Microsoft Mail Documents (*.msg)|*.msg|Word Documents (*.doc)|*.doc|Excel Files(*.xl*)|*.xl*|Excel Worksheets (*.xls)|*.xls|Excel Charts (*.xlc)|*.xlc|PowerPoint Presentations (*.ppt)|*.ppt|Text Files (*.txt)|*.txt"
filedialogAttach.FilterIndex = 1
' The OpenFileDialog control only has an Open button, not an OK button.
' However, there is no DialogResult.Open enum so use DialogResult.OK.
If filedialogAttach.ShowDialog() = DialogResult.OK Then
If IsNothing(alAttach) Then
alAttach = New ArrayList()
'清除 ListBox
lboxAttach.Items.Clear()
End If
'加入 ArrayList
alAttach.Add(New MailAttachment(filedialogAttach.FileName))
'只显示文件名称, 去除路径
Dim strFileName() As String = filedialogAttach.FileName.Split(New Char() {CChar("\")})
strFileName.Reverse(strFileName)
lboxAttach.Items.Add(strFileName(0))
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim mail As New MailMessage()
mail.From = txtFrom.Text.Trim
mail.To = txtTo.Text.Trim
mail.Cc = txtCc.Text.Trim
mail.Bcc = txtBcc.Text.Trim
mail.Subject = txtSubject.Text.Trim
mail.Body = txtBody.Text.Trim
mail.Priority = CType(cboxPriority.SelectedIndex, MailPriority)
'ArrayList 储存所有附件文件名
If Not IsNothing(alAttach) Then
Dim ma As Object
For Each ma In alAttach
mail.Attachments.Add(ma)
Next
End If
'联机的 E-Mail server
SmtpMail.SmtpServer = "msa.hinet.net" '"127.0.0.1" '"localhost"
Try
SmtpMail.Send(mail)
'lboxAttach.Items.Clear()
MessageBox.Show("E-mail 已经传送完成!", "E-mail传送", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch exp As Exception
MessageBox.Show("无法传送 E-mail: " & exp.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -