複数の添付ファイルを含むメールを送信するクラスがあり、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