Hi Waghmare,
Refer below code.
Namespaces
C#
using System.Data;
using System.IO;
using System.Net;
using System.Net.Mail;
VB.Net
Imports System.Data
Imports System.IO
Imports System.Net
Imports System.Net.Mail
Code
C#
protected void OnSend(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Empid");
dt.Columns.Add("FileName");
dt.Columns.Add("FilePath");
dt.Rows.Add(1, 112233, "Aggrement_112233", "~/Docs/Aggrement_112233.pdf");
string filePath = Server.MapPath(dt.Rows[0]["FilePath"].ToString());
string fileName = Path.GetFileName(filePath);
byte[] bytes = File.ReadAllBytes(filePath);
MailMessage mm = new MailMessage("sender@gmail.com", "receiver@gmail.com");
mm.Subject = "PDF";
mm.Body = "PDF Attachment";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), fileName));
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);
}
VB.Net
Protected Sub OnSend(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.Add("ID")
dt.Columns.Add("Empid")
dt.Columns.Add("FileName")
dt.Columns.Add("FilePath")
dt.Rows.Add(1, 112233, "Aggrement_112233", "~/Docs/Aggrement_112233.pdf")
Dim filePath As String = Server.MapPath(dt.Rows(0)("FilePath").ToString())
Dim fileName As String = Path.GetFileName(filePath)
Dim bytes As Byte() = File.ReadAllBytes(filePath)
Dim mm As MailMessage = New MailMessage("sender@gmail.com", "receiver@gmail.com")
mm.Subject = "PDF"
mm.Body = "PDF Attachment"
mm.Attachments.Add(New Attachment(New MemoryStream(bytes), fileName))
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)
End Sub