VB.net 发送邮件 Email

================================================================================

这个是在开发自动发报表程序中使用到的,下面的内容有几段,来自不同的地方,有网上的,也有日本人提供给我的样式(其实是敷衍)

================================================================================
首先添加引用

Imports System.Net.Mail

Dim client As New System.Net.Mail.SmtpClient()
client.Host = "smtp.163.com"
client.UseDefaultCredentials = False
client.Credentials = New System.Net.NetworkCredential("自已的163邮件地址", "密码")
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network
Dim message As New System.Net.Mail.MailMessage("自已的163邮件地址", "收件人邮件地址")
message.Subject = "主题测试"
message.Body = "asp.net发送邮件"
message.BodyEncoding = System.Text.Encoding.UTF8
message.IsBodyHtml = True
'添加附件需将(附件先上传到服务器)
Dim data As New System.Net.Mail.Attachment("Upload/File1.rar", System.Net.Mime.MediaTypeNames.Application.Octet)
message.Attachments.Add(data)
Try
    client.Send(message)
    Response.Write("Email successfully send.")
Catch ex As Exception
    Response.Write("Send Email Failed." + ex.ToString())
End Try

===========================================

   '送信者
        Dim senderMail As String = "邮件地址"
        '宛先
        Dim recipientMail As String = "邮件地址"
        '件名
        Dim subject As String = "業務連絡"
        '本文
        Dim body As String = "おはようございます"

        Dim sc As New System.Net.Mail.SmtpClient()
        'SMTPサーバーを指定する
        sc.Host = "192.168.0.3"
        Dim mms As New System.Net.Mail.MailMessage

        'メールを送信する
        Try
            sc.Send(senderMail, recipientMail, subject, body)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try

=============================
创建发件连接,根据你的发送邮箱的SMTP设置填充
        Dim smtp As New System.Net.Mail.SmtpClient("smtp.163.com", 25)

        '发件邮箱身份验证,参数分别为 发件邮箱登录名和密码
        smtp.Credentials = New System.Net.NetworkCredential("用户名", "密码")

        '创建邮件
        Dim mail As New System.Net.Mail.MailMessage()

        '邮件主题
        mail.Subject = "邮件主题"

        '主题编码
        mail.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312")

        '邮件正文件编码
        mail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312")

        '发件人邮箱
        mail.From = New System.Net.Mail.MailAddress("[email protected]")

        '邮件优先级
        mail.Priority = System.Net.Mail.MailPriority.Normal

        'HTML格式的邮件,为false则发送纯文本邮箱
        mail.IsBodyHtml = True

        '邮件内容
        mail.Body = "邮件正文内容"

        '添加收件人,如果有多个,可以多次添加
        mail.To.Add("收件人地址")

        '定义附件,参数为附件文件名,包含路径,推荐使用绝对路径
        '如果不需要附件,下面三行可以不要
        Dim a As New System.Net.Mail.Attachment("附件文件")

        '附件文件名,用于收件人收到附件时显示的名称
        a.Name = "附件文件名"

        '加入附件,可以多次添加
        mail.Attachments.Add(a)


        '发送邮件
        Try

            smtp.Send(mail)
            MessageBox.Show("发送成功")

        Catch

            MessageBox.Show("发送失败")

        Finally

            mail.Dispose()
        End Try

你可能感兴趣的:(VB.net 发送邮件 Email)