Wednesday, February 17, 2010

Send Mass Email With Image ASP.NET C# sql-server

Send Mass Email With Image using ASP.NET C#  , sql-server database..
 
using
System;

using

System.Data;

using

System.Configuration;

using

System.Net.Mail;

using

System.Net.Mime;

using

System.Collections.Generic;

using

System.Web;

using

System.Web.UI;

using

System.Web.UI.WebControls;

using

System.Data.SqlClient;

 

public

partial class sndmail : System.Web.UI.Page

{

string strSQL;

string txtEmail;

protected void Page_Load(object sender, EventArgs e)

{

//Label1.Style.Add(

HtmlTextWriterStyle.BackgroundImage, "url(images//visliXmasFlyer2009.jpg)");

}

protected void Button3_Click(object sender, EventArgs e)

{

ReadDB();

}

void ReadDB()

{

SqlConnection myConnection = new SqlConnection(ConnectionString);

strSQL =

"select * from mEmailList ";

SqlCommand cmd = new SqlCommand(strSQL, myConnection);

myConnection.Open();

SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);

// lableadid.Text = Request["LeadID"].ToString();

while (myReader.Read())

{

Response.Write(txtEmail);

txtEmail = myReader[

"EmailID"].ToString();

sndaamail();

}

myReader.Close();

myConnection.Close();

}

void sndaamail()

{

string strMailContent = "<b><center><a href=http://visli.com> visli Warehouse Sale Coupon</a></center></b>";

string fromAddress = webmaster@visli.com;

string toAddress = txtEmail.ToString();

string contentId = "image1";

string path = Server.MapPath(@"images/visliXmasFlyer2009.jpg"); // my logo is placed in images folder

MailMessage mailMessage = new MailMessage(fromAddress, toAddress);

//mailMessage.Bcc.Add(shivaliv@visli.com); // put your id here

mailMessage.Subject =

"visli Warehouse Sale Coupon";

LinkedResource logo = new LinkedResource(path);

logo.ContentId =

"companylogo";

// done HTML formatting in the next line to display my logo

AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><a href=http://visli.com><img src=http://visli.com/images/visliXmasFlyer2009.jpg width=570px height=825px></a><br></body></html>" + strMailContent, null, MediaTypeNames.Text.Html);

av1.LinkedResources.Add(logo);

mailMessage.AlternateViews.Add(av1);

mailMessage.IsBodyHtml =

true;

//SmtpClient mailSender = new SmtpClient("10.0.1.32");

SmtpClient mailSender = new SmtpClient("mail.xxxxx.com");

mailSender.Send(mailMessage);

}

private string ConnectionString

{

get

{

string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

return connectionString;

}

}

}

No comments: