How to send order details in a Email in asp.net and c#

Hello Experts,

currently i am sending order confirmation to my customer thru an email. the email is sent when the customer clicks on the confirm button when he/she places the order. its a very simple email which just includes the customer first name and the orderID.

Now i am want to include other details like Billing, Shipping and what products details also.

the current process was setup by developer whose is no longer with us and i am getting confused while trying to make the changes i want to.

I am attaching the current process we are using. can some one please guide me or suggest me how to include the billing, shipping and product information in the current process or

is there a better way of doing this. I open to suggestions.
Thanks appreciate it.

private void sendConfirmationEmail( Order confirmedOrder )
    {
        
        SmtpClient MailRelay = new SmtpClient(
            ConfigurationManager.AppSettings["SmtpRelay"]);
        MailRelay.DeliveryMethod = SmtpDeliveryMethod.Network;
        MailRelay.UseDefaultCredentials = true;

        MailMessage Email = new MailMessage();
        Email.To.Add(confirmedOrder.Email);
        Email.From = new MailAddress("customerservice@mysite.com");
        Email.Subject = string.Format(
            "Mysite.com Confirmation - Order #{0}",
            confirmedOrder.Id);

        Email.Body = string.Format(
            getEmailBody(),
            confirmedOrder.Billing.FirstName,
            confirmedOrder.Id);

     //   Email.IsBodyHtml = false;
        Email.IsBodyHtml = true;

        MailRelay.Send(Email);
        
    }

    /// <summary>
    /// Loads the format string for the catalog request email.  (uses cache)
    /// </summary>
    /// <returns></returns>
    private static string getEmailBody( )
    {
        HttpContext Ctxt = HttpContext.Current;
        string CacheKey = "orderconfirmation_email";
        object CachedEmail = Ctxt.Cache.Get( CacheKey );

        if ( CachedEmail == null )
        {
            string EmailFile = Ctxt.Server.MapPath(
                ConfigurationManager.AppSettings[ "OrderConfirmation" ] );

            StreamReader Reader = new StreamReader( EmailFile );
            string Email = Reader.ReadToEnd( );

            Ctxt.Cache.Insert( 
                CacheKey, 
                Email, 
                new CacheDependency( EmailFile ) );
            return Email;
        }
        else
        {
            return Convert.ToString( CachedEmail );
        }
    }


-------------------------------------------------------------
              ORDERCONFIRMATION.TXT
-------------------------------------------------------------
 	
	Dear {0},

Thank you for shopping at The My Site. We are currently processing your order. We will contact you by email if any of the items you have ordered are on backorder or are no longer available.

Please allow 3-7 business days to receive in-stock items. You will be notified by email if an item is temporarily out of stock. Items ordered together are occasionally shipped separately, however, you are only billed a one-time shipping charge per order. Your credit card is charged for items only when they are shipped.

If you have any questions, please email us at customerservice@MySite.com, or call us at 1(000)000-0000. Please reference your order number: #{1}.

Open in new window

LVL 2
niceoneishereAsked:
Who is Participating?
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Your first port of call should be the definition of the Order class, as you are going to need to check if that classes currently exposes the data you need. If it doesn't then you are either going to have to extend the class so that it does, or pull the data you need from somewhere else.
0
 
niceoneishereAuthor Commented:
Hello Sir,
yes the Order Class as all the information i need. It has Billing, Shipping, and product details. I just want to use this information in the email .

how to do that sir, thats where i am stuck

Thanks for replying and appreciate it
0
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Well, from the look of your code, you are going to need to modify two parts. Firstly you need to modify the text in OrderConfirmation.txt.

If you look at the first line in the text it says "Dear {0}". The "{0}" part is a placeholder, you will need to add place holders for the rest of the text you need to add in. Each one will be a incremental number which will map to the position of the statement in the string.Format() statement:
// You need to extend this statement to contain each member of your Order class that you want
// in the email

        Email.Body = string.Format( 
            getEmailBody(), 
            confirmedOrder.Billing.FirstName,      // this replaces placeholder {0}
            confirmedOrder.Id);                    // this replaces placeholder {1}

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
niceoneishereAuthor Commented:
Ok sir,

i have changed the way you have suggested but i am getting stuck when trying to add the item details to the email. the code for the items is like this

confirmedOrder.Items[0].ItemNumber
confrmedOrder.Items[0].ProductName
confirmedOrder.Item[0].Price
confirmedOrder.Item[0].Size

