我有一门课可以发送带有多个附件的电子邮件,它使用 gmail,但我如何使用 Outlook 发送电子邮件?
Imports System.Net.Mail
Imports System.Net.Mime
Public Sub SendThis(ByVal SubjectText As String, _
ByVal BodyText As String, _
ByVal FromAddress As String, _
ByVal ToAddress As String, _
Optional ByVal FileName As Collection = Nothing _
)
Try
Dim email As New Net.Mail.MailMessage(FromAddress, ToAddress)
email.Subject = SubjectText
email.Body = BodyText
If Not FileName Is Nothing Then
For Each Name As String In FileName
Dim attach As New Net.Mail.Attachment(Name) 'Includes Path
email.Attachments.Add(attach)
Next
For Each At As Attachment In email.Attachments
At.TransferEncoding() = Net.Mime.TransferEncoding.Base64
Next
End If
Dim TheSmtp As New SmtpClient(YourSmtpServerName, 587)
TheSmtp.Credentials = New Net.NetworkCredential("abc@gmail.com","MYPASS")
TheSmtp.DeliveryMethod = SmtpDeliveryMethod.Network
TheSmtp.Send(email)
email.Attachments.Clear()
TheSmtp = Nothing
email = Nothing
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message, "HFB", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
我将函数称为:
Private Sub BtnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSend.Click
Dim BodyText As String
Dim SubjectText As String
Dim FromAddress As String
Dim ToAddress As String
Dim Filename As New Collection
If Me.LstBxAttach.Items.Count > 0 Then
For Each TheItem As String In LstBxAttach.Items
Filename.Add(TheItem)
Next
End If
SubjectText = Me.TbSubject.Text
BodyText = Me.TbBody.Text
SendThis(SubjectText, _
BodyText, _
"from@example.com", _
"to@example.com", _
Filename _
)
SubjectText = ""
BodyText = ""
FromAddress = ""
ToAddress = ""
MessageBox.Show("Sent!", "HFB", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub