VB.NETでメールを送信する方法 [プログラミング]
VB.NETでメールを送信する方法について説明する。
ここで説明する内容は、前回のC#.NETでメールを送信する方法のVB.NET版である。
基本的な実装方法はC#.NETと同様なので詳しい説明は割愛する。
C#.NET版と異なる点は、メール送信に関するコードの記述をクラス化(clsMailクラス)しているということで、フォームに入力された内容をclsMailクラスのプロパティに設定してからSendMailプロシージャを実行している。
また、SendMailの戻り値がFalse(メール送信失敗)の場合に失敗の原因分けのコードも追加している。
以下がそのサンプルコード(抜粋)である。
------------------------------------------------------------
Module modMain
Private Function fncSendMail(ByVal pToAddressee As String, ByVal pSubject As String, ByVal pBody As String, _
ByVal pPriority As Net.Mail.MailPriority, ByVal pOwner As Form) As Boolean
'********************************
'メールを送信
'
' pToAddressee : メール宛先
' pSubject : メール件名
' pBody : メール本文
' pPriority : メール優先度
' pOwner : オーナーウインドウインスタンス
'
' Return : True:正常、False:異常
'
'********************************
Dim blnReturn As Boolean = False
Dim clsSnedMail As clsMail = Nothing
Try
'メール送信クラスのインスタンスを生成
clsSnedMail = New clsMail()
With clsSnedMail
'各種プロパティを設定
.SmtpHost = "smtp.xxxxxxxx.ne.jp"
.UserName = "username"
.Password = "password"
.FromAddress = "username@xxxxxxxx.ne.jp"
.ToAddress = pToAddressee
.Subject = pSubject
.Body = pBody
.Priority = pPriority
'メールを送信
Dim intStatusCode As System.Net.Mail.SmtpStatusCode = 0
Dim intReTry As Integer = 0
Do While blnReturn = False
If .SendMail(intStatusCode) = False Then
'メール送信が失敗した場合、ステータスコードをチェック
Select Case intStatusCode
Case Net.Mail.SmtpStatusCode.MailboxBusy, Net.Mail.SmtpStatusCode.MailboxUnavailable
'メールボックスが使用中の場合
If intReTry >= 10 Then
'再送信回数が規定回数に達してもメールが送信できない場合
'
'エラー処理を記述
'
Exit Do
End If
'5秒待機してから再送信
Threading.Thread.Sleep(5000)
intStatusCode = 0
intReTry += 1
Continue Do
Case Net.Mail.SmtpStatusCode.ClientNotPermitted
'クライアントが認証されなかった場合
'
'エラー処理を記述
'
Exit Do
Case Else
'その他のエラー
'
'エラー処理を記述
'
Exit Do
End Select
End If
blnReturn = True
Loop
If blnReturn = False Then
'メール送信に失敗した場合
'
'エラー処理を記述
'
End If
End With
blnReturn = True
Catch ex As Exception
'
'エラー処理を記述
'
blnReturn = False
Finally
'メール送信クラスのインスタンスを破棄
clsSnedMail = Nothing
End Try
Return blnReturn
End Function
End Module
Imports System
Imports System.Net
Imports System.Net.Mail
Public Class clsMail
'*******************************
'メール送信クラス
'*******************************
'定数宣言
Private Const CON_SERVER_PORT As Integer = 25
'メール送信用変数
Private strSmtpHost As String
Private strUserName As String
Private strPassword As String
Private strFromAddress As String
Private strToAddress As String
Private strSubject As String
Private strBody As String
Private intPriority As MailPriority
Public WriteOnly Property SmtpHost() As String
'********************************
'SMTPサーバ名
'********************************
Set(value As String)
strSmtpHost = value
End Set
End Property
'
'この間に同様のプロパティに関する記述をする。
'
Public WriteOnly Property Priority As MailPriority
'********************************
'優先度
'********************************
Set(value As MailPriority)
intPriority = value
End Set
End Property
Public Sub New()
'********************************
'メール送信クラスのコンストラクター
'********************************
'メール送信用変数の初期化
SmtpHost = String.Empty
UserName = String.Empty
Password = String.Empty
FromAddress = String.Empty
ToAddress = String.Empty
Subject = String.Empty
Body = String.Empty
Priority = MailPriority.Normal
End Sub
Public Function SendMail(ByRef pStatusCode As SmtpStatusCode) As Boolean
'********************************
'メール送信を実行
'
' Return : True:正常、False:異常
'
'********************************
Dim blnReturn As Boolean
Dim scMail As SmtpClient = Nothing
Dim msgMail As MailMessage = Nothing
Try
'メール送信用SmtpClientオブジェクトのインスタンスを生成
scMail = New SmtpClient()
'メールメッセージオブジェクトのインスタンスを生成
msgMail = New MailMessage(strFromAddress, strToAddress, strSubject, strBody)
msgMail.Priority = intPriority
'SmtpClientを設定
With scMail
.Host = strSmtpHost
.Port = CON_SERVER_PORT
.DeliveryMethod = SmtpDeliveryMethod.Network
.Credentials = New NetworkCredential(strUserName, strPassword)
End With
'メールを送信
scMail.Send(msgMail)
pStatusCode = SmtpStatusCode.Ok
blnReturn = True
Catch smtpex As SmtpException
pStatusCode = smtpex.StatusCode
blnReturn = False
Catch ex As Exception
pStatusCode = 0
blnReturn = False
Finally
'メール送信用オブジェクトを破棄
msgMail.Dispose()
msgMail = Nothing
scMail = Nothing
End Try
Return blnReturn
End Function
End Class
------------------------------------------------------------
スーパーコピー時計
超人気なブランドの商品を発売こちらは超人気なディオールやシャネルなどのブランドコピーの冬の大量新作をご紹介いたします。爆安セールも進行中ですので、超低価格で逸品を購入できます。すべての商品の種類が豊富なので、好きに選べます。逸品を見逃げさないで、こちらへチックしましょう。}}}}}}
人気の売れ筋商品を多数取り揃えております。
全て激安特価でご提供.お願いします.
★100%品質保証!満足保障!リピーター率100%!
★商品数も大幅に増え、品質も大自信です。
★スタイルが多い、品質がよい、価格が低い!
★顧客は至上 誠実 信用。
★歓迎光臨
★送料無料(日本全国)
https://www.88kopi.com/product/product-8026.html
by 日本超人気のスーパーコピーブランド激安通販専門店 (2020-07-09 07:17)