Thursday, June 18, 2009

Newsletter sample template using access database , C# and smtp

Newsletter format using access database , C# and smtp

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="nl1.aspx.cs" Inherits="nl1" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%; " id="maintabl" runat="server">
<tr>
<td colspan="1" align="center" >



&nbsp; &nbsp; &nbsp;
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send" /></td>
</tr>
<tr>
<td valign="top">
<asp:Label ID="Label1" runat="server" Text="Label" BackColor="Transparent" Width="100%"></asp:Label><br />

<br />
<br />
</td>
</tr>
<tr>
<td colspan="1" style="height: 20px" align="center" >
<asp:Label ID="labmsg" runat="server"></asp:Label></td>
</tr>
</table>
&nbsp;
&nbsp;&nbsp;
<br />
<br />
<br />
<br />
</div>
</form>
</body>
</html>

Code Behind......

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Web.Mail;

public partial class nl1 : System.Web.UI.Page
{
string dynTable = "";
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source=c:\\xxxx\\database\\MailDB.mdb";
OleDbConnection myOleDbConnection = new OleDbConnection(connectionString);
OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand();
string ProdTy = "M2";
string Strsql = "SELECT top 5 * from DetailURLMisc ";
myOleDbCommand.CommandText = Strsql;
myOleDbConnection.Open();
OleDbDataReader myOleDbDataReader = myOleDbCommand.ExecuteReader();
myOleDbDataReader.Read();

dynTable = "<table align=\"center\" width=\"95%\" cellspacing=\"0\" cellpadding=\"3\" border=\"1\">";
dynTable += "<tr><td height=\"50px\" style=\"background-color:Blue\" colspan=\"3\">"
+"<a href=\"default.aspx\" style=\"text-decoration: none\"><font class=\"X-small\""
+"color=\"gold\" size=\"6\"><font color=\"red\" face=\"Monotype Corsiva\">Header</font></font></a>"
+ "</td></tr>";
dynTable += "<tr><td height=\"25px\" style=\"background-color:Silver\" colspan=\"3\">Header2</td></tr>";
dynTable += "<tr>";
dynTable += "<td width=\"20%\">&nbsp;1</td>";
dynTable += "<td width=\"50%\" valign=\"top\">President Barack Obama's plan to transform the Federal Reserve into a super-regulator ran into skepticism Thursday from lawmakers who worry that the central bank is not the best suited to keep an eye on firms deemed so big and influential that their demise could hurt the economy.</td>";
dynTable += "<td width=\"30%\">";
while (myOleDbDataReader.Read())
{

//dynTable += "<td>"+(myOleDbDataReader["TextUrl"].ToString())+"</td>";

dynTable += "<a href=" + myOleDbDataReader["NameUrl"].ToString() + "><img border=0 src=" + (myOleDbDataReader["ImageUrl"].ToString()) + "xxx></img></a>"
+ "<a href=" + myOleDbDataReader["NameUrl"].ToString() + ">" + (myOleDbDataReader["TextUrl"].ToString()) + "</a><br/>";

}
dynTable += "</td></tr>";
dynTable += "<tr><td height=\"25px\" style=\"background-color:Silver\" colspan=\"3\">&nbsp;Footer</td></tr>";
dynTable += "</table>";
Label1.Text = dynTable.ToString();

}

void sndmail()
{
MailMessage oMessage = new MailMessage();
oMessage.To = "xxxxxx@gmail.com";
oMessage.From = "vvvvv@xxxxxx.net";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
oMessage.Subject = "Great Find";
oMessage.Body = dynTable;
oMessage.BodyFormat = MailFormat.Html;
//oMessage.Attachments = txtBody.Text;
//Response.Write("http://www.visli.com/" + System.IO.Path.GetFileName(Request.Url.ToString()));
oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = "mail.xxxxx.net";
oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = "kkkkk@xxxxxx.net";
//oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = "xxxxx";
oMessage.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
System.Web.Mail.SmtpMail.SmtpServer = "mail.xxxxxx.net";//
try
{
oMessage = oMessage;
System.Web.Mail.SmtpMail.Send(oMessage);
labmsg.Text = "Message Send";
}
catch (Exception Ex)
{
labmsg.Text = ("Unable to send mail! " + Ex.Message);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//Label2.Text = maintabl;
sndmail();
}
}

No comments: