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

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

[VB.NET] แจกโค้ดฟรีในการส่งอีเมล์ พร้อมไฟล์แนบแบบหลายไฟล์ (Multiple Attachment)

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

310

กระทู้

501

โพสต์

6035

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6035




เมื่อวานแอดมินแจกโค้ดฟรีในการส่งอีเมล์บน VB6 ด้วยการใช้งาน CDO (Collaboration Data Objects) วันนี้จัดโค้ด VB.NET โดยที่สามารถแนบไฟล์ (Attachment) ไปพร้อมๆกันได้หลายไฟล์ ทั้งกราฟิคและไฟล์เอกสาร ... อย่ารอช้าไปดูโค้ดกันเลย

โค้ดหลักในการส่งเมล์ ...
  1.     ' / --------------------------------------------------------------------------
  2.     Public Sub SendMail()
  3.         '//
  4.         Try
  5.             Dim SmtpServer As New SmtpClient
  6.             Dim MyMail As New MailMessage()
  7.             With SmtpServer
  8.                 .UseDefaultCredentials = False
  9.                 .Credentials = New Net.NetworkCredential(txtUsername.Text, txtPassword.Text)
  10.                 .Port = Val(txtPort.Text)
  11.                 .EnableSsl = chkSSL.CheckState
  12.                 .Host = txtServer.Text
  13.             End With
  14.             '//
  15.             MyMail = New MailMessage()
  16.             MyMail.From = New MailAddress(txtFromMail.Text, txtFromName.Text)
  17.             MyMail.To.Add(txtToMail.Text)
  18.             MyMail.Subject = txtSubject.Text
  19.             MyMail.IsBodyHtml = False
  20.             MyMail.Body = txtMessage.Text
  21.             '// Attach Files
  22.             If dgvData.RowCount > 0 Then
  23.                 For i = 0 To dgvData.RowCount - 1
  24.                     MyMail.Attachments.Add(New Net.Mail.Attachment(dgvData.Rows(i).Cells(0).Value))
  25.                 Next
  26.             End If
  27.             '// SENDING
  28.             SmtpServer.Send(MyMail)

  29.         Catch ex As Exception
  30.             lblStatus.Text = ex.Message
  31.             Return
  32.         Finally
  33.             lblStatus.Text = "Send mail successfully."
  34.             '//
  35.             txtSubject.Clear()
  36.             txtMessage.Clear()
  37.             dgvData.Rows.Clear()
  38.         End Try
  39.     End Sub
คัดลอกไปที่คลิปบอร์ด



