.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

select IMAGE(from database)through sending mail in click button in asp.net

Posted By: kavitha     Posted Date: November 28, 2010    Points:2   Category :ASP.Net
select IMAGE(from database)through sending mail in click button

hi friends,

how to create select IMAGE(from database)through sending mail in click button in asp.net.

thanks.

Responses
Author: SureshM             
Posted Date: November 29, 2010     Points: 5   

Hi,

I am not clear about your requirement.

Please confirm the below.

1.you need to select(retrieve) an image from DB
2.then you need to send a mail to somebody, the email content is the retrieved image.



Author: SureshM             
Posted Date: November 29, 2010     Points: 5   

Put the code into the click event of the button.

you have to store the retrieved image into your project folder.
you can save the image using bitmap object.

string strImageurl="Path of your image";
///Create the content with your image.
string strBody = "<table border=\"1\" bordercolor=\"black\" cellspadding=\"0\">" +
"<tr><td colspan=\"4\"><b>Header of your mail content</b></td></tr>"+
"<tr><td colspan=\"4\"><img scr=\"+strImageurl+""\"+"</td></tr></table>";

////subject
string strSubject = "Enter Your subject";


// enter your from and to address

/// This is not working in your local, if u host in live only it ll work,
System.Net.Mail.MailMessage MailMsg = new System.Net.Mail.MailMessage("from address", "To address", strSubject, strBody);

System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
MailMsg.IsBodyHtml = true;
client.Send(MailMsg);



Author: kavitha             
Posted Date: November 30, 2010     Points: 5   

thanks for your response.

I need,

1.to select(retrieve) an image from DB and details
2.then to send a mail to somebody, the email content is the retrieved image.

please give in detail.


thanks.

Author: SureshM             
Posted Date: December 01, 2010     Points: 5   


using System.IO;
using System.Drawing;
using System.Drawing.Imaging;



protected void imgbtn_Click(object sender, EventArgs e)
{
try
{
string strconn = "Data Source=RMNW011\\SQL2008; Initial Catalog=TEST_DB; Integrated Security =True;";

FileInfo fInfo = new FileInfo(Server.MapPath(imgbtn.ImageUrl.ToString()));

FileStream fs = new FileStream(Server.MapPath(imgbtn.ImageUrl.ToString()), FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] imgbytes = br.ReadBytes((int)fInfo.Length);

SqlConnection objcon = new SqlConnection();
objcon.ConnectionString = strconn;
objcon.Open();
SqlCommand objcmd = new SqlCommand();



objcmd.Connection = objcon;
objcmd.CommandText = "INSERT INTO Products (ProductName,productImage) Values(@name,@img)";// ('" + "suresh" + "','" + imgbytes + "')";

SqlParameter objpara = new SqlParameter("@img", (object)imgbytes);
objcmd.Parameters.Add(objpara);

SqlParameter objpara1 = new SqlParameter("@name", "Suresh");
objcmd.Parameters.Add(objpara1);

objcmd.ExecuteNonQuery();

objcmd = new SqlCommand();
objcmd.CommandText = " SELECT * FROM PRODUCTS ";
objcmd.Connection = objcon;

//objcmd = new SqlCommand();
//objcmd.CommandType = CommandType.TableDirect;
//objcmd.CommandText = "Products";

SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = objcmd;

DataSet dst = new DataSet();
sda.Fill(dst);

byte[] imgbyte = (byte[])dst.Tables[0].Rows[0]["ProductImage"];
System.Drawing.Image img;
using (MemoryStream ms = new MemoryStream(imgbyte, 0, imgbyte.Length))
{
ms.Write(imgbyte, 0, imgbyte.Length);
img = System.Drawing.Image.FromStream(ms, true);
}
imgbmp = new Bitmap(img, 100, 100);
imgbmp.Save(Server.MapPath("~/TEST.jpg"), ImageFormat.Jpeg);

string strImageurl = "/Test.jpg";

string strBody = "<table border=\"1\" bordercolor=\"black\" cellspadding=\"0\">" +
"<tr><td colspan=\"4\"><b>Header of your mail content</b></td></tr>" +
"<tr><td colspan=\"4\"><img src=" + "\"" + strImageurl + "\"" + "</td></tr></table>";
SendMailToBorrower("", strBody, "Image Mail");


}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}

private void SendMailToBorrower(string strTo, string strbody, string strsubject)
{
System.Web.Mail.MailMessage MailMsg = new System.Web.Mail.MailMessage();
string strUserName = "msures4@gmail.com";
string strPassword = "";
string strHost = "smtp.gmail.com";
string strPort = "465";

MailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
//MailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;
MailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = strUserName;
MailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = strPassword;
MailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = Convert.ToInt32(strPort);
MailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "True";

MailMsg.To = "suresh.m@carotechs.com";
MailMsg.From = strUserName;
MailMsg.Subject = strsubject;
MailMsg.Body = strbody;
MailMsg.BodyFormat = System.Web.Mail.MailFormat.Html;

System.Web.Mail.SmtpMail.SmtpServer = strHost;
System.Web.Mail.SmtpMail.Send(MailMsg);

}

you may need to change the code for send mail as the below.



System.Net.Mail.MailMessage MailMsg = new System.Net.Mail.MailMessage("from address", "To address", strSubject, strBody);

System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
MailMsg.IsBodyHtml = true;
client.Send(MailMsg);


Author: kavitha             
Posted Date: December 01, 2010     Points: 5   

thanks to your effort,

but i need ,

images and details from database send through mail.

thanks.



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend