?
Solved

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

Posted on 2009-12-18
13
Medium Priority
?
314 Views
Last Modified: 2013-11-26
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

0
Comment
Question by:niceoneishere
  • 7
  • 6
13 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 26080475
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
 
LVL 2

Author Comment

by:niceoneishere
ID: 26080505
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
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 2000 total points
ID: 26080562
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
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
LVL 2

Author Comment

by:niceoneishere
ID: 26080724
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
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 2000 total points
ID: 26080779
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
 
LVL 2

Author Comment

by:niceoneishere
ID: 26081019
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
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 2000 total points
ID: 26081137
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
 
LVL 2

Author Comment

by:niceoneishere
ID: 26081580
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
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 2000 total points
ID: 26081631
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
 
LVL 2

Author Comment

by:niceoneishere
ID: 26082472
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 26082489
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
 
LVL 2

Author Comment

by:niceoneishere
ID: 26097089
Thanks a lot it did fix the problem, i appreciate your help


0
 
LVL 2

Author Closing Comment

by:niceoneishere
ID: 31667748
Your were Awesome!!!!!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question