Hi Professionals,

I am working on a mass emailing program. I can send mass email successfully. My question is how do i add attachments? i have a label(lblAttachment), a textbox(txtAttachment) and a button(btnBrowse) on the attachment row.

Here are my codes for the form:-

Please advise.

Thank you in advance.

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Net.Mail
Imports System.IO

Public Class Form1


    Public Sub SendMassEmail(ByVal MsgFrom As String, ByVal MsgTo As String, ByVal MsgSubject As String, ByVal MsgBody As String)


        Try

            '  Pass in the message information to a new MailMessage

            Dim mailMessage As New Net.Mail.MailMessage(MsgFrom, MsgTo, MsgSubject, MsgBody)


            '   Create an SmtpClient to send the e-mail

            Dim mailClient As New SmtpClient("152.226.152.152")  '  = 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 Subject:    ' {0} '    successfully sent     FROM:    '{1}'    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

        SendMassEmail(txtFrom.Text, rtbTo.Text, txtSubj.Text, txtMsg.Text)

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        txtFrom.Text = ""
        rtbTo.Text = ""
        txtSubj.Text = ""
        txtMsg.Text = ""

    End Sub

    Private Sub btnTo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim DGV As New DGV
        DGV.Show()

    End Sub

    Private Sub btnClrMsg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClrMsg.Click

        txtMsg.Text = ""

    End Sub

    Private Sub btnBrowseTo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseTo.Click

        ofdComma.ShowDialog()

    End Sub

    Private Sub ofdComma_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ofdComma.FileOk

        Dim sr As New StreamReader(ofdComma.FileName)

        While Not sr.EndOfStream

            rtbTo.AppendText(sr.ReadLine & ", ")

            rtbTo.ScrollToCaret()

        End While

        sr.Close()
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        txtFrom.Text = "0611145D@student.tp.edu.sg"

    End Sub
End Class

Attachments collection of MailMessage instance.

mailMessage.Attachments(new System.Net.Mail.Attachment("c:\file.ext"))

Attachments collection of MailMessage instance.

mailMessage.Attachments(new System.Net.Mail.Attachment("c:\file.ext"))

Thank you very much. That worked great if i wanted to send a specific attachment. But what if i want to insert an attachment by clicking a button that opens an OpenFileDialog, and i choose the file and the directory of the file will be displayed in a textbox?

Dim file as String = txtFile.Text
mailMessage.Attachments(new System.Net.Mail.Attachment(file))
Dim file as String = txtFile.Text
mailMessage.Attachments(new System.Net.Mail.Attachment(file))

Where should i place those codes? Form_Load or browse button

Add following code in brows button's click event.

txtFile.Text=openFiledialog1.FileName

Add following code in brows button's click event.

txtFile.Text=openFiledialog1.FileName

Thank you very much for your time and contribution. That worked great.

Last but not least, how do i remove the attachments if i decided not to insert any attachments?

Remove, RemoveAt, Clear methods of Attachments collection.

Remove, RemoveAt, Clear methods of Attachments collection.

Does the code look like this:-

msg.Attachments.Clear()

Remove, RemoveAt, Clear methods of Attachments collection.

ok so i discovered how to put the method which is :-

mailMessage.Attachments.Remove(My_Attach)

but how do i call it from a button?

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.