943,650 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 2453
  • ASP.NET RSS
Apr 11th, 2009
0

populate email with data from database

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
julseypart is offline Offline
42 posts
since Oct 2008
Apr 11th, 2009
1

Re: populate email with data from database

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.
Reputation Points: 26
Solved Threads: 44
Posting Whiz in Training
mail2saion is offline Offline
247 posts
since Apr 2009
Apr 12th, 2009
0

Re: populate email with data from database

thanks for the advice, i will try it out!
Reputation Points: 10
Solved Threads: 0
Light Poster
julseypart is offline Offline
42 posts
since Oct 2008
Apr 12th, 2009
0

Re: populate email with data from database

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
julseypart is offline Offline
42 posts
since Oct 2008
Apr 12th, 2009
0

Re: populate email with data from database

Hi,
TRY WITH BELOW CODE. THIS IS AN EXAMPLE:
c# Syntax (Toggle Plain Text)
  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:
c# Syntax (Toggle Plain Text)
  1. using System.Text;

Happy programming............
Reputation Points: 26
Solved Threads: 44
Posting Whiz in Training
mail2saion is offline Offline
247 posts
since Apr 2009
Apr 12th, 2009
0

Re: populate email with data from database

thanks very much - your a genious!!!!
Last edited by julseypart; Apr 12th, 2009 at 5:12 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
julseypart is offline Offline
42 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: text box validataion
Next Thread in ASP.NET Forum Timeline: Error: Your Login attempt was not succesful. please try again





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC