Imports Microsoft.VisualBasic
Imports System.Net.Mail
Imports System.Text
Public Class SendEmail
Dim smtpCredentials As New Net.NetworkCredential("username", "password")
Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo() _
As String, ByVal strSubject _
As String, ByVal strMessage _
As String, ByVal fileList() As String)
'This procedure takes string array parameters for multiple recipients and files
Try
For Each item As String In strTo
'For each to address create a mail message
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(item))
MailMsg.BodyEncoding = Encoding.Default
MailMsg.Subject = strSubject.Trim()
MailMsg.Body = strMessage.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True
'attach each file attachment
For Each strfile As String In fileList
If Not strfile = "" Then
Dim MsgAttach As New Attachment(strfile)
MailMsg.Attachments.Add(MsgAttach)
End If
Next
'Smtpclient to send the mail message
Dim SmtpMail As New SmtpClient
SmtpMail.UseDefaultCredentials = False
SmtpMail.Host = "hostname"
SmtpMail.EnableSsl = True
SmtpMail.Credentials = smtpCredentials
SmtpMail.Send(MailMsg)
Next
MessageBox.Show("Success")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Sub SendEmailMessage(ByVal strFrom As String, ByVal strTo _
As String, ByVal strSubject _
As String, ByVal strMessage _
As String, ByVal file As String)
'This procedure overrides the first procedure and accepts a single
'string for the recipient and file attachement
Try
Dim MailMsg As New MailMessage(New MailAddress(strFrom.Trim()), New MailAddress(strTo))
MailMsg.BodyEncoding = Encoding.Default
MailMsg.Subject = strSubject.Trim()
MailMsg.Body = strMessage.Trim() & vbCrLf
MailMsg.Priority = MailPriority.High
MailMsg.IsBodyHtml = True
If Not file = "" Then
Dim MsgAttach As New Attachment(file)
MailMsg.Attachments.Add(MsgAttach)
End If
'Smtpclient to send the mail message
Dim SmtpMail As New SmtpClient
SmtpMail.UseDefaultCredentials = False
SmtpMail.Host = "mail.elections.gov.mv"
SmtpMail.Credentials = smtpCredentials
SmtpMail.Send(MailMsg)
MessageBox.Show("List has been Successfully mailed to Elections Commission", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
'Send Mail Button Code
Private Sub btnSendEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendEmail.Click
Dim SendEmail As New SendEmail
Dim SendTo As String
Dim FileAttach As String = Nothing
Dim strSubject As String
Dim strMessage As String
SendTo = "to"
FileAttach = "location of the Attachment"
strSubject = "Subject"
strMessage = "Message" 'The body encoding is set to HTML
SendEmail.SendEmailMessage("frommail", SendTo, strSubject, strMessage, FileAttach)
End Sub
Private Sub cmbConsit_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbConsit.SelectedIndexChanged
values(cmbConsit.SelectedItem)
End Sub