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

Use MailMessage.Bcc collection - It is used to add a BCC recipient to an e-mail message, create a MailAddress for the recipient's address, and then add that object to the collection returned by the Bcc property.

Use MailMessage.Bcc collection - It is used to add a BCC recipient to an e-mail message, create a MailAddress for the recipient's address, and then add that object to the collection returned by the Bcc property.

Alright. I found the website on how to apply the MailMessage.Bcc.
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.bcc.aspx
But how do i apply it in my SendMassEmail method?

Property Bcc is collection of MailAddress objects.

....
 mailMessage.Bcc.Add(new MailAddress("aa@aa.com"))
 mailMessage.Bcc.Add(new MailAddress("bb@aa.com"))
 mailClient.Send(mailMessage)
 ....

Property Bcc is collection of MailAddress objects.

....
 mailMessage.Bcc.Add(new MailAddress("aa@aa.com"))
 mailMessage.Bcc.Add(new MailAddress("bb@aa.com"))
 mailClient.Send(mailMessage)
 ....

Thank you. But i do not want a predefined Bcc feature. What i planned is to program my application such that the user can manually input a few email address in to the rich textbox.

Use following code for comma seperated emails,

...
    s = "aa@aa.com,bb@bb.com,cc@cc.com"
    Dim addr As String() = s.Split(New String() {","}, System.StringSplitOptions.RemoveEmptyEntries)
    For Each ele As String In addr
            mailMessage.Bcc.Add(New MailAddress(ele))
    Next

Use following code for comma seperated emails,

...
    s = "aa@aa.com,bb@bb.com,cc@cc.com"
    Dim addr As String() = s.Split(New String() {","}, System.StringSplitOptions.RemoveEmptyEntries)
    For Each ele As String In addr
            mailMessage.Bcc.Add(New MailAddress(ele))
    Next

Actually i used streamreader to insert the commas. But my question is how do i insert the mailMessage.Bcc.Add() into the SendMassEmail method?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.