Imports System.net.mail

Public Class MailForm
    Enum MailType
        auth = 1
        Err = 2
        question = 3
        others = 4
    End Enum
    'Public ProgName As String = vbNullString

    Public Sub New(ByVal from As String, ByVal subject As String, ByVal Message As String, ByVal ProgramName As String, Optional ByVal type As MailType = 3, Optional ByVal Errmsg As String = "")

        ' 이 호출은 Windows Form 디자이너에 필요합니다.
        InitializeComponent()

        ' InitializeComponent() 호출 뒤에 초기화 코드를 추가하십시오.

        Me.TB_BODY.Text = Message
        Me.TB_SUBJECT.Text = subject

        If from = vbNullString Then
            Me.TB_FROM.Text = "보내는사람 메일주소"
        Else
            Me.TB_FROM.Text = from
        End If

        Me.tb_errbody.Text = Errmsg

        Me.tb_pgno.Text = ProgramName

        Select Case type
            Case MailType.auth '인증
                Me.RadioButton1.Checked = True
            Case MailType.Err '에러
                Me.RadioButton2.Checked = True
            Case MailType.question '질문
                Me.RadioButton4.Checked = True
            Case Else '기타
                Me.RadioButton4.Checked = True
        End Select
        Me.TB_BODY.Focus()

    End Sub

    Function IsValidEmail(ByVal strIn As String) As Boolean
        ' Return true if strIn is in valid e-mail format.
        Return System.Text.RegularExpressions.Regex.IsMatch(strIn, "^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")
    End Function

    Private Sub Msg(ByVal mm As String)
        ' MsgBox(mm, MsgBoxStyle.Information, "확인")
        Me.lb_msg.Text = mm
    End Sub

    Private Sub TB_FROM_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TB_FROM.GotFocus, tb_to.GotFocus
        Msg("본인의 메일주소를 입력하세요")
        Me.TB_FROM.SelectAll()
    End Sub
    Private Sub TB_SUBJECT_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TB_SUBJECT.GotFocus
        Msg("메일제목을 입력하세요")
        Me.TB_SUBJECT.SelectAll()
    End Sub

    Private Sub TB_BODY_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TB_BODY.GotFocus
        Msg("본문 내용을 입력하세요")
        Me.TB_BODY.SelectAll()
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Dim Buf As New System.Text.StringBuilder
        Dim SubJect As String = ""

        If Me.TB_BODY.Text = vbNullString Then
            Msg("본문내용이 없습니다")
            Exit Sub
        End If

        If Not IsValidEmail(Me.TB_FROM.Text) Then    '//@가없는 메일주소일때
            Me.TB_FROM.SelectAll()
            Me.TB_FROM.Focus()
            Msg("메일주소 형식이 잘못되었습니다")
            Exit Sub
        End If

        If Me.RadioButton1.Checked Then SubJect = "[인증요청]"
        If Me.RadioButton2.Checked Then SubJect = "[에러보고]"
        If Me.RadioButton4.Checked Then SubJect = "[기타]"

        SubJect = SubJect & Space(1) & Me.TB_SUBJECT.Text

        Dim mailobj As New MailMessage(TB_FROM.Text, Me.tb_to.Text)  '//여기메일서버는 나아니면 못받게되어잇다.
        mailobj.Subject = SubJect '//제목줄
        mailobj.BodyEncoding = System.Text.Encoding.Default
        mailobj.IsBodyHtml = False
        Buf.AppendLine("=======================")
        Buf.AppendLine("보내는사람 : " & Me.TB_FROM.Text)
        Buf.AppendLine("프로그램명 : " & Me.tb_pgno.Text)
        Buf.AppendLine("=======================")
        Buf.AppendLine("본문메세지")
        Buf.AppendLine(Me.TB_BODY.Text)
        Buf.AppendLine("=======================")
        Buf.AppendLine("오류메세지")
        Buf.AppendLine(Me.tb_errbody.Text)

        mailobj.Body = Buf.ToString

        Dim SC As New System.Net.Mail.SmtpClient("tindevil.com", 2552)
        SC.Credentials = New System.Net.NetworkCredential("mail", "Mail2506!@+_")

        SC.Send(mailobj)
        Me.Dispose()
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_cancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub TB_FROM_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TB_FROM.KeyDown, tb_to.KeyDown
        If e.KeyCode = Windows.Forms.Keys.Enter Then
            Me.TB_SUBJECT.Focus()
        End If
    End Sub


    Private Sub MailForm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Windows.Forms.Keys.Escape Then
            Me.bt_cancel.PerformClick()
        End If
    End Sub

    Private Sub TB_SUBJECT_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TB_SUBJECT.KeyDown
        If e.KeyCode = Windows.Forms.Keys.Enter Then
            Me.TB_BODY.Focus()
        End If
    End Sub

    Private Sub TB_SUBJECT_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TB_SUBJECT.TextChanged

    End Sub

    Private Sub TB_BODY_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TB_BODY.TextChanged

    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Me.ToolStripButton1.PerformClick()
    End Sub
End Class