ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 3094|ตอบกลับ: 0

[VB6] การส่งเมล์ด้วย CDO (Collaboration Data Objects) พร้อมกับการแนบไฟล์

[คัดลอกลิงก์]

310

กระทู้

501

โพสต์

6041

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6041




วันนี้แอดมินก็จะมาแนะนำวิธีในการส่งอีเมล์จากโค้ด Visual Basic 6.0 ด้วยการใช้
CDO (Collaboration Data Objects) ชื่อเต็มของไลบรารีที่เราจะใช้คือ Microsoft CDO for Windows 2000 Library ซึ่งเราต้องทำการ Add References เข้ามาก่อนด้วยล่ะครับ มีอีกคำที่อยากจะแนะนำให้รู้จักคือ SMTP หรือ Simple Mail Transfer Protocol มันคือ มาตรฐานบนอินเทอร์เน็ตสำหรับการรับส่ง Email หรือจะเรียกว่า Protocol ในการส่งเมล์ก็ว่าได้ ในโค้ดตัวอย่างนี้ที่รันทดสอบผ่านแอดมินใช้ SMTP ของ GMAIL และใช้ Port หมายเลข 465 รายละเอียดต่างๆก็คงต้องไปศึกษาหาความรู้เพิ่มเติมกันเองครับผม ...


Project --> References ... Microsoft CDO for Windows 200 Library ...


ฟังค์ชั่นในส่วนของการส่งเมล์ ... โดยจะรีเทิร์นค่า True หากส่งเมล์เสร็จสมบูรณ์, False คือไม่สำเร็จ
  1. ' / --------------------------------------------------------------------------------
  2. ' / Function to send mail.
  3. Public Function SendMail() As Boolean
  4. ' / --------------------------------------------------------------------------------
  5.       
  6.     '// Trap Error
  7.     On Error GoTo ErrHandler
  8.    
  9.     Dim CdoMail As CDO.Message
  10.     Set CdoMail = New CDO.Message
  11.     With CdoMail
  12.         .Configuration.Fields(cdoSMTPServer) = txtServer.Text
  13.         .Configuration.Fields(cdoSMTPServerPort) = Val(txtPort.Text)
  14.         .Configuration.Fields(cdoSMTPUseSSL) = chkSSL.Value
  15.         .Configuration.Fields(cdoSMTPAuthenticate) = cdoBasic
  16.         .Configuration.Fields(cdoSendUserName) = txtUsername.Text
  17.         .Configuration.Fields(cdoSendPassword) = txtPassword.Text
  18.         .Configuration.Fields(cdoSMTPConnectionTimeout) = 30
  19.         .Configuration.Fields(cdoSendUsingMethod) = cdoSendUsingPort
  20.         .Configuration.Fields.Update
  21.         CdoMail.To = Trim$(txtToMail.Text)
  22.         CdoMail.From = Trim$(txtFromMail.Text)
  23.         CdoMail.Subject = Trim$(txtSubject.Text)
  24.         CdoMail.TextBody = Trim$(txtMessage.Text)
  25.     End With
  26.     '// Attachment
  27.     If Trim$(txtAttachFile.Text) <> vbNullString Then CdoMail.AddAttachment (txtAttachFile.Text)
  28.     '// Sending mail.
  29.     CdoMail.Send
  30.     '// Clear memory
  31.     Set CdoMail = Nothing
  32.     '// Send Complete.
  33.     SendMail = True
  34.     Exit Function
  35.          
  36. ErrHandler:
  37.     Msg = "Error Number: " & Err.Number & vbCrLf & Err.Description
  38.     SendMail = False
  39. End Function
คัดลอกไปที่คลิปบอร์ด

