Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
database.ExecCommand("SELECT_Email1", dr)
While dr.Read
smtpserver.Credentials = New Net.NetworkCredential("[email protected]", "xxxxxxx")
smtpserver.Host = "smtp.live.com"
smtpserver.Port = "587"
mail = New MailMessage
mail.From = New MailAddress("[email protected]")
mail.To.Add(New MailAddress(dr.Item("emailname")))
mail.Subject = txtSuject.Text
mail.Body = txtmess.Text.Replace("<!--Link-->", "<a href=""http://localhost:65150/TEST/frmemailcancel.aspx?email=" & des.EncryptString(dr.Item("emailname")) & """ target=""_blank"">")
mail.IsBodyHtml = True
If Not txtAttachment.Text = Nothing Then
Dim attach As New Attachment(txtAttachment.Text)
mail.Attachments.Add(attach)
End If
If Not TextBox1.Text = Nothing Then
Dim attach As New Attachment(TextBox1.Text)
mail.Attachments.Add(attach)
End If
If Not TextBox2.Text = Nothing Then
Dim attach As New Attachment(TextBox2.Text)
mail.Attachments.Add(attach)
End If
smtpserver.EnableSsl = True
smtpserver.Send(mail)
For i = 1 To dt.Rows.Count
lbl1.Text = dt.Rows.Count
lbl1.Text = "กำลังส่ง" & i & "ฉบับ"
BackgroundWorker1.ReportProgress(i)
System.Threading.Thread.Sleep(CInt(i + dt.Rows.Count)) 'ส่วนที่ทำให้ lbl นับจำนวนส่ง
Next
End While
MessageBox.Show("send mail ok")
dr.Close()
Catch ex As Exception
MsgBox("error")
End Try
End Sub
Private Sub btnsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsend.Click
btnsend.Enabled = False
btnPause.Enabled = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click
btnsend.Enabled = False
btnPause.Enabled = True
End Sub
ขอบคุนนะคับบบบบ
Tag : Ms SQL Server 2005, Ms SQL Server 2008, VB.NET
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.BackgroundWorker1 = New System.ComponentModel.BackgroundWorker()
Me.ProgressBar1 = New System.Windows.Forms.ProgressBar()
Me.LabelPercent = New System.Windows.Forms.Label()
Me.ButtonRun = New System.Windows.Forms.Button()
Me.ButtonPause = New System.Windows.Forms.Button()
Me.ButtonReset = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'BackgroundWorker1
'
Me.BackgroundWorker1.WorkerReportsProgress = True
Me.BackgroundWorker1.WorkerSupportsCancellation = True
'
'ProgressBar1
'
Me.ProgressBar1.Location = New System.Drawing.Point(12, 12)
Me.ProgressBar1.Name = "ProgressBar1"
Me.ProgressBar1.Size = New System.Drawing.Size(260, 15)
Me.ProgressBar1.TabIndex = 0
'
'LabelPercent
'
Me.LabelPercent.AutoSize = True
Me.LabelPercent.Location = New System.Drawing.Point(12, 35)
Me.LabelPercent.Name = "LabelPercent"
Me.LabelPercent.Size = New System.Drawing.Size(39, 13)
Me.LabelPercent.TabIndex = 1
Me.LabelPercent.Text = "Label1"
'
'ButtonRun
'
Me.ButtonRun.Location = New System.Drawing.Point(12, 57)
Me.ButtonRun.Name = "ButtonRun"
Me.ButtonRun.Size = New System.Drawing.Size(75, 23)
Me.ButtonRun.TabIndex = 2
Me.ButtonRun.Text = "Run"
Me.ButtonRun.UseVisualStyleBackColor = True
'
'ButtonPause
'
Me.ButtonPause.Location = New System.Drawing.Point(93, 57)
Me.ButtonPause.Name = "ButtonPause"
Me.ButtonPause.Size = New System.Drawing.Size(75, 23)
Me.ButtonPause.TabIndex = 3
Me.ButtonPause.Text = "Pause"
Me.ButtonPause.UseVisualStyleBackColor = True
'
'ButtonReset
'
Me.ButtonReset.Location = New System.Drawing.Point(197, 57)
Me.ButtonReset.Name = "ButtonReset"
Me.ButtonReset.Size = New System.Drawing.Size(75, 23)
Me.ButtonReset.TabIndex = 4
Me.ButtonReset.Text = "Reset"
Me.ButtonReset.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 92)
Me.Controls.Add(Me.ButtonReset)
Me.Controls.Add(Me.ButtonPause)
Me.Controls.Add(Me.ButtonRun)
Me.Controls.Add(Me.LabelPercent)
Me.Controls.Add(Me.ProgressBar1)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.Text = "Test"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents BackgroundWorker1 As System.ComponentModel.BackgroundWorker
Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
Friend WithEvents LabelPercent As System.Windows.Forms.Label
Friend WithEvents ButtonRun As System.Windows.Forms.Button
Friend WithEvents ButtonPause As System.Windows.Forms.Button
Friend WithEvents ButtonReset As System.Windows.Forms.Button
End Class
Form1.vb
Public Class Form1
Private _Busy As System.Threading.ManualResetEvent
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
_Busy = New System.Threading.ManualResetEvent(False)
LabelPercent.Text = String.Empty
ButtonPause.Enabled = False
End Sub
Private Sub ButtonRun_Click(sender As System.Object, e As System.EventArgs) Handles ButtonRun.Click
If (Not BackgroundWorker1.IsBusy) Then
ProgressBar1.Value = 0
BackgroundWorker1.RunWorkerAsync()
End If
_Busy.Set()
ButtonRun.Enabled = False
ButtonPause.Enabled = True
ButtonReset.Enabled = False
End Sub
Private Sub ButtonPause_Click(sender As System.Object, e As System.EventArgs) Handles ButtonPause.Click
_Busy.Reset()
ButtonRun.Enabled = True
ButtonPause.Enabled = False
ButtonReset.Enabled = True
End Sub
Private Sub ButtonReset_Click(sender As System.Object, e As System.EventArgs) Handles ButtonReset.Click
If (BackgroundWorker1.IsBusy) Then
' Set CancellationPending property to true
BackgroundWorker1.CancelAsync()
' Unblock worker so it can see that
_Busy.Set()
ButtonRun.Enabled = True
ButtonPause.Enabled = False
ButtonReset.Enabled = True
End If
ProgressBar1.Value = 0
LabelPercent.Text = String.Empty
End Sub
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i As Integer = 1 To 100
_Busy.WaitOne()
If BackgroundWorker1.CancellationPending Then
Return
End If
System.Threading.Thread.Sleep(100)
BackgroundWorker1.ReportProgress(i)
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
LabelPercent.Text = String.Format("{0}%", e.ProgressPercentage.ToString())
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
ButtonRun.Enabled = True
ButtonPause.Enabled = False
ButtonReset.Enabled = True
End Sub
End Class
Private _Busy As System.Threading.ManualResetEvent
Private Sub Sendmail_Backgroundworker_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
database.ExecCommand("SELECT_Email_VB", dr)
If dr.HasRows Then
dt = New DataTable
dt.Load(dr)
DataGridView1.DataSource = dt
End If
dr.Close()
Control.CheckForIllegalCrossThreadCalls = False
_Busy = New System.Threading.ManualResetEvent(False)
Label1.Text = String.Empty
btnPause.Enabled = False
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
Dim i As Integer
Do Until i = dt.Rows.Count
Dim smtpserver As New SmtpClient
smtpserver.Credentials = New Net.NetworkCredential("[email protected]", "xxxxxx")
smtpserver.Host = ComboBox1.Text
smtpserver.Port = "587"
Dim des As New TohomeCenter.EmCRMLib.Encryption
Dim mail As New MailMessage
mail = New MailMessage
mail.From = New MailAddress("[email protected]")
mail.To.Add(New MailAddress(dt.Rows(i)("emailname")))
mail.Subject = txtSuject.Text
mail.Body = txtmess.Text.Replace("<!--Link-->", "<a href=""http://localhost:65150/TEST/frmemailcancel.aspx?email=" & des.EncryptString(Me.txtto.Text) & """ target=""_blank"">")
If Not txtAttachment.Text = Nothing Then
Dim Attach As Net.Mail.Attachment = New Net.Mail.Attachment(txtAttachment.Text)
mail.Attachments.Add(Attach)
End If
mail.IsBodyHtml = True
smtpserver.EnableSsl = True
smtpserver.Send(mail)
i += 1
lbl1.Text = "กำลังส่ง : " & i.ToString & "ฉบับ"
Me.Refresh()
_Busy.WaitOne()
If BackgroundWorker1.CancellationPending Then
Return
End If
BackgroundWorker1.ReportProgress(i)
System.Threading.Thread.Sleep(1000)
Loop
i = 0
MessageBox.Show("send mail ok")
dr.Close()
Catch ex As Exception
MsgBox("error")
End Try
end sub
Private Sub btnsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsend.Click
'btnsend.Enabled = False
'btnPause.Enabled = True
'BackgroundWorker1.RunWorkerAsync()
If (Not BackgroundWorker1.IsBusy) Then
ProgressBar1.Value = 0
BackgroundWorker1.RunWorkerAsync()
End If
_Busy.Set()
btnsend.Enabled = False
btnPause.Enabled = True
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
Label1.Text = String.Format("{0}%", e.ProgressPercentage.ToString())
End Sub
Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click
'btnsend.Enabled = False
'btnPause.Enabled = True
_Busy.Reset()
btnsend.Enabled = True
btnPause.Enabled = False
Private _Busy As System.Threading.ManualResetEvent
Private Sub Sendmail_Backgroundworker_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
database.ExecCommand("SELECT_Email_VB", dr)
If dr.HasRows Then
dt = New DataTable
dt.Load(dr)
DataGridView1.DataSource = dt
End If
dr.Close()
Control.CheckForIllegalCrossThreadCalls = False
_Busy = New System.Threading.ManualResetEvent(False)
Label1.Text = String.Empty
btnPause.Enabled = False
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
Dim i As Integer
Do Until i = dt.Rows.Count
Dim smtpserver As New SmtpClient
smtpserver.Credentials = New Net.NetworkCredential("[email protected]", "xxxxxx")
smtpserver.Host = ComboBox1.Text
smtpserver.Port = "587"
Dim des As New TohomeCenter.EmCRMLib.Encryption
Dim mail As New MailMessage
mail = New MailMessage
mail.From = New MailAddress("[email protected]")
mail.To.Add(New MailAddress(dt.Rows(i)("emailname")))
mail.Subject = txtSuject.Text
mail.Body = txtmess.Text.Replace("<!--Link-->", "<a href=""http://localhost:65150/TEST/frmemailcancel.aspx?email=" & des.EncryptString(Me.txtto.Text) & """ target=""_blank"">")
If Not txtAttachment.Text = Nothing Then
Dim Attach As Net.Mail.Attachment = New Net.Mail.Attachment(txtAttachment.Text)
mail.Attachments.Add(Attach)
End If
mail.IsBodyHtml = True
smtpserver.EnableSsl = True
smtpserver.Send(mail)
i += 1
lbl1.Text = "กำลังส่ง : " & i.ToString & "ฉบับ"
Me.Refresh()
_Busy.WaitOne()
If BackgroundWorker1.CancellationPending Then
Return
End If
BackgroundWorker1.ReportProgress(i)
System.Threading.Thread.Sleep(1000)
Loop
i = 0
MessageBox.Show("send mail ok")
dr.Close()
Catch ex As Exception
MsgBox("error")
End Try
end sub
Private Sub btnsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsend.Click
'btnsend.Enabled = False
'btnPause.Enabled = True
'BackgroundWorker1.RunWorkerAsync()
If (Not BackgroundWorker1.IsBusy) Then
ProgressBar1.Value = 0
BackgroundWorker1.RunWorkerAsync()
End If
_Busy.Set()
btnsend.Enabled = False
btnPause.Enabled = True
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
Label1.Text = String.Format("{0}%", e.ProgressPercentage.ToString())
End Sub
Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click
'btnsend.Enabled = False
'btnPause.Enabled = True
_Busy.Reset()
btnsend.Enabled = True
btnPause.Enabled = False
Private Sub Sendmail_Backgroundworker_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
database.ExecCommand("SELECT_Email_VB", dr)
If dr.HasRows Then
dt = New DataTable
dt.Load(dr)
DataGridView1.DataSource = dt
End If
dr.Close()
Control.CheckForIllegalCrossThreadCalls = False
_Busy = New System.Threading.ManualResetEvent(False)
Label1.Text = String.Empty
btnPause.Enabled = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fd As New OpenFileDialog
With fd
.Filter = "Text Documents (*.txt)|*.html"
If .ShowDialog = Windows.Forms.DialogResult.OK Then
txttemp.Text = .FileName
Dim txtParser As New TextBox
txtParser.Multiline = True
txtmess.Text = My.Computer.FileSystem.ReadAllText(.FileName, System.Text.ASCIIEncoding.Default)
End If
End With
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
file = Nothing
OpenFileDialog1.ShowDialog()
file = OpenFileDialog1.FileNames
txtAttachment.Text = file(0)
Try
TextBox1.Text = file(1)
Catch ex As IndexOutOfRangeException
End Try
Try
TextBox2.Text = file(2)
Catch ex As IndexOutOfRangeException
End Try
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
Dim i As Integer
Do Until i = dt.Rows.Count
'For i = 0 To dt.Rows.Count
smtpserver.Credentials = New Net.NetworkCredential("[email protected]", "xxxxxx")
smtpserver.Host = "smtp.live.com"
smtpserver.Port = "587"
mail = New MailMessage
mail.From = New MailAddress("[email protected]")
mail.To.Add(New MailAddress(dt.Rows(i)("emailname")))
mail.Subject = txtSuject.Text
mail.Body = txtmess.Text.Replace("<!--Link-->", "<a href=""http://localhost:65150/TEST/frmemailcancel.aspx?email=" & des.EncryptString(dt.Rows(i)("emailname")) & """ target=""_blank"">")
mail.IsBodyHtml = True
If Not txtAttachment.Text = Nothing Then
Dim attach As New Attachment(txtAttachment.Text)
mail.Attachments.Add(attach)
End If
If Not TextBox1.Text = Nothing Then
Dim attach As New Attachment(TextBox1.Text)
mail.Attachments.Add(attach)
End If
If Not TextBox2.Text = Nothing Then
Dim attach As New Attachment(TextBox2.Text)
mail.Attachments.Add(attach)
End If
smtpserver.EnableSsl = True
smtpserver.Send(mail)
i += 1
lbl1.Text = "กำลังส่ง : " & i.ToString & "ฉบับ"
Me.Refresh()
_Busy.WaitOne()
If BackgroundWorker1.CancellationPending Then
Return
End If
BackgroundWorker1.ReportProgress(i)
System.Threading.Thread.Sleep(100)
Loop
'Next
i = 0
MessageBox.Show("send mail ok")
dr.Close()
Catch ex As Exception
MsgBox("error")
End Try
End Sub
Private Sub btnsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsend.Click
'btnsend.Enabled = False
'btnPause.Enabled = True
'BackgroundWorker1.RunWorkerAsync()
If (Not BackgroundWorker1.IsBusy) Then
ProgressBar1.Value = 0
BackgroundWorker1.RunWorkerAsync()
End If
_Busy.Set()
btnsend.Enabled = False
btnPause.Enabled = True
'ButtonReset.Enabled = False
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click
'btnsend.Enabled = False
'btnPause.Enabled = True
_Busy.Reset()
btnsend.Enabled = True
btnPause.Enabled = False
'ButtonReset.Enabled = True
End Sub
Private Sub Sendmail_Backgroundworker_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
database.ExecCommand("SELECT_Email_VB", dr)
If dr.HasRows Then
dt = New DataTable
dt.Load(dr)
DataGridView1.DataSource = dt
End If
dr.Close()
Control.CheckForIllegalCrossThreadCalls = False
_Busy = New System.Threading.ManualResetEvent(False)
Label1.Text = String.Empty
btnPause.Enabled = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fd As New OpenFileDialog
With fd
.Filter = "Text Documents (*.txt)|*.html"
If .ShowDialog = Windows.Forms.DialogResult.OK Then
txttemp.Text = .FileName
Dim txtParser As New TextBox
txtParser.Multiline = True
txtmess.Text = My.Computer.FileSystem.ReadAllText(.FileName, System.Text.ASCIIEncoding.Default)
End If
End With
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
file = Nothing
OpenFileDialog1.ShowDialog()
file = OpenFileDialog1.FileNames
txtAttachment.Text = file(0)
Try
TextBox1.Text = file(1)
Catch ex As IndexOutOfRangeException
End Try
Try
TextBox2.Text = file(2)
Catch ex As IndexOutOfRangeException
End Try
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
Dim i As Integer
Do Until i = dt.Rows.Count
'For i = 0 To dt.Rows.Count
smtpserver.Credentials = New Net.NetworkCredential("[email protected]", "xxxxxx")
smtpserver.Host = "smtp.live.com"
smtpserver.Port = "587"
mail = New MailMessage
mail.From = New MailAddress("[email protected]")
mail.To.Add(New MailAddress(dt.Rows(i)("emailname")))
mail.Subject = txtSuject.Text
mail.Body = txtmess.Text.Replace("<!--Link-->", "<a href=""http://localhost:65150/TEST/frmemailcancel.aspx?email=" & des.EncryptString(dt.Rows(i)("emailname")) & """ target=""_blank"">")
mail.IsBodyHtml = True
If Not txtAttachment.Text = Nothing Then
Dim attach As New Attachment(txtAttachment.Text)
mail.Attachments.Add(attach)
End If
If Not TextBox1.Text = Nothing Then
Dim attach As New Attachment(TextBox1.Text)
mail.Attachments.Add(attach)
End If
If Not TextBox2.Text = Nothing Then
Dim attach As New Attachment(TextBox2.Text)
mail.Attachments.Add(attach)
End If
smtpserver.EnableSsl = True
smtpserver.Send(mail)
i += 1
lbl1.Text = "กำลังส่ง : " & i.ToString & "ฉบับ"
Me.Refresh()
_Busy.WaitOne()
If BackgroundWorker1.CancellationPending Then
Return
End If
BackgroundWorker1.ReportProgress(i)
System.Threading.Thread.Sleep(100)
Loop
'Next
i = 0
MessageBox.Show("send mail ok")
dr.Close()
Catch ex As Exception
MsgBox("error")
End Try
End Sub
Private Sub btnsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsend.Click
'btnsend.Enabled = False
'btnPause.Enabled = True
'BackgroundWorker1.RunWorkerAsync()
If (Not BackgroundWorker1.IsBusy) Then
ProgressBar1.Value = 0
BackgroundWorker1.RunWorkerAsync()
End If
_Busy.Set()
btnsend.Enabled = False
btnPause.Enabled = True
'ButtonReset.Enabled = False
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click
'btnsend.Enabled = False
'btnPause.Enabled = True
_Busy.Reset()
btnsend.Enabled = True
btnPause.Enabled = False
'ButtonReset.Enabled = True
End Sub
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.BackgroundWorker1 = New System.ComponentModel.BackgroundWorker()
Me.ProgressBar1 = New System.Windows.Forms.ProgressBar()
Me.LabelPercent = New System.Windows.Forms.Label()
Me.ButtonRun = New System.Windows.Forms.Button()
Me.ButtonPause = New System.Windows.Forms.Button()
Me.ButtonReset = New System.Windows.Forms.Button()
Me.DataGridView1 = New System.Windows.Forms.DataGridView()
Me.Label1 = New System.Windows.Forms.Label()
Me.TextBoxSmtp = New System.Windows.Forms.TextBox()
Me.Label2 = New System.Windows.Forms.Label()
Me.TextBoxPort = New System.Windows.Forms.TextBox()
Me.Label3 = New System.Windows.Forms.Label()
Me.TextBoxEmail = New System.Windows.Forms.TextBox()
Me.Label4 = New System.Windows.Forms.Label()
Me.TextBoxPassword = New System.Windows.Forms.TextBox()
Me.GroupBox1 = New System.Windows.Forms.GroupBox()
Me.GroupBox2 = New System.Windows.Forms.GroupBox()
Me.ButtonBrowse = New System.Windows.Forms.Button()
Me.TextBoxMessage = New System.Windows.Forms.TextBox()
Me.Label6 = New System.Windows.Forms.Label()
Me.ListView1 = New System.Windows.Forms.ListView()
Me.Label5 = New System.Windows.Forms.Label()
Me.GroupBox3 = New System.Windows.Forms.GroupBox()
Me.Label7 = New System.Windows.Forms.Label()
Me.GroupBox4 = New System.Windows.Forms.GroupBox()
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
Me.Column1 = New System.Windows.Forms.DataGridViewTextBoxColumn()
Me.Column2 = New System.Windows.Forms.DataGridViewTextBoxColumn()
Me.Label8 = New System.Windows.Forms.Label()
Me.TextBoxSubject = New System.Windows.Forms.TextBox()
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.GroupBox1.SuspendLayout()
Me.GroupBox2.SuspendLayout()
Me.GroupBox3.SuspendLayout()
Me.GroupBox4.SuspendLayout()
Me.SuspendLayout()
'
'BackgroundWorker1
'
Me.BackgroundWorker1.WorkerReportsProgress = True
Me.BackgroundWorker1.WorkerSupportsCancellation = True
'
'ProgressBar1
'
Me.ProgressBar1.Location = New System.Drawing.Point(15, 31)
Me.ProgressBar1.Name = "ProgressBar1"
Me.ProgressBar1.Size = New System.Drawing.Size(647, 15)
Me.ProgressBar1.TabIndex = 0
'
'LabelPercent
'
Me.LabelPercent.AutoSize = True
Me.LabelPercent.Location = New System.Drawing.Point(12, 57)
Me.LabelPercent.Name = "LabelPercent"
Me.LabelPercent.Size = New System.Drawing.Size(39, 13)
Me.LabelPercent.TabIndex = 1
Me.LabelPercent.Text = "Label1"
'
'ButtonRun
'
Me.ButtonRun.Location = New System.Drawing.Point(402, 76)
Me.ButtonRun.Name = "ButtonRun"
Me.ButtonRun.Size = New System.Drawing.Size(75, 23)
Me.ButtonRun.TabIndex = 2
Me.ButtonRun.Text = "Run"
Me.ButtonRun.UseVisualStyleBackColor = True
'
'ButtonPause
'
Me.ButtonPause.Location = New System.Drawing.Point(483, 76)
Me.ButtonPause.Name = "ButtonPause"
Me.ButtonPause.Size = New System.Drawing.Size(75, 23)
Me.ButtonPause.TabIndex = 3
Me.ButtonPause.Text = "Pause"
Me.ButtonPause.UseVisualStyleBackColor = True
'
'ButtonReset
'
Me.ButtonReset.Location = New System.Drawing.Point(587, 76)
Me.ButtonReset.Name = "ButtonReset"
Me.ButtonReset.Size = New System.Drawing.Size(75, 23)
Me.ButtonReset.TabIndex = 4
Me.ButtonReset.Text = "Reset"
Me.ButtonReset.UseVisualStyleBackColor = True
'
'DataGridView1
'
Me.DataGridView1.AllowUserToAddRows = False
Me.DataGridView1.AllowUserToDeleteRows = False
Me.DataGridView1.AllowUserToResizeColumns = False
Me.DataGridView1.AllowUserToResizeRows = False
Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.Column1, Me.Column2})
Me.DataGridView1.Location = New System.Drawing.Point(14, 64)
Me.DataGridView1.Name = "DataGridView1"
Me.DataGridView1.ReadOnly = True
Me.DataGridView1.RowHeadersVisible = False
Me.DataGridView1.Size = New System.Drawing.Size(306, 310)
Me.DataGridView1.TabIndex = 5
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(15, 35)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(74, 13)
Me.Label1.TabIndex = 6
Me.Label1.Text = "SMTP Server:"
'
'TextBoxSmtp
'
Me.TextBoxSmtp.Location = New System.Drawing.Point(95, 32)
Me.TextBoxSmtp.Name = "TextBoxSmtp"
Me.TextBoxSmtp.Size = New System.Drawing.Size(466, 20)
Me.TextBoxSmtp.TabIndex = 7
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(567, 35)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(29, 13)
Me.Label2.TabIndex = 8
Me.Label2.Text = "Port:"
'
'TextBoxPort
'
Me.TextBoxPort.Location = New System.Drawing.Point(602, 32)
Me.TextBoxPort.Name = "TextBoxPort"
Me.TextBoxPort.Size = New System.Drawing.Size(50, 20)
Me.TextBoxPort.TabIndex = 9
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(15, 72)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(39, 13)
Me.Label3.TabIndex = 10
Me.Label3.Text = "E-Mail:"
'
'TextBoxEmail
'
Me.TextBoxEmail.Location = New System.Drawing.Point(95, 69)
Me.TextBoxEmail.Name = "TextBoxEmail"
Me.TextBoxEmail.Size = New System.Drawing.Size(244, 20)
Me.TextBoxEmail.TabIndex = 11
'
'Label4
'
Me.Label4.AutoSize = True
Me.Label4.Location = New System.Drawing.Point(15, 108)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(56, 13)
Me.Label4.TabIndex = 12
Me.Label4.Text = "Password:"
'
'TextBoxPassword
'
Me.TextBoxPassword.Location = New System.Drawing.Point(95, 105)
Me.TextBoxPassword.Name = "TextBoxPassword"
Me.TextBoxPassword.PasswordChar = Global.Microsoft.VisualBasic.ChrW(42)
Me.TextBoxPassword.Size = New System.Drawing.Size(244, 20)
Me.TextBoxPassword.TabIndex = 13
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.Label1)
Me.GroupBox1.Controls.Add(Me.TextBoxPassword)
Me.GroupBox1.Controls.Add(Me.TextBoxSmtp)
Me.GroupBox1.Controls.Add(Me.Label4)
Me.GroupBox1.Controls.Add(Me.Label2)
Me.GroupBox1.Controls.Add(Me.TextBoxEmail)
Me.GroupBox1.Controls.Add(Me.TextBoxPort)
Me.GroupBox1.Controls.Add(Me.Label3)
Me.GroupBox1.Location = New System.Drawing.Point(12, 12)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(677, 147)
Me.GroupBox1.TabIndex = 14
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Mail Server"
'
'GroupBox2
'
Me.GroupBox2.Controls.Add(Me.TextBoxSubject)
Me.GroupBox2.Controls.Add(Me.Label8)
Me.GroupBox2.Controls.Add(Me.ButtonBrowse)
Me.GroupBox2.Controls.Add(Me.TextBoxMessage)
Me.GroupBox2.Controls.Add(Me.Label6)
Me.GroupBox2.Controls.Add(Me.ListView1)
Me.GroupBox2.Controls.Add(Me.Label5)
Me.GroupBox2.Location = New System.Drawing.Point(12, 165)
Me.GroupBox2.Name = "GroupBox2"
Me.GroupBox2.Size = New System.Drawing.Size(336, 390)
Me.GroupBox2.TabIndex = 15
Me.GroupBox2.TabStop = False
Me.GroupBox2.Text = "Mail Body"
'
'ButtonBrowse
'
Me.ButtonBrowse.Location = New System.Drawing.Point(82, 66)
Me.ButtonBrowse.Name = "ButtonBrowse"
Me.ButtonBrowse.Size = New System.Drawing.Size(75, 23)
Me.ButtonBrowse.TabIndex = 4
Me.ButtonBrowse.Text = "Browse..."
Me.ButtonBrowse.UseVisualStyleBackColor = True
'
'TextBoxMessage
'
Me.TextBoxMessage.Location = New System.Drawing.Point(15, 239)
Me.TextBoxMessage.Multiline = True
Me.TextBoxMessage.Name = "TextBoxMessage"
Me.TextBoxMessage.Size = New System.Drawing.Size(306, 135)
Me.TextBoxMessage.TabIndex = 3
'
'Label6
'
Me.Label6.AutoSize = True
Me.Label6.Location = New System.Drawing.Point(12, 214)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(53, 13)
Me.Label6.TabIndex = 2
Me.Label6.Text = "Message:"
'
'ListView1
'
Me.ListView1.GridLines = True
Me.ListView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable
Me.ListView1.Location = New System.Drawing.Point(15, 101)
Me.ListView1.MultiSelect = False
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(306, 103)
Me.ListView1.TabIndex = 1
Me.ListView1.UseCompatibleStateImageBehavior = False
Me.ListView1.View = System.Windows.Forms.View.Details
'
'Label5
'
Me.Label5.AutoSize = True
Me.Label5.Location = New System.Drawing.Point(12, 71)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(64, 13)
Me.Label5.TabIndex = 0
Me.Label5.Text = "Attachment:"
'
'GroupBox3
'
Me.GroupBox3.Controls.Add(Me.Label7)
Me.GroupBox3.Controls.Add(Me.DataGridView1)
Me.GroupBox3.Location = New System.Drawing.Point(354, 165)
Me.GroupBox3.Name = "GroupBox3"
Me.GroupBox3.Size = New System.Drawing.Size(335, 390)
Me.GroupBox3.TabIndex = 16
Me.GroupBox3.TabStop = False
Me.GroupBox3.Text = "Contact List"
'
'Label7
'
Me.Label7.AutoSize = True
Me.Label7.Location = New System.Drawing.Point(11, 34)
Me.Label7.Name = "Label7"
Me.Label7.Size = New System.Drawing.Size(39, 13)
Me.Label7.TabIndex = 6
Me.Label7.Text = "Label7"
'
'GroupBox4
'
Me.GroupBox4.Controls.Add(Me.ProgressBar1)
Me.GroupBox4.Controls.Add(Me.LabelPercent)
Me.GroupBox4.Controls.Add(Me.ButtonRun)
Me.GroupBox4.Controls.Add(Me.ButtonPause)
Me.GroupBox4.Controls.Add(Me.ButtonReset)
Me.GroupBox4.Location = New System.Drawing.Point(12, 561)
Me.GroupBox4.Name = "GroupBox4"
Me.GroupBox4.Size = New System.Drawing.Size(677, 112)
Me.GroupBox4.TabIndex = 17
Me.GroupBox4.TabStop = False
Me.GroupBox4.Text = "Sent"
'
'OpenFileDialog1
'
Me.OpenFileDialog1.FileName = "OpenFileDialog1"
'
'Column1
'
Me.Column1.DataPropertyName = "Index"
Me.Column1.HeaderText = "#"
Me.Column1.Name = "Column1"
Me.Column1.ReadOnly = True
Me.Column1.Width = 50
'
'Column2
'
Me.Column2.DataPropertyName = "Email"
Me.Column2.HeaderText = "Email Address"
Me.Column2.Name = "Column2"
Me.Column2.ReadOnly = True
Me.Column2.Width = 225
'
'Label8
'
Me.Label8.AutoSize = True
Me.Label8.Location = New System.Drawing.Point(12, 34)
Me.Label8.Name = "Label8"
Me.Label8.Size = New System.Drawing.Size(46, 13)
Me.Label8.TabIndex = 5
Me.Label8.Text = "Subject:"
'
'TextBoxSubject
'
Me.TextBoxSubject.Location = New System.Drawing.Point(64, 31)
Me.TextBoxSubject.Name = "TextBoxSubject"
Me.TextBoxSubject.Size = New System.Drawing.Size(257, 20)
Me.TextBoxSubject.TabIndex = 6
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(701, 687)
Me.Controls.Add(Me.GroupBox4)
Me.Controls.Add(Me.GroupBox3)
Me.Controls.Add(Me.GroupBox2)
Me.Controls.Add(Me.GroupBox1)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.Text = "Test"
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox1.PerformLayout()
Me.GroupBox2.ResumeLayout(False)
Me.GroupBox2.PerformLayout()
Me.GroupBox3.ResumeLayout(False)
Me.GroupBox3.PerformLayout()
Me.GroupBox4.ResumeLayout(False)
Me.GroupBox4.PerformLayout()
Me.ResumeLayout(False)
End Sub
Friend WithEvents BackgroundWorker1 As System.ComponentModel.BackgroundWorker
Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
Friend WithEvents LabelPercent As System.Windows.Forms.Label
Friend WithEvents ButtonRun As System.Windows.Forms.Button
Friend WithEvents ButtonPause As System.Windows.Forms.Button
Friend WithEvents ButtonReset As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBoxSmtp As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TextBoxPort As System.Windows.Forms.TextBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents TextBoxEmail As System.Windows.Forms.TextBox
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents TextBoxPassword As System.Windows.Forms.TextBox
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
Friend WithEvents TextBoxMessage As System.Windows.Forms.TextBox
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
Friend WithEvents GroupBox4 As System.Windows.Forms.GroupBox
Friend WithEvents ButtonBrowse As System.Windows.Forms.Button
Friend WithEvents Label7 As System.Windows.Forms.Label
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents Column1 As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Column2 As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView
Friend WithEvents TextBoxSubject As System.Windows.Forms.TextBox
Friend WithEvents Label8 As System.Windows.Forms.Label
End Class
Form1.vb
Imports System.Net.Mail
Imports System.IO
Imports System.Net.Mime
Public Class Form1
Private Busy As System.Threading.ManualResetEvent
Private ContactList As DataTable
Private FilePaths As List(Of String)
Private SentResult As Boolean
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Busy = New System.Threading.ManualResetEvent(False)
ContactList = GetContactList()
FilePaths = New List(Of String)()
LabelPercent.Text = String.Empty
ButtonPause.Enabled = False
PrepareListView()
DataGridView1.DataSource = ContactList
Label7.Text = String.Format("Total list: {0} Items.", ContactList.Rows.Count.ToString())
End Sub
Private Sub ButtonRun_Click(sender As System.Object, e As System.EventArgs) Handles ButtonRun.Click
If TextBoxSmtp.Text <> String.Empty Or TextBoxPort.Text <> String.Empty Or TextBoxEmail.Text <> String.Empty Or TextBoxPassword.Text <> String.Empty Then
If (Not BackgroundWorker1.IsBusy) Then
ProgressBar1.Value = 0
BackgroundWorker1.RunWorkerAsync()
End If
Busy.Set()
ButtonRun.Enabled = False
ButtonPause.Enabled = True
ButtonReset.Enabled = False
Else
MessageBox.Show(String.Format("I'm not ready"), "Warning")
End If
End Sub
Private Sub ButtonPause_Click(sender As System.Object, e As System.EventArgs) Handles ButtonPause.Click
Busy.Reset()
LabelPercent.Text = "Pause"
ButtonRun.Enabled = True
ButtonPause.Enabled = False
ButtonReset.Enabled = True
End Sub
Private Sub ButtonReset_Click(sender As System.Object, e As System.EventArgs) Handles ButtonReset.Click
If (BackgroundWorker1.IsBusy) Then
' Set CancellationPending property to true
BackgroundWorker1.CancelAsync()
' Unblock worker so it can see that
Busy.Set()
ButtonRun.Enabled = True
ButtonPause.Enabled = False
ButtonReset.Enabled = True
End If
ProgressBar1.Value = 0
LabelPercent.Text = String.Empty
End Sub
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim Percent As Double = 0.0F
SentResult = True
For Each Email As DataRow In ContactList.Rows
Busy.WaitOne()
If BackgroundWorker1.CancellationPending Then
Return
End If
SentResult = SentEmail(Email("Email").ToString())
Percent += 100.0F / Convert.ToDouble(ContactList.Rows.Count)
BackgroundWorker1.ReportProgress(Convert.ToInt32(Math.Round(Percent)), Email("Email").ToString())
' System.Threading.Thread.Sleep(1000)
If Not SentResult Then
Exit For
End If
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
LabelPercent.Text = String.Format("{0}%", e.ProgressPercentage.ToString())
LabelPercent.Text = String.Format("Sending to {0}", e.UserState.ToString())
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If SentResult Then
LabelPercent.Text = "Success"
Else
LabelPercent.Text = "Error"
End If
ButtonRun.Enabled = True
ButtonPause.Enabled = False
ButtonReset.Enabled = True
End Sub
Private Sub ButtonBrowse_Click(sender As System.Object, e As System.EventArgs) Handles ButtonBrowse.Click
OpenFileDialog1.Multiselect = True
OpenFileDialog1.FileName = String.Empty
Dim result As DialogResult = OpenFileDialog1.ShowDialog()
If result = Windows.Forms.DialogResult.OK And OpenFileDialog1.FileNames.Length > 0 Then
For Each filePath As String In OpenFileDialog1.FileNames
Dim aFile As New FileInfo(filePath)
If (From f In FilePaths Where f = aFile.FullName Select f).Count() = 0 Then
UpdateListView(aFile.Name, aFile.Length)
FilePaths.Add(filePath)
Else
MessageBox.Show(String.Format("'{0}' is already exist.", aFile.Name), "Warning")
End If
Next
End If
End Sub
Private Sub PrepareListView()
' Clear ListView Column.
If (ListView1.Columns.Count > 0) Then
ListView1.Columns.Clear()
End If
' Create columns for the items and subitems.
' Width of -2 indicates auto-size.
ListView1.Columns.Add(String.Empty, 20, HorizontalAlignment.Left)
ListView1.Columns.Add("File Name", 190, HorizontalAlignment.Left)
ListView1.Columns.Add("Size", 70, HorizontalAlignment.Left)
' Create two ImageList objects.
Dim imageListSmall As New ImageList()
imageListSmall.ImageSize = New System.Drawing.Size(16, 16)
' Initialize the ImageList objects with bitmaps.
imageListSmall.Images.Add(My.Resources.attachment)
'Assign the ImageList objects to the ListView.
ListView1.SmallImageList = imageListSmall
End Sub
Private Sub UpdateListView(FileName As String, FileSize As Integer)
Dim item As New ListViewItem(String.Empty, 0)
item.UseItemStyleForSubItems = False
item.SubItems.Add(FileName)
item.SubItems.Add(FileSize.ToString("#,##0"))
ListView1.Items.Add(item)
End Sub
Private Function GetContactList()
' ================== แก้ตรงนี้เองแล้วกัน ====================
' ================== อยากได้ mail address จากไหนก็ทำเอาเองตรงนี้
Dim Dt As New DataTable
Dt.Columns.Add(New DataColumn("index", GetType(Integer)))
Dt.Columns.Add(New DataColumn("Email", GetType(String)))
For i As Integer = 1 To 80
Dim DrNew As DataRow = Dt.NewRow()
DrNew("Index") = i
DrNew("Email") = String.Format("xxxxxxx{0}@email.com", i.ToString("00"))
Dt.Rows.Add(DrNew)
Next
' ================== แก้ตรงนี้เองแล้วกัน ====================
GetContactList = Dt
End Function
Private Function SentEmail(MailAddress As String)
Dim result As Boolean = True
Dim mailTo As New MailAddress(MailAddress)
Dim mailFrom As New MailAddress(TextBoxEmail.Text)
Dim message As New MailMessage(mailFrom, mailTo)
message.IsBodyHtml = True
message.Subject = TextBoxSubject.Text
message.Body = TextBoxMessage.Text
For Each FilePath In FilePaths
' Create the file attachment for this e-mail message.
Dim AttachFile As New Attachment(FilePath)
' Add time stamp information for the file.
Dim disposition As ContentDisposition = AttachFile.ContentDisposition
disposition.CreationDate = File.GetCreationTime(FilePath)
disposition.ModificationDate = File.GetLastWriteTime(FilePath)
disposition.ReadDate = File.GetLastAccessTime(FilePath)
message.Attachments.Add(AttachFile)
Next
Dim smtp As New SmtpClient(TextBoxSmtp.Text, Convert.ToInt32(TextBoxPort.Text))
smtp.EnableSsl = True
smtp.UseDefaultCredentials = False
smtp.Credentials = New System.Net.NetworkCredential(TextBoxEmail.Text, TextBoxPassword.Text)
Try
smtp.Send(message)
Catch ex As SmtpException
result = False
MessageBox.Show(String.Format("Error: {0}", ex.StatusCode))
End Try
SentEmail = result
End Function
End Class
Private Sub Sendmail_Backgroundworker_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
database.ExecCommand("SELECT_Email_VB", dr)
If dr.HasRows Then
dt = New DataTable
dt.Load(dr)
DataGridView1.DataSource = dt
End If
dr.Close()
Control.CheckForIllegalCrossThreadCalls = False
_Busy = New System.Threading.ManualResetEvent(False)
Label1.Text = String.Empty
btnPause.Enabled = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fd As New OpenFileDialog
With fd
.Filter = "Text Documents (*.txt)|*.html"
If .ShowDialog = Windows.Forms.DialogResult.OK Then
txttemp.Text = .FileName
Dim txtParser As New TextBox
txtParser.Multiline = True
txtmess.Text = My.Computer.FileSystem.ReadAllText(.FileName, System.Text.ASCIIEncoding.Default)
End If
End With
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
file = Nothing
OpenFileDialog1.ShowDialog()
file = OpenFileDialog1.FileNames
txtAttachment.Text = file(0)
Try
TextBox1.Text = file(1)
Catch ex As IndexOutOfRangeException
End Try
Try
TextBox2.Text = file(2)
Catch ex As IndexOutOfRangeException
End Try
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
Dim i As Integer
For i = 0 To dt.Rows.Count - 1
''Debug.WriteLine(i)
smtpserver.Credentials = New Net.NetworkCredential("[email protected]", "xxxxxx")
smtpserver.Host = "smtp.live.com"
smtpserver.Port = "587"
mail = New MailMessage
mail.From = New MailAddress("[email protected]")
mail.To.Add(New MailAddress(dt.Rows(i)("emailname")))
mail.Subject = txtSuject.Text
mail.Body = txtmess.Text.Replace("<!--Link-->", "<a href=""http://localhost:65150/TEST/frmemailcancel.aspx?email=" & des.EncryptString(dt.Rows(i)("emailname")) & """ target=""_blank"">")
mail.IsBodyHtml = True
If Not txtAttachment.Text = Nothing Then
Dim attach As New Attachment(txtAttachment.Text)
mail.Attachments.Add(attach)
End If
If Not TextBox1.Text = Nothing Then
Dim attach As New Attachment(TextBox1.Text)
mail.Attachments.Add(attach)
End If
If Not TextBox2.Text = Nothing Then
Dim attach As New Attachment(TextBox2.Text)
mail.Attachments.Add(attach)
End If
smtpserver.EnableSsl = True
smtpserver.Send(mail)
lbl1.Text = "กำลังส่ง : " & i.ToString & "ฉบับ" 'ของ do unit
Dim icount As Integer 'ของ for
icount += 1 'ของ for
lbl1.Text = "กำลังส่ง : " & icount.ToString & "ฉบับ" 'ของ for
Me.Refresh()
_Busy.WaitOne()
If BackgroundWorker1.CancellationPending Then
Return
End If
BackgroundWorker1.ReportProgress(CInt((icount / dt.Rows.Count) * 100)) 'ของ for
System.Threading.Thread.Sleep(100)
Next
i = 0 'do unit
MessageBox.Show("send mail ok")
dr.Close()
Catch ex As Exception
MsgBox("error")
End Try
End Sub
Private Sub btnsend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsend.Click
If (Not BackgroundWorker1.IsBusy) Then
ProgressBar1.Value = 0
BackgroundWorker1.RunWorkerAsync()
End If
_Busy.Set()
btnsend.Enabled = False
btnPause.Enabled = True
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
ProgressBar1.PerformStep()
End Sub
Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click
_Busy.Reset()
btnsend.Enabled = True
btnPause.Enabled = False
'ButtonReset.Enabled = True
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
btnsend.Enabled = True
btnPause.Enabled = False
End Sub
End Class