Imports System.Net.Mail

'=========================================================================
'クラス名称：メール送信クラス
'機能      ：メール情報の各種設定，メールの送信
'備考      ：なし
'=========================================================================
Public Class MailSender
    Private Shared singleton As MailSender = New MailSender

    ' メール送信フラグ
    Private m_bSendFlag As Boolean
    ' 添付ファイルフラグ
    Private m_bAttachFlag As Boolean
    ' 送信元アドレス
    Private m_strFrom As String
    ' 送信先アドレス
    Private m_strTo As String
    ' SMTPサーバ名
    Private m_strSMTP As String

    ' メール送信フラグのプロパティ
    Public Property sendFlag() As Boolean
        Get
            Return m_bSendFlag
        End Get
        Set(ByVal Value As Boolean)
            m_bSendFlag = Value
        End Set
    End Property

    ' 添付ファイルフラグのプロパティ
    Public Property attachFlag() As Boolean
        Get
            Return m_bAttachFlag
        End Get
        Set(ByVal Value As Boolean)
            m_bAttachFlag = Value
        End Set
    End Property

    ' 送信元アドレスのプロパティ
    Public Property MailFrom() As String
        Get
            Return m_strFrom
        End Get
        Set(ByVal Value As String)
            m_strFrom = Value
        End Set
    End Property

    ' 送信先アドレスのプロパティ
    Public Property MailTo() As String
        Get
            Return m_strTo
        End Get
        Set(ByVal Value As String)
            m_strTo = Value
        End Set
    End Property

    ' SMTPサーバ名のプロパティ
    Public Property SMTPServer() As String
        Get
            Return m_strSMTP
        End Get
        Set(ByVal Value As String)
            m_strSMTP = Value
        End Set
    End Property

    '*********************************************************************
    '関数名称  ：コンストラクタ(外部からのアクセス不可)
    '機能      ：各種設定の初期化
    '引数      ：なし
    '戻り値    ：なし
    '備考      ：なし
    '*********************************************************************
    Private Sub New()
        m_bSendFlag = False
        m_bAttachFlag = False
        m_strFrom = "test@sample.ne.jp"
        m_strTo = "test@sample.ne.jp"
        m_strSMTP = "mail.smtp.ne.jp"
    End Sub

    '*********************************************************************
    '関数名称  ：インスタンスの取得
    '機能      ：唯一のインスタンスを取得する
    '引数      ：なし
    '戻り値    ：MailSender : インスタンス
    '備考      ：なし
    '*********************************************************************
    Public Shared Function GetInstance() As MailSender
        Return singleton
    End Function

    '*********************************************************************
    '関数名称  ：メール送信処理
    '機能      ：メールを送信する
    '引数      ：subject :メールの件名
    '            body    :メールの本文
    '戻り値    ：なし
    '備考      ：なし
    '*********************************************************************
    Public Sub send(ByVal subject As String, ByVal body As String)
        Dim client As SmtpClient
        client = New SmtpClient(m_strSMTP)
        'Mail.SmtpMail.SmtpServer = m_strSMTP

        'Sendメソッドで送信する
        Try
            'Mail.SmtpMail.Send(m_strFrom, m_strTo, subject, body)
            client.Send(m_strFrom, m_strTo, subject, body)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    '*********************************************************************
    '関数名称  ：メール送信処理　添付ファイル付き
    '機能      ：添付ファイルつきのメールを送信する
    '引数      ：subject :メールの件名
    '            body    :メールの本文
    '            attachFile :添付ファイルのパス
    '戻り値    ：なし
    '備考      ：なし
    '*********************************************************************
    Public Sub send(ByVal subject As String, _
                ByVal body As String, ByVal attachFile As String)
        Dim message As New MailMessage
        Dim client As SmtpClient

        client = New SmtpClient(m_strSMTP)
        'Mail.SmtpMail.SmtpServer = m_strSMTP

        ' メッセージを設定
        message.From = New MailAddress(m_strFrom)
        message.To.Add(m_strTo)
        message.Subject = subject
        message.Body = body
        message.Attachments.Add(New Attachment(attachFile))

        ' Sendメソッドで送信する
        Try
            client.Send(message)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

End Class
