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