是一种常见的邮件发送方式,可以在发送邮件的同时抄送给其他收件人或者将某些收件人隐藏起来。
在vb.net中,可以使用System.Net.Mail命名空间下的SmtpClient和MailMessage类来实现邮件的发送。以下是一个示例代码:
Imports System.Net.Mail
Sub SendEmail()
Try
Dim fromAddress As New MailAddress("your_email@example.com", "Your Name")
Dim toAddress As New MailAddress("recipient@example.com", "Recipient Name")
Dim ccAddress As New MailAddress("cc@example.com", "CC Name")
Dim bccAddress As New MailAddress("bcc@example.com", "BCC Name")
Dim fromPassword As String = "your_password"
Dim subject As String = "Test Email"
Dim body As String = "This is a test email."
Dim smtpClient As New SmtpClient()
smtpClient.Host = "smtp.example.com"
smtpClient.Port = 587
smtpClient.EnableSsl = True
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
smtpClient.UseDefaultCredentials = False
smtpClient.Credentials = New System.Net.NetworkCredential(fromAddress.Address, fromPassword)
Dim mail As New MailMessage()
mail.From = fromAddress
mail.To.Add(toAddress)
mail.CC.Add(ccAddress)
mail.Bcc.Add(bccAddress)
mail.Subject = subject
mail.Body = body
smtpClient.Send(mail)
Console.WriteLine("Email sent successfully.")
Catch ex As Exception
Console.WriteLine("Failed to send email. Error message: " & ex.Message)
End Try
End Sub
在上述代码中,需要替换以下内容:
your_email@example.com
:发送邮件的邮箱地址Your Name
:发送邮件的姓名recipient@example.com
:收件人的邮箱地址CC Name
:抄送人的姓名cc@example.com
:抄送人的邮箱地址BCC Name
:密件抄送人的姓名bcc@example.com
:密件抄送人的邮箱地址your_password
:发送邮件的邮箱密码smtp.example.com
:SMTP服务器地址这段代码会发送一封带有主题和正文内容的测试邮件,并将邮件抄送给指定的收件人和抄送人。
在实际应用中,可以根据需要动态设置邮件的收件人、抄送人、密件抄送人、主题和正文内容。另外,还可以通过附件相关的方法添加附件。
腾讯云提供了云邮件服务(https://cloud.tencent.com/product/ce)来帮助用户更方便地发送电子邮件。该服务提供了高可用、高性能的邮件发送能力,并且支持API调用,可以方便地集成到应用程序中。
领取专属 10元无门槛券
手把手带您无忧上云