自動メール送信(excelを介さない場合)
excelを介さないで、自動的にメールを送信する方法は下記のとおりとなる。
あらかじめ、下記のとおりのリストを作成すると効率的でよい。
Dim list As New List(Of String)
'------------------------------------------
'mail の宛先:tx1
'mailのcc:tx2
'mailのBCC:tx3
'添付ファイルの場所:tx4
'Mailの題名:tx5
'Mailのテキスト内容:tx6
'------------------------------------------
Dim tx1 As String = ""
list.Add(mailaddress)
list.Add("")
list.Add("xxx@xxx.co.jp")
list.Add("")
list.Add("メールの題名")
list.Add("メール内容")
'------------------------------------------------------
Imports Outlook = Microsoft.Office.Interop.Outlook
Imports Microsoft.Office.Interop
Public Class email
Public Sub nonauto(ByVal List1 As List(Of String))
Dim tx2 As String = ""
Dim tx3 As String = ""
Dim tx4 As String = ""
Dim tx5 As String = ""
Dim tx6 As String = ""
tx1 = List1(0)
tx2 = List1(1)
tx3 = List1(2)
tx4 = List1(3)
tx5 = List1(4)
tx6 = List1(5)
Try
' ● Outlookアプリケーションの生成(非表示)
Dim olApp As New Outlook.Application
Dim olNs As Outlook.NameSpace = olApp.GetNamespace("MAPI")
olNs.Logon("", "", False, False)
' ● メール作成
Dim mail As Outlook.MailItem
mail = CType(olApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
' ● 各項目の設定
mail.To = tx1
mail.CC = tx2
mail.BCC = tx3
' ● 添付ファイル
If System.IO.File.Exists(tx4) Then
mail.Attachments.Add(tx4)
Else
End If
mail.Subject = tx5
mail.Body = tx6
mail.Display()
Catch ex As Exception
MessageBox.Show("送信エラー: " & ex.Message)
End Try
End Sub