Private Sub cmdProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcess.Click
' STEP ONE - Setup INI File
Dim objIniFile As New IniFile(Application.StartupPath & "\settings.ini")
' STEP TWO - Get Settings from INI File
Dim vCustomerEmailDirectory As String = objIniFile.GetString("Settings", "vCustomerEmailDirectory", "")
Dim vOtherAttachmentsDirectory As String = objIniFile.GetString("Settings", "vOtherAttachmentsDirectory", "")
Dim vFromEmailAddress As String = objIniFile.GetString("Settings", "vFromEmailAddress", "")
Dim vFromName As String = objIniFile.GetString("Settings", "vFromName", "")
Dim vRecipientEmailAddress As String = objIniFile.GetString("Settings", "vRecipientEmailAddress", "")
Dim vBCCRecipient As String = objIniFile.GetString("Settings", "vBCCRecipient", "")
Dim vUseSMTPServer As String = objIniFile.GetString("Settings", "vUseSMTPServer", "")
Dim vSMTPServer As String = objIniFile.GetString("Settings", "vSMTPServer", "")
Dim vSMTPPort As String = objIniFile.GetString("Settings", "vSMTPPort", "")
Dim vSMTPUsername As String = objIniFile.GetString("Settings", "vSMTPUsername", "")
Dim vSMTPPassword As String = objIniFile.GetString("Settings", "vSMTPPassword", "")
Dim vMessageSubject As String = objIniFile.GetString("Settings", "vMessageSubject", "")
Dim vMessagePriority As String = objIniFile.GetString("Settings", "vMessagePriority", "")
Dim vMessageBody As String = objIniFile.GetString("Settings", "vMessageBody", "")
Dim vAltMessageBody As String = objIniFile.GetString("Settings", "vAltMessageBody", "")
' STEP THREE - Create Customer Collection & Populate with Directory Names from vCustomerEmailDirectory
Dim vCustomers As New Collection
Dim fFileSystemInfo As FileSystemInfo
Dim dDir As New DirectoryInfo(vCustomerEmailDirectory)
For Each fFileSystemInfo In dDir.GetFileSystemInfos()
If fFileSystemInfo.Attributes = FileAttributes.Directory Then
vCustomers.Add(fFileSystemInfo.Name.ToString)
End If
Next
'STEP FOUR - Setup MailBee Email Components
Smtp.LicenseKey = "MN600-1DD5EA7BD5B7D56FD51316C6DA1E-1CEA".ToString
mailer = New Smtp
mailer.DnsServers.Clear()
mailer.SmtpServers.Clear()
If vUseSMTPServer.ToString <> "True" Then
mailer.DnsServers.Autodetect()
Else
If vSMTPUsername.Length = 0 Then
mailer.SmtpServers.Add(vSMTPServer.ToString)
Else
mailer.SmtpServers.Add(vSMTPServer.ToString, vSMTPUsername.ToString, vSMTPPassword.ToString, AuthenticationMethods.SaslLogin Or AuthenticationMethods.SaslPlain)
End If
End If
'STEP FIVE - Cycle from Customer Object and Send Mails to Each Customer
For Each i In vCustomers
' STEP 1 - Setup Customer INI File Access
Dim CustIniFile As New IniFile(vCustomerEmailDirectory.ToString & i.ToString & "\settings.ini")
' STEP 2 - Set from Email, BCC Email & Subject Fields
mailer.From.Email = vFromEmailAddress.ToString
mailer.From.DisplayName = vFromName.ToString
mailer.Bcc.AsString = vBCCRecipient.ToString
' STEP 3 - Get Customer Name & Email Address
Dim vCustomerName As String = CustIniFile.GetString("Settings", "RecipientName", "")
vCustomerName = StrConv(vCustomerName, VbStrConv.ProperCase)
Dim vCustomerEmailAddress As String = CustIniFile.GetString("Settings", "RecipientAddress", "").ToLower
' STEP 4 - Set To Displayname and Email Address
mailer.To.AsString = vCustomerName.ToString & "<" & vCustomerEmailAddress.ToString & ">"
' STEP 5 - Setup Message Subject & Body
mailer.Subject = vMessageSubject.Replace("%custname%", vCustomerName).Replace("%custid%", i.ToString).ToString
mailer.Message.BodyHtmlText = vMessageBody.Replace("%custname%", vCustomerName).Replace("%custid%", i.ToString).ToString
mailer.Message.BodyPlainText = vAltMessageBody.Replace("%custname%", vCustomerName).Replace("%custid%", i.ToString).ToString
' STEP 6 - Send Email
Try
If mailer.Send() Then
Dim lv As ListViewItem = lvStatus.Items.Add(Now.ToString("hh:mm:ss tt"))
lv.SubItems.Add(i.ToString)
lv.SubItems.Add(vCustomerEmailAddress)
lv.SubItems.Add("Sent Successfully")
End If
Catch ex As MailBeeException
MessageBox.Show(ex.Message, "Error")
If TypeOf ex Is MailBeeConnectionException Then
MessageBox.Show("Typical reason is blocked SMTP port. The port may be blocked by firewall, antispam or antivirus software installed on your system or your ISP's network.")
End If
End Try
' STEP 7 - Reset Message for Next Customer
mailer.Message.Attachments.Clear()
mailer.ResetMessage()
Next i
End Sub
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Most Valuable Expert award recognizes technology experts who passionately share their knowledge with the community, demonstrate the core values of this platform, and go the extra mile in all aspects of their contributions. This award is based off of nominations by EE users and experts. Multiple MVEs may be awarded each year.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.