my question what happens when i have more than one item, do i have to insert a loop so i checks for all the items that are there in the order sir.

i have also modified my orderconfrimation text like below.

can please tell me am i on the right track. or would you suggest a different approach sir

Thanks appreciate it



-------------------------------------------------------------
              ORDERCONFIRMATION.TXT
-------------------------------------------------------------
        
        Dear {0},

Thank you for shopping at The My Site. We are currently processing your order. We will contact you by email if any of the items you have ordered are on backorder or are no longer available.

Please allow 3-7 business days to receive in-stock items. You will be notified by email if an item is temporarily out of stock. Items ordered together are occasionally shipped separately, however, you are only billed a one-time shipping charge per order. Your credit card is charged for items only when they are shipped.

If you have any questions, please email us at customerservice@MySite.com, or call us at 1(000)000-0000. Please reference your order number: #{1}.

Order Details:
Item           Description            Size        Price
--------------------------------------------------------
{14}            {15}                  {16}         {17}

Billing Info:                Shipping Info:
{2} {3}                       {8}{9}
{4}                           {10}
{5}                           {11}
{6} {7}                       {12} {13}

Open in new window

0
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
What you will have to do in that instance is to loop through your order lines and build a string for the output. You would then use a single placeholder for the whole order details table.

Since you are using plain text, that is probably going to involve some fixed size column formatting, plus you need to end each line with "\n" in order to force a line break between each.
0
 
niceoneishereAuthor Commented:
Hello Sir,
i tried adding \n at the end of each line, its not working. and one more thing how will i loop sir can you please guide me in that.

Thanks for your patience and time, i appreciate it,

here is what i have done so far


 Email.Body = string.Format(
            getEmailBody(),
            confirmedOrder.Billing.FirstName, // this replaces placeholder {0}
            confirmedOrder.Id,               // this replaces placeholder {1}
            confirmedOrder.Billing.FirstName,  // this replaces placeholder {2}
            confirmedOrder.Billing.LastName,   // this replaces placeholder {3}
            confirmedOrder.Billing.Address.Line1, // this replaces placeholder {4}
            confirmedOrder.Billing.Address.City,  // this replaces placeholder {5}
            confirmedOrder.Billing.Address.State, // this replaces placeholder {6}
            confirmedOrder.Billing.Address.Zip,  // this replaces placeholder {7}
            confirmedOrder.Shipping.FirstName, // this replaces placeholder {8}
            confirmedOrder.Shipping.LastName,  // this replaces placeholder {9}
            confirmedOrder.Shipping.Address.Line1,  // this replaces placeholder {10}
            confirmedOrder.Shipping.Address.City,  // this replaces placeholder {11}
            confirmedOrder.Shipping.Address.State,  // this replaces placeholder {12}
            confirmedOrder.Shipping.Address.State,  // this replaces placeholder {13}
            confirmedOrder.Items[0].ItemNumber,   // this replaces placeholder {14}
            confirmedOrder.Items[0].ProductTitle, // this replaces placeholder {15}
            confirmedOrder.Items[0].Size,     // this replaces placeholder {16}
            confirmedOrder.Items[0].Price,    // this replaces placeholder {17}
            confirmedOrder.Subtotal,       // this replaces placeholder {18}
            confirmedOrder.Shipping.Cost,  // this replaces placeholder {19}
            confirmedOrder.TotalCost      // this replaces placeholder {20}
            );               


-------------------------------------------------------------
              ORDERCONFIRMATION.TXT
-------------------------------------------------------------
        
        Dear {0},

Thank you for shopping at The My Site. We are currently processing your order. We will contact you by email if any of the items you have ordered are on backorder or are no longer available.

\n
\n

Please allow 3-7 business days to receive in-stock items. You will be notified by email if an item is temporarily out of stock. Items ordered together are occasionally shipped separately, however, you are only billed a one-time shipping charge per order. Your credit card is charged for items only when they are shipped.

\n
\n

If you have any questions, please email us at customerservice@MySite.com, or call us at 1(000)000-0000. Please reference your order number: #{1}.

Order Details: \n \n
Item           Description            Size        Price   \n
--------------------------------------------------------   \n
{14}            {15}                  {16}         {17}   \n

\n

Billing Info:                Shipping Info:
\n

{2} {3} \n                      {8}{9} \n
{4} \n                          {10} \n
{5} \n                          {11} \n
{6} {7} \n                      {12} {13} \n

Open in new window

