sending mail with attachment(File which i want to attach is stored in database)

Posted by Sandeep418995 under ASP.NET on 8/14/2012 | Points: 10 | Views : 1588 | Status : [Member] | Replies : 4
hello sir


i have a requirement in which i have to attach two files from database directly into mail attachment
please tell me how can i do it

please have a look on commented line

thanx in advance.

MailMessage mm = new MailMessage();
SmtpClient smtp = new SmtpClient();
mm.From = new MailAddress("sandybmas11@gmail.com");
mm.To.Add(new MailAddress("sandybmas11@gmail.com"));
mm.Subject = "Your Marksheet";
mm.Body = "hi";

// Attachment attachFile = new Attachment(abc.ToString());
// mm.Attachments.Add(attachFile);


mm.IsBodyHtml = true;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = " ";
NetworkCred.Password = " ";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);




Responses

Posted by: Ranjeet_8 on: 8/14/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Hi..
Refer this article...
you will also find sample file in this url
http://www.dotnetfunda.com/articles/article1106-sending-email-with-attachment-in-aspnet.aspx
.

Sandeep418995, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandeep418995 on: 8/14/2012 [Member] Starter | Points: 25

Up
0
Down
hello Ranjeet_8


please read the question carefully.

" i have to attach two files which are stored in database directly into mail attachment"

Sandeep418995, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 8/14/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
@Sandeep418995
you can use for loop to attach more than one file.
Please attach one by one file location using
mm.Attachments.Add(attachFile);




Sandeep418995, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sandeep418995 on: 8/16/2012 [Member] Starter | Points: 25

Up
0
Down
please see my code i did exactly what you are saying.
here my problem is to give path of the file which is stored in database


string StdId = context.Request.QueryString["autoId"];
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT StudentImage from MarksCardInfo where MarksCardInfo.StudentId='" + StdId + "'", conn);
conn.Open();
// MarksCardImage,StudentImage

SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
reader.Read();
context.Response.BinaryWrite((Byte[])reader[reader.GetOrdinal("StudentImage")]);

byte[] abc = (Byte[])reader[reader.GetOrdinal("StudentImage")]; ;
reader.Close();



here i want to attach file abc .i am doing it like.....// Attachment attachFile = new Attachment(abc);
but its not working.....

Sandeep418995, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response