Hi professionals,
I am a student who is working on a project for a company on mass emailing system. How do i add a Bcc attribute in my program? I found a few codes in the internet but all the codes are for pre-defined email address. What i am planning to do is to make my program such that the user can input their recipients' email addresses onto the BCC richtextbox(rtbBCC) instead of putting a pre-defined email address in my windows application design.
Below are the codes that i have already created with the help of some helpful experts in this forums and online tutorials:-
Imports System
Imports System.Net
Imports System.IO
Imports System.Net.Mail
Public Class Email
'Declarations
Private mailMessage As New Net.Mail.MailMessage()
Dim attachment As Net.Mail.Attachment
Public Sub SendMassEmail(ByVal MsgFrom As String, ByVal MsgTo As String, ByVal MsgSubject As String, ByVal MsgBody As String)
'This procedure takes string array parameters for multiple recipients and files
Try
' Pass in the message information to a new MailMessage
mailMessage = New Net.Mail.MailMessage(MsgFrom, MsgTo, MsgSubject, MsgBody)
' Adding attachments
Dim attachment As Net.Mail.Attachment = New Net.Mail.Attachment(ofdAttach.FileName)
mailMessage.Attachments.Add(New Attachment(txtAttach.Text))
' Create an SmtpClient to send the e-mail
Dim mailClient As New SmtpClient("152.226.153.93") ' = local machine IP Address
' Use the Windows credentials of the current User
mailClient.UseDefaultCredentials = True
' Pass the message to the mail server
mailClient.Send(mailMessage)
' Optional user reassurance:
MessageBox.Show(String.Format("Message Successfully Sent!" & vbCrLf & vbCrLf & "Message Subject: {0}" & vbCrLf & "FROM: {1}" & vbCrLf & "TO: {2}", MsgSubject, MsgFrom, MsgTo), "EMail", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Information)
' Housekeeping
'mailMessage.Dispose()
'Error Checking
Catch ex As FormatException
MessageBox.Show(ex.Message & " :Format Exception")
Catch ex As SmtpException
MessageBox.Show(ex.Message & " :SMTP Exception")
End Try
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
'Error Messages
If rtbBcc.Text = "" Then
MsgBox("Please enter the recipient(s)", MsgBoxStyle.Information, "Send Email")
Exit Sub
End If
If txtFrom.Text = "" Then
MsgBox("Please enter the sender", MsgBoxStyle.Information, "Send Email")
Exit Sub
End If
If txtMsg.Text = "" Then
MsgBox("Please enter the email message(s)", MsgBoxStyle.Information, "Send Email")
Exit Sub
End If
'Calling the SendMassEmail() from a button
SendMassEmail(txtFrom.Text, rtbTo.Text, txtSubj.Text, txtMsg.Text)
End Sub