Bulk email send with Attached file in VB.NET
In this article, We will see how to send bulk email and with attached file in VB.NET
In this article, We will see how to send bulk email and with attached file in VB.NET.This technique is very best for bulk email send. We can send bulk mail at the one time throw the database.
This is aspx code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="tblemail" runat="server" cellpadding="0" cellspacing="0" width="100%"
bgcolor="#66ff66">
<tr>
<td>
<fieldset>
<legend style="border: none; font-weight: bold; color: #660099; font-size: 12pt;
font-family: Segoe UI">Send Your Email </legend>
<table id="tblbody" runat="server" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td colspan="2" align="center">
Fields marked with an asterisk (<asp:Label ID="Label1" runat="server" ForeColor="Red"
Text="*"></asp:Label>)are mandatory
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Label ID="lblmail" ForeColor="Red" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td width="10%" align="right">
<asp:Label ID="lblfrom" runat="server" Text="From:"></asp:Label>
<asp:Label ID="lblredstar3" runat="server" ForeColor="Red" Text="*"></asp:Label>
</td>
<td align="left" width="90%">
<asp:TextBox ID="txtemailfrom" Width="50%" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ForeColor="Red"
Display="static" ControlToValidate="txtemailfrom" ErrorMessage="Enter valid Email Address"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+*"> </asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td width="10%" align="right">
<asp:Label ID="lblsubject" runat="server" Text="Subject:"></asp:Label>
<asp:Label ID="lblredstar4" runat="server" ForeColor="Red" Text="*"></asp:Label>
</td>
<td align="left" width="90%">
<asp:TextBox ID="txtsubject" Width="50%" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td width="10%" align="right">
<asp:Label ID="lblbody" runat="server" Text="Body:"></asp:Label>
<asp:Label ID="lblredstar5" runat="server" ForeColor="Red" Text="*"></asp:Label>
</td>
<td align="left" width="90%">
<asp:TextBox ID="txtbody" Width="50%" runat="server" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td width="10%" align="right">
<asp:Label ID="lblAttachFile" runat="server" Text="File to send:"></asp:Label>
</td>
<td align="left" width="90%">
<input type="file" id="fileAttachement" runat="server" name="fileAttachement" />
</td>
</tr>
<tr>
<td>
</td>
<td align="left">
<asp:Button ID="btnSendEmail" runat="server" Width="76px" Text="Send Mail"
OnClick="btnSendEmail_Click" />
</td>
</tr>
</table>
</fieldset>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
This is .vb code:-
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
Imports System.Net.Mail
Imports System.IO
-------------------------------------------------------------------------------------------------------
Partial Class _Default
Inherits System.Web.UI.Page
-------------------------------------------------------------------------------------------------------
Private sqlDataAdapter As Data.SqlClient.SqlDataAdapter
Private dataSet As New Data.DataSet()
Private sqlConnection As Data.SqlClient.SqlConnection
Private sqlCommand As New Data.SqlClient.SqlCommand()
Dim FileName As String
-------------------------------------------------------------------------------------------------------
Protected Sub btnSendEmail_Click(ByVal sender As Object, ByVal e As EventArgs)
If txtemailfrom.Text = "" Or txtsubject.Text = "" Or txtbody.Text = "" Then
lblmail.Text = "Please fill all mandatory fields."
lblmail.Visible = True
Return
End If
Dim smtpClient As SmtpClient = New SmtpClient()
Dim mailMessage As MailMessage = New MailMessage()
sqlConnection = New SqlConnection(ConfigurationManager.AppSettings("connect"))
Dim list_emails As ArrayList = New ArrayList()
Dim i As Integer = 0
Dim email As String
sqlConnection.Open()
Dim cmd As SqlCommand = New SqlCommand("Select EmailId from Employee", sqlConnection)
Dim read_Email As SqlDataReader = cmd.ExecuteReader()
While read_Email.Read()
email = read_Email.GetValue(i).ToString()
list_emails.Add(email)
i = i + 1 - 1
End While
read_Email.Close()
sqlConnection.Close()
Dim email_to As String
For Each email_to In list_emails
If Not fileAttachement.PostedFile Is Nothing Then
Dim AttachFile As HttpPostedFile = fileAttachement.PostedFile
Dim AttachFileLength As Integer = AttachFile.ContentLength
If AttachFileLength > 0 Then
FileName = Path.GetFileName(fileAttachement.PostedFile.FileName)
End If
End If
Dim fromAddress As MailAddress = New MailAddress(txtemailfrom.Text)
mailMessage.From = fromAddress
mailMessage.Subject = txtsubject.Text
mailMessage.Body = txtbody.Text
Dim Body As String = ""
Body += "\n" + "From:" + txtemailfrom.Text
Body += "\n" + ""
Body += "\n" + "Subject:" + txtsubject.Text
Body += "\n" + ""
mailMessage.IsBodyHtml = True
mailMessage.Body = Body
smtpClient.Host = "localHost"
smtpClient.Send(mailMessage)
lblmail.Visible = True
lblmail.Text = "Your mail has been sent successfully"
tblemail.Visible = True
Next
txtemailfrom.Text = ""
txtsubject.Text = ""
txtbody.Text = ""
End Sub
End Class
-------------------------------------------------------------------------------------------------------
Output:-