加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net实现邮件发送

发布时间:2020-12-17 07:39:22 所属栏目:百科 来源:网络整理
导读:界面 代码 span style="font-family:KaiTi_GB2312;font-size:24px;"Imports System.Net.MailImports System.TextImports System.SecurityImports System.Net.SocketsPublic Class frmEmail Private Sub btnSend_Click(sender As Object,e As EventArgs) Hand

界面


代码

<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>

结果显示



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



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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读