vb.net实现邮件发送

前端之家收集整理的这篇文章主要介绍了vb.net实现邮件发送前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

界面


代码

<span style="font-family:KaiTi_GB2312;font-size:24px;">Imports System.Net.Mail
Imports System.Text
Imports System.Security
Imports System.Net.Sockets
Public Class frmEmail

    Private Sub btnSend_Click(sender As Object,e As EventArgs) Handles btnSend.Click
        '创建邮件
        Dim mail As New MailMessage()
        '发件人邮箱
        mail.From = New MailAddress(txtFrom.Text)
        '如果有多个,可以多次添加
        mail.To.Add(txtTo.Text)

        '邮件主题
        mail.Subject = txtSubject.Text
        '邮件内容
        mail.Body = txtContent.Text

        '指定邮件的优先级
        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
        Try
            smtp.Send(mail)
            MsgBox("恭喜,邮件已经发送到指定的邮件地址.",MsgBoxStyle.Information,"邮件发送示例")
        Catch ex As Exception
            MsgBox(ex.Message,MsgBoxStyle.Critical,"邮件发送失败")
        End Try
    End Sub

    Private Sub chk_CheckedChanged(sender As Object,e As 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
End Class</span>

结果显示



此时我们查看收件人的邮箱:



这一步,也是在优化机房的时候想到,如果有什么问题,用户可以通过发送邮件来进行询问。

原文链接:https://www.f2er.com/vb/257004.html

猜你在找的VB相关文章