0
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Based on your code you will need something along the lines of:
confirmedOrder.Items[0].ItemNumber
confrmedOrder.Items[0].ProductName
confirmedOrder.Item[0].Price
confirmedOrder.Item[0].Size

StringBuilder sb = new StringBuilder();
for (int i = 0; i != confirmedOrder.Items.Count; ++i)
{
   sb.AppendLine(
       string.Format(
           "{0}{1}{2}{3}", 
           confirmedOrder.Items[i].ItemNumber,
           confirmedOrder.Items[i].ProductName,
           confirmedOrder.Items[i].Price,
           confirmedOrder.Items[i].Size
       )
   );
}

// you then dump this into you email body using "sb.ToString()"
Email.Body = string.Format( 
            getEmailBody(), 
            confirmedOrder.Billing.FirstName, // this replaces placeholder {0} 
            confirmedOrder.Id,               // this replaces placeholder {1} 
            confirmedOrder.Billing.FirstName,  // this replaces placeholder {2} 
            confirmedOrder.Billing.LastName,   // this replaces placeholder {3} 
            confirmedOrder.Billing.Address.Line1, // this replaces placeholder {4} 
            confirmedOrder.Billing.Address.City,  // this replaces placeholder {5} 
            confirmedOrder.Billing.Address.State, // this replaces placeholder {6} 
            confirmedOrder.Billing.Address.Zip,  // this replaces placeholder {7} 
            confirmedOrder.Shipping.FirstName, // this replaces placeholder {8} 
            confirmedOrder.Shipping.LastName,  // this replaces placeholder {9} 
            confirmedOrder.Shipping.Address.Line1,  // this replaces placeholder {10} 
            confirmedOrder.Shipping.Address.City,  // this replaces placeholder {11} 
            confirmedOrder.Shipping.Address.State,  // this replaces placeholder {12} 
            confirmedOrder.Shipping.Address.State,  // this replaces placeholder {13} 
            confirmedOrder.Items[0].ItemNumber,   // this replaces placeholder {14} 
            confirmedOrder.Items[0].ProductTitle, // this replaces placeholder {15} 
            confirmedOrder.Items[0].Size,     // this replaces placeholder {16} 
            confirmedOrder.Items[0].Price,    // this replaces placeholder {17} 
            confirmedOrder.Subtotal,       // this replaces placeholder {18} 
            confirmedOrder.Shipping.Cost,  // this replaces placeholder {19} 
            confirmedOrder.TotalCost,      // this replaces placeholder {20} 
            sb.ToString()                  // assuming order details is placeholder {21}
            ); 

Open in new window

0
 
niceoneishereAuthor Commented:
Hello Sir,
thank you now i can display all the items in an order. the only thing 
i changed is the below line


for
 
(
int
 i 
=
 
0
;
 i 
!=
 confirmedOrder
.
Items
.
Count
;
 
++
i
)
 
to

for (int i = 0; i != confirmedOrder.Items.Length; ++i)



it threw an error saying Count is not valid for system.array


i really appreciate it,

But the \n is still not working any idea how i can format my txt file sir

Thanks

Open in new window

0
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
Are you sending your email as text or as HTML? If its text thaen try using "\r\n" rather than just "\n". If its HTML then you can put a "<br/>" in there instead.
0
 
niceoneishereAuthor Commented:
Thank you the html did the trick. i appreciate it. i  just have to figure how to create a line break between the items when there are more then one item

right now all of them are diaplaying in one line.

Thanks once again i appreciate it
0
 
Carl TawnSystems and Integration DeveloperCommented:
If you are indeed using Html output then you just need to change:
   sb.AppendLine( 
       string.Format( 
           "{0}{1}{2}{3}",  
           confirmedOrder.Items[i].ItemNumber, 
           confirmedOrder.Items[i].ProductName, 
           confirmedOrder.Items[i].Price, 
           confirmedOrder.Items[i].Size 
       ) 

To:

   sb.AppendLine( 
       string.Format( 
           "{0}{1}{2}{3}<br />",  
           confirmedOrder.Items[i].ItemNumber, 
           confirmedOrder.Items[i].ProductName, 
           confirmedOrder.Items[i].Price, 
           confirmedOrder.Items[i].Size 
       ) 

Open in new window

0
 
niceoneishereAuthor Commented:
Thanks a lot it did fix the problem, i appreciate your help


0
 
niceoneishereAuthor Commented:
Your were Awesome!!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.