โค้ดในการส่งเมล์ด้วย VB6 แบบเต็ม ...
  1. ' / --------------------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / Purpose : Send mail using CDO with Visual Basic 6.0
  8. ' / Microsoft Visual Basic 6.0 (SP6)
  9. ' /
  10. ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
  11. ' / You can modify and/or distribute without to inform the developer.
  12. ' / --------------------------------------------------------------------------------

  13. '// References ... Microsoft CDO for Windows 2000 library.

  14. Dim Msg As String

  15. ' / --------------------------------------------------------------------------------
  16. ' / Function to send mail.
  17. Public Function SendMail() As Boolean
  18. ' / --------------------------------------------------------------------------------
  19.       
  20.     '// Trap Error
  21.     On Error GoTo ErrHandler
  22.    
  23.     Dim CdoMail As CDO.Message
  24.     Set CdoMail = New CDO.Message
  25.     With CdoMail
  26.         .Configuration.Fields(cdoSMTPServer) = txtServer.Text
  27.         .Configuration.Fields(cdoSMTPServerPort) = Val(txtPort.Text)
  28.         .Configuration.Fields(cdoSMTPUseSSL) = chkSSL.Value
  29.         .Configuration.Fields(cdoSMTPAuthenticate) = cdoBasic
  30.         .Configuration.Fields(cdoSendUserName) = txtUsername.Text
  31.         .Configuration.Fields(cdoSendPassword) = txtPassword.Text
  32.         .Configuration.Fields(cdoSMTPConnectionTimeout) = 30
  33.         .Configuration.Fields(cdoSendUsingMethod) = cdoSendUsingPort
  34.         .Configuration.Fields.Update
  35.         CdoMail.To = Trim$(txtToMail.Text)
  36.         CdoMail.From = Trim$(txtFromMail.Text)
  37.         CdoMail.Subject = Trim$(txtSubject.Text)
  38.         CdoMail.TextBody = Trim$(txtMessage.Text)
  39.     End With
  40.     '// Attachment
  41.     If Trim$(txtAttachFile.Text) <> vbNullString Then CdoMail.AddAttachment (txtAttachFile.Text)
  42.     '// Sending mail.
  43.     CdoMail.Send
  44.     '// Clear memory
  45.     Set CdoMail = Nothing
  46.     '// Send Complete.
  47.     SendMail = True
  48.     Exit Function
  49.          
  50. ErrHandler:
  51.     Msg = "Error Number: " & Err.Number & vbCrLf & Err.Description
  52.     SendMail = False
  53. End Function

  54. ' / --------------------------------------------------------------------------------
  55. Private Sub cmdSend_Click()
  56.     Dim blnStatus As Boolean
  57.     Dim Ctrl  As Control
  58.     '// Validate data before to sending them exclude attachments.
  59.     For Each Ctrl In Me.Controls
  60.         If TypeOf Ctrl Is TextBox Then
  61.             If Trim$(Ctrl.Text) = vbNullString And LCase$(Ctrl.Name) <> "txtattachfile" Then
  62.                 lblStatus.Caption = "Error: You must to enter all the field, exclude attachments."
  63.                 Exit Sub
  64.             End If
  65.         End If
  66.     Next
  67.    
  68.     Frame1.Enabled = False
  69.     Frame2.Enabled = False
  70.     cmdSend.Enabled = False
  71.     lblStatus.Caption = "Sending data ..."
  72.     '// Sending Mail
  73.     blnStatus = SendMail
  74.     Frame1.Enabled = True
  75.     Frame2.Enabled = True
  76.     cmdSend.Enabled = True
  77.     '// Sending Status.
  78.     lblStatus.Caption = IIf(blnStatus = True, "Message sent successfully.", Msg)
  79.    
  80. End Sub

  81. Private Sub chkSSL_KeyDown(KeyCode As Integer, Shift As Integer)
  82.     If KeyCode = 13 Then
  83.         Sendkeys "{TAB}"
  84.     End If
  85. End Sub

  86. ' / --------------------------------------------------------------------------------
  87. ' / Attatch File.
  88. Private Sub cmdBrowseFile_Click()
  89. ' / --------------------------------------------------------------------------------
  90.     'On Error Resume Next

  91.     '// Trap Error
  92.     On Error GoTo ErrHandler
  93.     With dlgOpenFile
  94.         .DialogTitle = " Graphics File Format " '// Title Bar
  95.         .Filter = "Picture or Image (*.jpg;*.gif;*.bmp;*.png)|*.jpg;*.gif;*.bmp;*.png"   '// Image types
  96.         .InitDir = App.Path
  97.         .CancelError = True
  98.         .ShowOpen
  99.         If .FileName <> "" Then txtAttachFile.Text = .FileName
  100.     End With
  101.    
  102.     If txtAttachFile = "" Then Exit Sub

  103. ExitProc:
  104.     Exit Sub

  105. ErrHandler:
  106.     Select Case Err.Number
  107.         Case 32755
  108.             Err.Clear
  109.             Resume ExitProc
  110.         Case Else
  111.             MsgBox Err.Number & vbCrLf & Err.Description
  112.         End Select
  113. End Sub

  114. Private Sub Form_Load()
  115.     '// SMTP Setting
  116.     Me.txtServer.Text = "smtp.gmail.com"
  117.     Me.txtUsername.Text = "youraccount@gmail.com"   '<-- CHANGE
  118.     Me.txtPassword.Text = "password"    '<-- CHANGE
  119.     Me.txtPort.Text = "465"
  120.     '// Body
  121.     Me.txtFromName.Text = "G2GNet"  '<-- CHANGE
  122.     Me.txtFromMail.Text = "From@yourmail.com" '<-- CHANGE
  123.     Me.txtToMail.Text = "To@someonemail.com" '<-- CHANGE
  124.     Me.txtSubject.Text = "·´Êͺ¡ÒÃÊè§ÍÕàÁÅì " & Now()
  125.     Me.txtMessage.Text = "This is a test Email from Visual Basic 6.0 on " & Now
  126. End Sub

  127. '/  Solve problem SendKeys not work in Windows 8+
  128. Public Sub Sendkeys(Text As String, Optional Wait As Boolean = False)
  129.     Dim WshShell As Object
  130.     Set WshShell = CreateObject("Wscript.shell")
  131.     WshShell.Sendkeys Text, Wait
  132.     Set WshShell = Nothing
  133. End Sub

  134. Private Sub txtAttachFile_KeyPress(KeyAscii As Integer)
  135.     If KeyAscii = 13 Then
  136.         KeyAscii = 0
  137.         Sendkeys "{TAB}"
  138.     End If
  139. End Sub

  140. Private Sub txtFromMail_KeyPress(KeyAscii As Integer)
  141.     If KeyAscii = 13 Then
  142.         KeyAscii = 0
  143.         Sendkeys "{TAB}"
  144.     End If
  145. End Sub

  146. Private Sub txtFromName_KeyPress(KeyAscii As Integer)
  147.     If KeyAscii = 13 Then
  148.         KeyAscii = 0
  149.         Sendkeys "{TAB}"
  150.     End If
  151. End Sub

  152. Private Sub txtPassword_KeyPress(KeyAscii As Integer)
  153.     If KeyAscii = 13 Then
  154.         KeyAscii = 0
  155.         Sendkeys "{TAB}"
  156.     End If
  157. End Sub

  158. Private Sub txtPort_KeyPress(KeyAscii As Integer)
  159.     If KeyAscii = 13 Then
  160.         KeyAscii = 0
  161.         Sendkeys "{TAB}"
  162.     End If
  163. End Sub

  164. Private Sub txtServer_KeyPress(KeyAscii As Integer)
  165.     If KeyAscii = 13 Then
  166.         KeyAscii = 0
  167.         Sendkeys "{TAB}"
  168.     End If
  169. End Sub

  170. Private Sub txtSubject_KeyPress(KeyAscii As Integer)
  171.     If KeyAscii = 13 Then
  172.         KeyAscii = 0
  173.         Sendkeys "{TAB}"
  174.     End If
  175. End Sub

  176. Private Sub txtToMail_KeyPress(KeyAscii As Integer)
  177.     If KeyAscii = 13 Then
  178.         KeyAscii = 0
  179.         Sendkeys "{TAB}"
  180.     End If
  181. End Sub

  182. Private Sub txtUsername_KeyPress(KeyAscii As Integer)
  183.     If KeyAscii = 13 Then
  184.         KeyAscii = 0
  185.         Sendkeys "{TAB}"
  186.     End If
  187. End Sub
คัดลอกไปที่คลิปบอร์ด



ดาวน์โหลดโค้ดต้นฉบับ VB6 ได้ที่นี่ ...



ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-24 10:24 , Processed in 0.145351 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้