มาดูโค้ดแบบฉบับเต็มกันเถอะ ...
  1. #Region "ABOUT"
  2. ' / --------------------------------------------------------------------------
  3. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  4. ' / eMail : thongkorn@hotmail.com
  5. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  6. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  7. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  8. ' / Purpose: Send mail with VB.NET (2010)
  9. ' / Microsoft Visual Basic .NET (2010)
  10. ' /
  11. ' / This is open source code under @CopyLeft by Thongkorn/Common Tubtimkrob.
  12. ' / You can modify and/or distribute without to inform the developer.
  13. ' / --------------------------------------------------------------------------
  14. #End Region

  15. Imports System.Net.Mail
  16. Imports System.IO
  17. '// Link Reference.
  18. '// https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?redirectedfrom=MSDN&view=netframework-4.7.2

  19. Public Class frmSendMailNet
  20.     '// Create MenuStrip @Run Time
  21.     Dim _contextmenu As New ContextMenuStrip

  22.     ' / --------------------------------------------------------------------------
  23.     Private Sub frmSendMail_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  24.         '// SMTP Setting
  25.         Me.txtServer.Text = "smtp.gmail.com"
  26.         Me.txtUsername.Text = "yourmail@gmail.com"  '<-- Change value
  27.         Me.txtPassword.Text = "yourpassword" '<-- Change value
  28.         Me.txtPort.Text = "587" '"465"
  29.         '// Body
  30.         Me.txtFromName.Text = "Your Name" '<-- Change value
  31.         Me.txtFromMail.Text = "yourname@gmail.com" '<-- Change value
  32.         Me.txtToMail.Text = "tosomeone@mail.com" '<-- Change value
  33.         Me.txtSubject.Text = "Test Mail " & Now()
  34.         Me.txtMessage.Text = "This is a test Email from Visual Basic .Net (2010) on " & Now
  35.         '// Setup DataGridView
  36.         Call InitializeGrid()
  37.         '// Add new contextmenu
  38.         _contextmenu.Items.Add("Image Files")
  39.         _contextmenu.Items.Add("Document Files")
  40.         AddHandler _contextmenu.ItemClicked, AddressOf Contextmenu_Click
  41.     End Sub

  42.     ' / --------------------------------------------------------------------------
  43.     Private Sub Contextmenu_Click(ByVal sender As System.Object, ByVal e As ToolStripItemClickedEventArgs)
  44.         '// Choose item from contextmenu.
  45.         Select Case e.ClickedItem.Text
  46.             '// Browse Images
  47.             Case "Image Files"
  48.                 _contextmenu.Visible = False
  49.                 Call AttachFiles("image")
  50.                 '// Browse Documents
  51.             Case "Document Files"
  52.                 _contextmenu.Visible = False
  53.                 Call AttachFiles("document")
  54.         End Select
  55.     End Sub

  56.     ' / --------------------------------------------------------------------------
  57.     Private Sub btnAttach_Click(sender As System.Object, e As System.EventArgs) Handles btnAttach.Click
  58.         '// Show contextmenu on button @run Time.
  59.         _contextmenu.Show(btnAttach, 0, btnAttach.Height)
  60.     End Sub

  61.     ' / --------------------------------------------------------------------------
  62.     Private Sub AttachFiles(ByVal FileType As String)
  63.         Dim dlgFile As OpenFileDialog = New OpenFileDialog()
  64.         Select Case FileType
  65.             Case "image"
  66.                 ' / Open File Dialog
  67.                 With dlgFile
  68.                     .InitialDirectory = Application.StartupPath
  69.                     .Title = "Select images"
  70.                     .Filter = "Image types (*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp"
  71.                     .FilterIndex = 1
  72.                     .RestoreDirectory = True
  73.                 End With
  74.             Case "document"
  75.                 With dlgFile
  76.                     .InitialDirectory = Application.StartupPath
  77.                     .Title = "Select Document"
  78.                     .Filter = "Document types (*.doc;*.docx;*.xls;*.xlsx;*.pdf)|*.doc;*.docx;*.xls;*.xlsx;*.pdf"
  79.                     .FilterIndex = 1
  80.                     .RestoreDirectory = True
  81.                 End With
  82.         End Select
  83.         '/ Select OK after Browse ...
  84.         If dlgFile.ShowDialog() = DialogResult.OK Then
  85.             For i = 0 To dgvData.RowCount - 1
  86.                 '// Not Duplicate
  87.                 If dgvData.Rows(i).Cells(0).Value = dlgFile.FileName.ToString Then Return
  88.             Next
  89.             dgvData.Rows.Add()
  90.             dgvData.Rows(dgvData.RowCount - 1).Cells(0).Value = dlgFile.FileName.ToString
  91.             '// dgvData.Rows.Add(New String(){Value1, Value2, Value3})
  92.         End If
  93.     End Sub

  94.     ' / --------------------------------------------------------------------------
  95.     Private Sub btnSend_Click(sender As System.Object, e As System.EventArgs) Handles btnSend.Click
  96.         '// Validate data before to sending them exclude attachments.
  97.         For Each gb As GroupBox In Me.Controls.OfType(Of GroupBox)()
  98.             For Each tb As TextBox In gb.Controls.OfType(Of TextBox)()
  99.                 If Trim$(tb.Text) = vbNullString Then
  100.                     lblStatus.Text = "Error: You must to enter all the field, exclude attachments."
  101.                     MessageBox.Show(lblStatus.Text)
  102.                     Exit Sub
  103.                 End If
  104.             Next
  105.         Next
  106.         '//
  107.         Call SendMail()
  108.         '//
  109.     End Sub

  110.     ' / --------------------------------------------------------------------------
  111.     Public Sub SendMail()
  112.         '//
  113.         Try
  114.             Dim SmtpServer As New SmtpClient
  115.             Dim MyMail As New MailMessage()
  116.             With SmtpServer
  117.                 .UseDefaultCredentials = False
  118.                 .Credentials = New Net.NetworkCredential(txtUsername.Text, txtPassword.Text)
  119.                 .Port = Val(txtPort.Text)
  120.                 .EnableSsl = chkSSL.CheckState
  121.                 .Host = txtServer.Text
  122.             End With
  123.             '//
  124.             MyMail = New MailMessage()
  125.             MyMail.From = New MailAddress(txtFromMail.Text, txtFromName.Text)
  126.             MyMail.To.Add(txtToMail.Text)
  127.             MyMail.Subject = txtSubject.Text
  128.             MyMail.IsBodyHtml = False
  129.             MyMail.Body = txtMessage.Text
  130.             '// Attach Files
  131.             If dgvData.RowCount > 0 Then
  132.                 For i = 0 To dgvData.RowCount - 1
  133.                     MyMail.Attachments.Add(New Net.Mail.Attachment(dgvData.Rows(i).Cells(0).Value))
  134.                 Next
  135.             End If
  136.             '// SENDING
  137.             SmtpServer.Send(MyMail)

  138.         Catch ex As Exception
  139.             lblStatus.Text = ex.Message
  140.             Return
  141.         Finally
  142.             lblStatus.Text = "Send mail successfully."
  143.             '//
  144.             txtSubject.Clear()
  145.             txtMessage.Clear()
  146.             dgvData.Rows.Clear()
  147.         End Try
  148.     End Sub

  149.     ' / --------------------------------------------------------------------------
  150.     '// Setting is for DataGridView @Run Time.
  151.     Private Sub InitializeGrid()
  152.         '// Declare columns type.
  153.         Dim Column0 As New DataGridViewTextBoxColumn()
  154.         Dim Column1 As New DataGridViewButtonColumn
  155.         '// Add new Columns
  156.         dgvData.Columns.AddRange(New DataGridViewColumn() { _
  157.                                  Column0, Column1 _
  158.                                 })
  159.         '// Startup
  160.         With Column0
  161.             .Name = "FileAttach"
  162.             .HeaderText = "File Attachment"
  163.             .Visible = True
  164.         End With
  165.         With Column1
  166.             .HeaderText = ""
  167.             .Text = "Remove"
  168.             .Name = "btnDelRow"
  169.             .UseColumnTextForButtonValue = True
  170.             .Width = 60
  171.             .ReadOnly = True
  172.         End With
  173.         '//
  174.         With dgvData
  175.             .RowHeadersVisible = False
  176.             .AllowUserToAddRows = False
  177.             .AllowUserToDeleteRows = False
  178.             .AllowUserToResizeRows = False
  179.             .MultiSelect = False
  180.             .SelectionMode = DataGridViewSelectionMode.FullRowSelect
  181.             .ReadOnly = True
  182.             .Font = New Font("Tahoma", 9)
  183.             ' Adjust the width each Column to fit.
  184.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  185.             '.AutoResizeColumns()
  186.             ' Adjust Header Styles.
  187.             With .ColumnHeadersDefaultCellStyle
  188.                 .BackColor = Color.Navy
  189.                 .ForeColor = Color.White
  190.                 .Font = New Font("Tahoma", 9, FontStyle.Bold)
  191.             End With
  192.         End With
  193.     End Sub

  194.     ' / --------------------------------------------------------------------------
  195.     Private Sub dgvData_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvData.CellClick
  196.         Select Case e.ColumnIndex
  197.             '// Remove row (Button)
  198.             Case 1
  199.                 Call DeleteRow(dgvData.Columns(e.ColumnIndex).Name)
  200.         End Select
  201.     End Sub

  202.     ' / --------------------------------------------------------------------------
  203.     ' / Remove select row.
  204.     Private Sub DeleteRow(ByVal ColName As String)
  205.         If ColName = "btnDelRow" Then
  206.             '// Remove select row.
  207.             dgvData.Rows.Remove(dgvData.CurrentRow)
  208.         End If
  209.     End Sub

  210.     ' / --------------------------------------------------------------------------
  211.     ' / Get numeric only.
  212.     Function CheckDigitOnly(ByVal index As Integer) As Boolean
  213.         Select Case index
  214.             Case 48 To 57 ' 0 - 9
  215.                 CheckDigitOnly = False
  216.             Case 8, 13 ' Backspace = 8, Enter = 13
  217.                 CheckDigitOnly = False
  218.             Case Else
  219.                 CheckDigitOnly = True
  220.         End Select
  221.     End Function

  222.     Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
  223.         Me.Close()
  224.     End Sub

  225.     Private Sub frmSendMail_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  226.         Me.Dispose()
  227.         Application.Exit()
  228.     End Sub

  229.     Private Sub txtServer_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtServer.KeyPress
  230.         If e.KeyChar = Chr(13) Then
  231.             e.Handled = True
  232.             SendKeys.Send("{TAB}")
  233.         End If
  234.     End Sub

  235.     Private Sub txtSubject_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtSubject.KeyPress
  236.         If e.KeyChar = Chr(13) Then
  237.             e.Handled = True
  238.             SendKeys.Send("{TAB}")
  239.         End If
  240.     End Sub

  241.     Private Sub txtToMail_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtToMail.KeyPress
  242.         If e.KeyChar = Chr(13) Then
  243.             e.Handled = True
  244.             SendKeys.Send("{TAB}")
  245.         End If
  246.     End Sub

  247.     Private Sub txtUsername_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtUsername.KeyPress
  248.         If e.KeyChar = Chr(13) Then
  249.             e.Handled = True
  250.             SendKeys.Send("{TAB}")
  251.         End If
  252.     End Sub

  253.     Private Sub txtPort_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtPort.KeyPress
  254.         If e.KeyChar = Chr(13) Then
  255.             e.Handled = True
  256.             SendKeys.Send("{TAB}")
  257.         Else
  258.             e.Handled = CheckDigitOnly(Asc(e.KeyChar))
  259.         End If
  260.     End Sub

  261.     Private Sub txtPassword_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtPassword.KeyPress
  262.         If e.KeyChar = Chr(13) Then
  263.             e.Handled = True
  264.             SendKeys.Send("{TAB}")
  265.         End If
  266.     End Sub

  267.     Private Sub txtFromName_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtFromName.KeyPress
  268.         If e.KeyChar = Chr(13) Then
  269.             e.Handled = True
  270.             SendKeys.Send("{TAB}")
  271.         End If
  272.     End Sub

  273.     Private Sub txtFromMail_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtFromMail.KeyPress
  274.         If e.KeyChar = Chr(13) Then
  275.             e.Handled = True
  276.             SendKeys.Send("{TAB}")
  277.         End If
  278.     End Sub

  279.     Private Sub chkSSL_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles chkSSL.KeyDown
  280.         If e.KeyCode = 13 Then
  281.             e.Handled = True
  282.             SendKeys.Send("{TAB}")
  283.         End If
  284.     End Sub
  285. End Class
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2010) ได้ที่นี่ ...



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

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

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

