หน้าเว็บ

วันเสาร์ที่ 14 กันยายน พ.ศ. 2556

โปรเจคส่ง Email ด้วย VB.NET

เริ่มต้นเรียน เขียน โปรเจค VB.NET 1

รูปตัวอย่าง โปรเจคส่งอีเมล์ด้วย vb.net


  
 รายระเอียดต่างๆ ของ โปรเจค

Object 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    TextBox
     1 (Name) txtMyname
     2 (Name) txtMyemail
     3 (Name) txtMypassword
     4 (Name) txtSubject 
     5 (Name) txtTo
     6 (Name) txtBody
     7 (Name) txtAttach
    Buttun
     8 (Name) btnBrowse
     9 (Name) btnSend
   
Code VB.NET
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Imports System.Net.Mail
Imports System.Net.Mail.SmtpClient
Imports System.Net.Mail.MailMessage 


Public Class Form1
     'สร้างตัวแปร attachToMsg เก็บการส่งไฟล์แนบ
     Dim attachToMsg As Attachment
     'สร้างตัวแปร eMailmessage เก็บการส่งข้อความอีเมล
     Dim eMailmessage As New MailMessage


     Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
         If Len(txtMyemail.Text) = 0 Or Len(txtMypassword.Text) = 0 Then
             MsgBox("ค่าอีเมล์โฮสต์หรือรหัสผ่านโฮสต์ยังไม่ได้ป้อนข้อมูล", MsgBoxStyle.Critical + MsgBoxStyle.ApplicationModal, "ล้มเหลว")
                txtMyemail.Focus()
         Else
             Try
                 If
Len(txtTo.Text) = 0 Then
                     MsgBox("คุณต้องป้อนอีเมล์ที่จะส่งก่อนทำการส่ง", MsgBoxStyle.Critical + MsgBoxStyle.ApplicationModal, "ล้มเหลว")
                 Else
                    'โฮสต์ และ เลขพอร์ต ที่คุณจะส่งอีเมล
                    'และคุณต้องมีบัญชีของ Gmail ถ้าจะใช้ smtp.gmail.com
                    Dim smtp As New SmtpClient("smtp.gmail.com", 587)
                    'สร้างตัวแปร eMailmessage เก็บการส่งข้อความอีเมล
                    Dim eMailmessage As New MailMessage
                    'กำหนดความปลอดภัยในการเข้ารหัสการส่งข้อความให้เป็น True
                    smtp.EnableSsl = True
                    'ติดตั้งโฮสต์ของ Gmail
                    smtp.Credentials = New System.Net.NetworkCredential(txtMyemail.Text, txtMypassword.Text)
                    'กำค่าส่วนประกอบของอีเมล์ เช่น ข้อความ, หัวข้อ, จากผู้ส่ง และ ผู้รับ
                    eMailmessage.Subject = txtSubject.Text
                    eMailmessage.Body = txtBody.Text
                    eMailmessage.From = New MailAddress(txtMyemail.Text, txtMyname.Text)
                    eMailmessage.To.Add(txtTo.Text)
                    smtp.Send(eMailmessage)
                    MsgBox("ส่งอีเมล์สำเร็จ", vbInformation, "รายงานผล")
                End If
            Catch ex As Exception
                MsgBox("ข้อมูลผิดพลาด กรุณาตรวจสอบข้อมูลอีกครั้ง", MsgBoxStyle.Critical + MsgBoxStyle.ApplicationModal, "การทำงานผิดพลาด")
            End Try
        End If
    End Sub

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        'กำหนดสิ่งที่แนบกับข้อความโดยใช้ OpeFileDialog เปิดหน้าต่างโต้ตอบเพื่อเลือกได้หลายไฟล์
        'สร้างตัวแปร openDLG เพื่อเก็บค่าไฟล์ที่เลือกแนบไปกับอีเมล

        Dim openDLG As New OpenFileDialog
        openDLG.AddExtension = True
        openDLG.ReadOnlyChecked = True
        openDLG.Multiselect = True
        openDLG.Title = "เลือกไฟล์แนบ"
        openDLG.Filter = "All Files (*.*)|*.*"
        openDLG.FileName = txtAttach.Text
        If openDLG.ShowDialog = Windows.Forms.DialogResult.OK Then
            For Each item As String In openDLG.FileNames
                attachToMsg = New Attachment(item)
                eMailmessage.Attachments.Add(attachToMsg)
                txtAttach.Text = item
            Next
            btnSend.Focus()
        End If
    End Sub
End Class


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------