📜  VB.Net-发送电子邮件

📅  最后修改于: 2020-11-19 09:03:47             🧑  作者: Mango


VB.Net允许从您的应用程序发送电子邮件。 System.Net.Mail命名空间包含用于将电子邮件发送到简单邮件传输协议(SMTP)服务器进行传递的类。

下表列出了其中一些常用的类-

Sr.No. Class & Description
1

Attachment

Represents an attachment to an e-mail.

2

AttachmentCollection

Stores attachments to be sent as part of an e-mail message.

3

MailAddress

Represents the address of an electronic mail sender or recipient.

4

MailAddressCollection

Stores e-mail addresses that are associated with an e-mail message.

5

MailMessage

Represents an e-mail message that can be sent using the SmtpClient class.

6

SmtpClient

Allows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP).

7

SmtpException

Represents the exception that is thrown when the SmtpClient is not able to complete a Send or SendAsync operation.

SmtpClient类

SmtpClient类允许应用程序使用简单邮件传输协议(SMTP)发送电子邮件。

以下是SmtpClient类的一些常用属性-

Sr.No. Property & Description
1

ClientCertificates

Specifies which certificates should be used to establish the Secure Sockets Layer (SSL) connection.

2

Credentials

Gets or sets the credentials used to authenticate the sender.

3

EnableSsl

Specifies whether the SmtpClient uses Secure Sockets Layer (SSL) to encrypt the connection.

4

Host

Gets or sets the name or IP address of the host used for SMTP transactions.

5

Port

Gets or sets the port used for SMTP transactions.

6

Timeout

Gets or sets a value that specifies the amount of time after which a synchronous Send call times out.

7

UseDefaultCredentials

Gets or sets a Boolean value that controls whether the DefaultCredentials are sent with requests.

以下是SmtpClient类的一些常用方法-

Sr.No. Method & Description
1

Dispose

Sends a QUIT message to the SMTP server, gracefully ends the TCP connection, and releases all resources used by the current instance of the SmtpClient class.

2

Dispose(Boolean)

Sends a QUIT message to the SMTP server, gracefully ends the TCP connection, releases all resources used by the current instance of the SmtpClient class, and optionally disposes of the managed resources.

3

OnSendCompleted

Raises the SendCompleted event.

4

Send(MailMessage)

Sends the specified message to an SMTP server for delivery.

5

Send(String, String, String, String)

Sends the specified e-mail message to an SMTP server for delivery. The message sender, recipients, subject, and message body are specified using String objects.

6

SendAsync(MailMessage, Object)

Sends the specified e-mail message to an SMTP server for delivery. This method does not block the calling thread and allows the caller to pass an object to the method that is invoked when the operation completes.

7

SendAsync(String, String, String, String, Object)

Sends an e-mail message to an SMTP server for delivery. The message sender, recipients, subject, and message body are specified using String objects. This method does not block the calling thread and allows the caller to pass an object to the method that is invoked when the operation completes.

8

SendAsyncCancel

Cancels an asynchronous operation to send an e-mail message.

9

SendMailAsync(MailMessage)

Sends the specified message to an SMTP server for delivery as an asynchronous operation.

10

SendMailAsync(String, String, String, String)

Sends the specified message to an SMTP server for delivery as an asynchronous operation. . The message sender, recipients, subject, and message body are specified using String objects.

11

ToString

Returns a string that represents the current object.

下面的示例演示如何使用SmtpClient类发送邮件。在这方面要注意以下几点-

  • 您必须指定用于发送电子邮件的SMTP主机服务器。对于不同的主机服务器,“主机”和“端口”属性将有所不同。我们将使用gmail服务器。

  • 如果SMTP服务器需要,则需要提供身份验证凭据

  • 您还应该分别使用MailMessage.FromMailMessage.To属性提供发件人的电子邮件地址和收件人的电子邮件地址。

  • 您还应该使用MailMessage.Body属性指定邮件内容。

在此示例中,让我们创建一个将发送电子邮件的简单应用程序。采取以下步骤-

  • 在表单中添加三个标签,三个文本框和一个按钮控件。

  • 将标签的文本属性分别更改为-“发件人”,“收件人:”和“消息:”。

  • 将文本的名称属性分别更改为txtFrom,txtTo和txtMessage。

  • 将按钮控件的text属性更改为“发送”

  • 在代码编辑器中添加以下代码。

Imports System.Net.Mail
Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ' Set the caption bar text of the form.   
      Me.Text = "tutorialspoint.com"
   End Sub

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Try
         Dim Smtp_Server As New SmtpClient
         Dim e_mail As New MailMessage()
         Smtp_Server.UseDefaultCredentials = False
         Smtp_Server.Credentials = New Net.NetworkCredential("username@gmail.com", "password")
         Smtp_Server.Port = 587
         Smtp_Server.EnableSsl = True
         Smtp_Server.Host = "smtp.gmail.com"

         e_mail = New MailMessage()
         e_mail.From = New MailAddress(txtFrom.Text)
         e_mail.To.Add(txtTo.Text)
         e_mail.Subject = "Email Sending"
         e_mail.IsBodyHtml = False
         e_mail.Body = txtMessage.Text
         Smtp_Server.Send(e_mail)
         MsgBox("Mail Sent")

      Catch error_t As Exception
         MsgBox(error_t.ToString)
      End Try
   End Sub
  • 您必须提供Gmail地址和真实密码以获取凭据。

  • 使用Microsoft Visual Studio工具栏上的“开始”按钮执行并运行上述代码后,它将显示以下窗口,您将使用该窗口发送电子邮件,然后自己尝试。

从VB.Net发送电子邮件