可以通过以下步骤实现:
Imports System.Net
Imports System.Net.Mail
MailMessage
对象,并设置发送方、接收方、主题和正文:Dim message As New MailMessage()
message.From = New MailAddress("sender@example.com")
message.To.Add("recipient@example.com")
message.Subject = "邮件主题"
message.Body = "邮件正文"
Dim attachment As New Attachment("文件路径")
message.Attachments.Add(attachment)
SmtpClient
对象,并设置SMTP服务器的相关信息:Dim smtpClient As New SmtpClient()
smtpClient.Host = "smtp.example.com"
smtpClient.Port = 587
smtpClient.EnableSsl = True
smtpClient.Credentials = New NetworkCredential("username", "password")
请注意,这里的SMTP服务器地址、端口号、用户名和密码需要根据实际情况进行设置。
smtpClient.Send(message)
完整的代码示例:
Imports System.Net
Imports System.Net.Mail
Module Module1
Sub Main()
Dim message As New MailMessage()
message.From = New MailAddress("sender@example.com")
message.To.Add("recipient@example.com")
message.Subject = "邮件主题"
message.Body = "邮件正文"
Dim attachment As New Attachment("文件路径")
message.Attachments.Add(attachment)
Dim smtpClient As New SmtpClient()
smtpClient.Host = "smtp.example.com"
smtpClient.Port = 587
smtpClient.EnableSsl = True
smtpClient.Credentials = New NetworkCredential("username", "password")
smtpClient.Send(message)
End Sub
End Module
附加说明:
MailMessage
类用于表示电子邮件的消息内容,可以设置发送方、接收方、主题、正文等信息。SmtpClient
类用于发送电子邮件,需要设置SMTP服务器的相关信息,如主机地址、端口号、是否启用SSL等。Attachment
类用于表示附件,可以通过Attachments
属性添加到MailMessage
对象中。推荐的腾讯云相关产品:腾讯云邮件推送(https://cloud.tencent.com/product/ses)
领取专属 10元无门槛券
手把手带您无忧上云