How To Send PDf Attachment In Web Page Mail


Step 1 Make a New Web Site and Create A Web Page and Do Coding as below given.

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using Microsoft.CSharp;
using System.Configuration;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
private SqlCommand _cmd;
private StringBuilder _strMailMessage;
private MailMessage _objMailMessage;
private MailAddress _objMailAddress;
private NetworkCredential _objMailCredentials;
private SmtpClient _objSmtpClient;
private string _strMailSubject;
private string[] _strVenderEmailAddressAfterSplit;
private char[] _chrSplitter = { ‘;’ };
private int _intFlag;
private SqlParameter[] _param;
private DataSet _dsTempData;
public static void WriteError(string errorMessage)
{
try
{
string path = “Error/” + DateTime.Today.ToString(“dd-MMM-yy”) + “.txt”;
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
{
File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
}
using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
{
w.WriteLine(“\r\nLog Entry : “);
w.WriteLine(“{0}”, DateTime.Now.ToString(CultureInfo.InvariantCulture));
string err = “Error in: ” + System.Web.HttpContext.Current.Request.Url.ToString() +
“. Error Message:” + errorMessage;
w.WriteLine(err);
w.WriteLine(“__________________________”);
w.Flush();
w.Close();
}
}
catch (Exception ex)
{
WriteError(ex.Message);
}

}

protected void Page_Load(object sender, EventArgs e)
{

}

private void Mailer()
{
try
{

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“cn”].ConnectionString))
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}

_param = new SqlParameter[1];
_param[0] = new SqlParameter(“@strEmail”, SqlDbType.VarChar, 1000);
_param[0].Direction = ParameterDirection.Output;
SqlHelper.ExecuteScalar(con, CommandType.StoredProcedure, “usp_GetEmails”, _param);
String strMails = Convert.ToString(_param[0].Value);
if (String.IsNullOrEmpty(strMails))
{

}
else
{
_strMailMessage = new StringBuilder();
_strMailMessage.Append(“<table border=’0′ style=’width: 100%;’ cellpadding=’0′ cellspacing=’0′>”);
_strMailMessage.Append(“<tr>”);
_strMailMessage.Append(“<td style=’width: 100%;’>”);
_strMailMessage.Append(“<p style=’margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt’>”);
_strMailMessage.Append(“<span>Dear Shareholder,</span></p>”);
_strMailMessage.Append(“<p style=’margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt’></p>”);
_strMailMessage.Append(“<p style=’margin-bottom: 5.0pt; text-align: justify; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt’>”);
_strMailMessage.Append(“<span>This is in continuation of our earlier e-mail, we are attaching herewith Annual Report of the Company for the year 2010-11.</span></p>”);
_strMailMessage.Append(“<p style=’margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt’></p>”);
_strMailMessage.Append(“<p style=’margin-bottom: 0in; margin-bottom: .0001pt; text-align: justify; line-height: normal;tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt’>”);
_strMailMessage.Append(“<span>The above said Annual Report has also been uploaded on company's website i.e.<a href=’http://www.owmnahar.in&#8217; target=’_blank’>www.owmnahar.in</a>.</span></p>”);
_strMailMessage.Append(“<p style=’margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt’></p>”);
_strMailMessage.Append(“<p style=’margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt’></p>”);
_strMailMessage.Append(“<p style=’margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt’>”);
_strMailMessage.Append(“<span>With Best Regards,</span></p>”);
_strMailMessage.Append(“<p style=’margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt’>”);
_strMailMessage.Append(“<span>For NAHAR INDUSTRIAL ENTERPRISES LIMITED</span></p>”);
_strMailMessage.Append(“<p style=’margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt’>”);
_strMailMessage.Append(“</p>”);
_strMailMessage.Append(“<p style=’margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt’>”);
_strMailMessage.Append(“</p>”);
_strMailMessage.Append(“<p style=’margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal’>”);
_strMailMessage.Append(“</p>”);
_strMailMessage.Append(“<p style=’margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt’>”);
_strMailMessage.Append(“<span>(MUKESH SOOD)</span></p>”);
_strMailMessage.Append(“<p style=’margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt’>”);
_strMailMessage.Append(“<span>COMPANY SECRETARY</span></p>”);
_strMailMessage.Append(“</td></tr></table>”);
_objMailMessage = new MailMessage();
_strMailSubject = “NAHAR INDUSTRIAL ANNUAL REPORT 2011”;
_strVenderEmailAddressAfterSplit = strMails.Split(‘;’);
for (_intFlag = 0; _intFlag < _strVenderEmailAddressAfterSplit.Length – 1; _intFlag++)
{
String tomail = _strVenderEmailAddressAfterSplit[_intFlag];
_objMailMessage = new MailMessage(Convert.ToString(ConfigurationManager.AppSettings[“user”]), tomail, _strMailSubject, _strMailMessage.ToString());
Attachment at = new Attachment(HttpContext.Current.Server.MapPath(“NIEL10-1116-08-11.pdf”));
_objMailMessage.Attachments.Add(at);
_objMailMessage.Priority = MailPriority.High;
_objMailMessage.IsBodyHtml = true;
_objSmtpClient = new SmtpClient();
//_objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
_objSmtpClient.Send(_objMailMessage);
WriteError(_objMailMessage.To.ToString() + DateTime.Now);
}

}
}

}
catch (Exception ee)
{
WriteError(ee.Message + DateTime.Now);
}
}

protected void Timer1_Tick(object sender, EventArgs e)
{
Mailer();
}
}

Step 2: Now Place Script Manager and Timer on Your Page

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<asp:ScriptManager ID=”ScriptManager1″ runat=”server”>
</asp:ScriptManager>
<asp:Timer ID=”Timer1″ runat=”server” Interval=”10000″ ontick=”Timer1_Tick”>
</asp:Timer>
</form>
</body>
</html>
Step 3: Remember About Wec Config Settings

<?xml version=”1.0″?>
<!–
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
–>
<configuration>
<system.web>
<compilation debug=”true” targetFramework=”4.0″/>
</system.web>
<appSettings>
<add key=”smtpClient” value=”127.0.0.1″/>
<add key=”pass” value=”321″/>
<add key=”user” value=”abc@gmail.com”/>
<add key=”port” value=”25″/>
</appSettings>
<connectionStrings>
<add name=”cn” connectionString=”Data Source=databasename;Initial Catalog=dbMailingApplication;Persist Security Info=True;User ID=sa;Password=abc”
providerName=”System.Data.SqlClient” />

</connectionStrings>
<system.net>
<mailSettings>
<smtp from=”gmail.com”>
<network host=”host” port=”25″ userName=”abc” password=”321″/>
</smtp>
</mailSettings>
</system.net>
</configuration>