5

กระทู้

21

โพสต์

165

เครดิต

Member

Rank: 2

เครดิต
165
โพสต์ 2020-10-21 21:08:22 | ดูโพสต์ทั้งหมด

ขอบพระคุณครับ อาจารย์
แต่ผมลองส่งเมลแล้ว Gmail มันบล๊อกครับ มันบอกไม่ได้เข้าสู่ระบบผ่านหน้าเว็บ

310

กระทู้

501

โพสต์

6035

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6035
 เจ้าของ| โพสต์ 2020-10-22 13:05:17 | ดูโพสต์ทั้งหมด

komenservice ตอบกลับเมื่อ 2020-10-21 21:08
ขอบพระคุณครับ อาจารย์
แต่ผมลองส่งเมลแล้ว Gmail มันบล๊อกครับ มันบอกไม่ได้เข้าสู่ระบบผ่านหน้าเว็บ

ต้องเปิดการเข้าถึงของแอพครับผม ...


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

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

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

5

กระทู้

21

โพสต์

165

เครดิต

Member

Rank: 2

เครดิต
165
โพสต์ 2020-10-28 14:57:41 | ดูโพสต์ทั้งหมด

ขอบพระคุณครับ อาจารย์

2

กระทู้

5

โพสต์

174

เครดิต

Member

Rank: 2

เครดิต
174
โพสต์ 2022-6-6 09:52:04 | ดูโพสต์ทั้งหมด

แก้ไขครั้งสุดท้ายโดย outhai เมื่อ 2022-6-6 09:55

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

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

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

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

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

GMT+7, 2024-4-20 21:19 , Processed in 0.218065 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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