level 7
暴牙归来
楼主

Imports System.net.MailImports System.Text
Imports System.Security
Imports System.Net.Sockets
Public Class 邮件发送
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk.CheckedChanged
If chk.Checked = True Then
txtName.Enabled = True
txtPassword.Enabled = True
Else
txtName.Enabled = False
txtPassword.Enabled = False
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Dim mail As New MailMessage()
mail.From = New MailAddress(txtFrom.Text)
mail.To.Add(txtTo.Text)
'set the content
mail.Subject = txtSubject.Text
mail.Body = txtContent.Text
''specify the priority of the mail message
mail.Priority = MailPriority.High
'to add custom headers, we use the Headers.Add(...) method to add headers to the
'.Headers collection
mail.Headers.Add("X-Company", "JOHN NG SDN BHD")
mail.Headers.Add("X-Location", "Malaysia")
Dim smtp As New SmtpClient(txtSMTP.Text)
If chk.Checked = True Then
smtp.Credentials = New System.Net.NetworkCredential(txtName.Text, txtPassword.Text)
End If
'检测是否有附件
If TextBox1.Text.Trim <> "" Then ''带附件
If Dir(TextBox1.Text.Trim) <> "" Then
Dim attach As Net.Mail.Attachment = New Net.Mail.Attachment(TextBox1.Text)
mail.Attachments.Add(attach)
End If
End If
Try
smtp.Send(mail)
MsgBox("恭喜,邮件已经发送到指定的邮件地址.", MsgBoxStyle.Information, "邮件发送示例")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "邮件发送失败")
End Try
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.OpenFileDialog1.Title = "选择附件"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
End If
End Sub
End Class