populate email with data from database

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 41
Reputation: julseypart is an unknown quantity at this point 
Solved Threads: 0
julseypart julseypart is offline Offline
Light Poster

populate email with data from database

 
0
  #1
Apr 11th, 2009
Hi, i would like to programatically send the user an email of all the files they have backed up + associated file information. My question is how can i send the body in a formated way

for example

To: userEmail()
subject: File Backup Report
Body: All users files here e.g. fileName, FileType, fileSize

I can connect to the database and execute a query and set result to a string variable for the body but i would like to display the body (query results) in a neat way, aligned and ordered if possible?

Thanks for the help
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 220
Reputation: mail2saion is an unknown quantity at this point 
Solved Threads: 33
mail2saion's Avatar
mail2saion mail2saion is offline Offline
Posting Whiz in Training

Re: populate email with data from database

 
1
  #2
Apr 11th, 2009
MailMessage msgMail = new MailMessage();

msgMail.To = ".....";
msgMail.Cc = ".....";
msgMail.From = ".....";
msgMail.Subject = ".........";

msgMail.BodyFormat = MailFormat.Html;string strBody = "<html><body><b>Hello World</b>" +
   " <font color=\"red\">ASP.NET</font></body></html>";
msgMail.Body = strBody;

SmtpMail.Send(msgMail);

Most probably you missed red marked line.

TO READ MORE YOU CAN VISIT:
http://www.4guysfromrolla.com/articles/080206-1.aspx
Last edited by peter_budo; Apr 12th, 2009 at 8:38 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 41
Reputation: julseypart is an unknown quantity at this point 
Solved Threads: 0
julseypart julseypart is offline Offline
Light Poster

Re: populate email with data from database

 
0
  #3
Apr 12th, 2009
thanks for the advice, i will try it out!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 41
Reputation: julseypart is an unknown quantity at this point 
Solved Threads: 0
julseypart julseypart is offline Offline
Light Poster

Re: populate email with data from database

 
0
  #4
Apr 12th, 2009
hi, im still having problems in how to pass the results of the sql command to the email body, heres what i have so far

SqlCommand emailCommand = new SqlCommand("SELECT fileName, fileType, fileSize, fileLocation, fileDescription, Compressed, compressedSize FROM aspnet_Files WHERE fileOwner = '" + User.Identity.Name + "'", myConnection);

            string userEmail = userEmailAddress.ExecuteScalar().ToString();
            string backupReport = emailCommand.ExecuteNonQuery().ToString();
            
            MailMessage m = new MailMessage();
            m.To.Add(userEmail);
            m.From = new MailAddress("postmaster@domain.net");
            m.Subject  = "Your Personal Backup Report";
            m.Body     = backupReport;
            m.Priority = MailPriority.High;

            SmtpClient client = new SmtpClient();
            
         //   try{

             client.Send(m);
             btnEmailReport.Text = "Report Sent Successfully!";
             btnEmailReport.Font.Bold = true;
             btnEmailReport.ForeColor = System.Drawing.Color.Green;

string backupReport = emailCommand.ExecuteNonQuery().ToString();
- this line is where the problem lies (only returns one column of one row), how do i return the full results as a string to put in the mail body?

Thanks
Last edited by julseypart; Apr 12th, 2009 at 1:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 220
Reputation: mail2saion is an unknown quantity at this point 
Solved Threads: 33
mail2saion's Avatar
mail2saion mail2saion is offline Offline
Posting Whiz in Training

Re: populate email with data from database

 
0
  #5
Apr 12th, 2009
Hi,
TRY WITH BELOW CODE. THIS IS AN EXAMPLE:
  1. StringBuilder backupReport = new StringBuilder();
  2. string connectionString = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
  3. SqlConnection conn = new SqlConnection(connectionString);
  4. SqlDataAdapter ad = new SqlDataAdapter("SELECT * from tblSupplier", conn);
  5. DataTable ds = new DataTable();
  6. ad.Fill(ds);
  7. backupReport.Append("<table border='1'><tr><td>Code</td><td>Name</td></tr>");
  8. foreach (DataRow oRow in ds.Rows)
  9. backupReport.Append("<tr><td>"+oRow["Code"]+"</td><td>"+oRow["Name"]+"</td></tr>");
  10. backupReport.Append("</table>");
  11. Response.Write(backupReport.ToString());

Dont forget to add the below directive:
  1. using System.Text;

Happy programming............
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 41
Reputation: julseypart is an unknown quantity at this point 
Solved Threads: 0
julseypart julseypart is offline Offline
Light Poster

Re: populate email with data from database

 
0
  #6
Apr 12th, 2009
thanks very much - your a genious!!!!
Last edited by julseypart; Apr 12th, 2009 at 5:12 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC