Hi SUJAYS,
Using the below articles i have created the sample example.
In the example i have used DataTable to get the data and export to excel and then attach the excel to email.
For multiple users i have used DataTable to save the user email and using Parallel Multi-Threading send multiple bulk email.
So you need to modify as per your logic in your code.
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using System.Data;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
using System.Windows.Forms;
using ClosedXML.Excel;
VB.Net
Imports System.Data
Imports System.IO
Imports System.Net
Imports System.Net.Mail
Imports System.Threading.Tasks
Imports System.Windows.Forms
Imports ClosedXML.Excel
Code
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSendEmail_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
dt.TableName = "Customers_Data";
//Create a New Workbook.
using (XLWorkbook wb = new XLWorkbook())
{
//Add the DataTable as Excel Worksheet.
wb.Worksheets.Add(dt);
using (MemoryStream memoryStream = new MemoryStream())
{
//Save the Excel Workbook to MemoryStream.
wb.SaveAs(memoryStream);
//Convert MemoryStream to Byte array.
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
//Create a temporary DataTable for user Emails.
DataTable dtEmails = new DataTable();
dtEmails.Columns.AddRange(new DataColumn[] {
new DataColumn("Name", typeof(string)),
new DataColumn("Email",typeof(string)) });
dtEmails.Rows.Add("John Hammond", "john.hammond@test.com");
dtEmails.Rows.Add("Mudassar Khan", "mudassar.khan@test.com");
dtEmails.Rows.Add("Suzanne Mathews", "suzzane.mathews@test.com");
dtEmails.Rows.Add("Robert Schidner", "robert.schidner@test.com");
string subject = "Welcome Email";
string body = "Hello {0},<br /><br />Welcome to ASPSnippets<br /><br />Thanks.";
//Using Parallel Multi-Threading send multiple bulk email.
Parallel.ForEach(dtEmails.AsEnumerable(), row =>
{
//Send Email with Excel attachment.
SendEmail(row["Email"].ToString(), subject, string.Format(body, row["Name"]), bytes);
});
}
}
}
private bool SendEmail(string recipient, string subject, string body, byte[] bytes)
{
MailMessage mm = new MailMessage("sender@gmail.com", recipient);
mm.Subject = subject;
mm.Body = body;
//Add Byte array as Attachment.
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "Customers.xlsx"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "<password>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
return true;
}
}
VB.Net
Public Class Form1
Private Sub btnSendEmail_Click(sender As System.Object, e As System.EventArgs) Handles btnSendEmail.Click
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
dt.TableName = "Customers_Data"
'Create a New Workbook.
Using wb As XLWorkbook = New XLWorkbook()
'Add the DataTable as Excel Worksheet.
wb.Worksheets.Add(dt)
Using memoryStream As MemoryStream = New MemoryStream()
'Save the Excel Workbook to MemoryStream.
wb.SaveAs(memoryStream)
'Convert MemoryStream to Byte array.
Dim bytes As Byte() = memoryStream.ToArray()
memoryStream.Close()
'Create a temporary DataTable for user Emails.
Dim dtEmails As DataTable = New DataTable()
dtEmails.Columns.AddRange(New DataColumn() {New DataColumn("Name", GetType(String)), New DataColumn("Email", GetType(String))})
dtEmails.Rows.Add("John Hammond", "john.hammond@test.com")
dtEmails.Rows.Add("Mudassar Khan", "mudassar.khan@test.com")
dtEmails.Rows.Add("Suzanne Mathews", "suzzane.mathews@test.com")
dtEmails.Rows.Add("Robert Schidner", "robert.schidner@test.com")
Dim subject As String = "Welcome Email"
Dim body As String = "Hello {0},<br /><br />Welcome to ASPSnippets<br /><br />Thanks."
'Using Parallel Multi-Threading send multiple bulk email.
Parallel.ForEach(dtEmails.AsEnumerable(), _
Function(row)
'Send Email with Excel attachment.
Return SendEmail(row("Email").ToString(), subject, String.Format(body, row("Name")), bytes)
End Function)
End Using
End Using
End Sub
Private Function SendEmail(ByVal recipient As String, ByVal subject As String, ByVal body As String, ByVal bytes As Byte()) As Boolean
Dim mm As MailMessage = New MailMessage("sender@gmail.com", recipient)
mm.Subject = subject
mm.Body = body
'Add Byte array as Attachment.
mm.Attachments.Add(New Attachment(New MemoryStream(bytes), "Customers.xlsx"))
mm.IsBodyHtml = True
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As NetworkCredential = New NetworkCredential()
NetworkCred.UserName = "sender@gmail.com"
NetworkCred.Password = "<password>"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mm)
Return True
End Function
End Class