2011-07-11 15 views
0

私は電子商取引サイトを構築しており、購入後に購入者に電子メールを送信してからデータベースの変更をコミットする前に、何らかの理由で送信失敗率が約25 - 30%私はホットメールの問題があるかどうかわからない、一時的な電子メールアカウントとして現在のホットメールを使用していますが、とにかくこれは私のコード、アドバイスですか?ありがとう:asp.netでの電子メールの送信不安定

コード:

MembershipUser u = Membership.GetUser(HttpContext.Current.User.Identity.Name); 
     AccountProfile usercustomProfile = new AccountProfile(); 
     var p = usercustomProfile.GetProfile(u.UserName); 

     MailMessage mail = new MailMessage(); 
     mail.To.Add(u.Email); 
     mail.IsBodyHtml = true; 
     mail.From = new MailAddress("[email protected]"); 
     mail.Subject = ("Purchase invoice" + ' ' + newOrder.OrderID); 

     string mailBodyHeader = 
     "<table border=0> <tr> <td>Product ID</td><td>Model Number</td><td>Model Name</td><td> Unit Cost</td> <td>Quantity</td><td>Price</td></tr>"; 

     System.Text.StringBuilder bodyContent = new System.Text.StringBuilder(); 

     double unitQtyPrice = 0; 
     double totalPrice = 0; 
     foreach (var cItem in cartList) 
     { 
      unitQtyPrice = cItem.Quantity * (double)cItem.UnitCost; 
      totalPrice += unitQtyPrice; 

      bodyContent.Append("<tr>"); 

      bodyContent.Append("<td>"); 
      bodyContent.Append(cItem.ProductID.ToString()); 
      bodyContent.Append("</td>"); 

      bodyContent.Append("<td>"); 
      bodyContent.Append(cItem.ModelNumber.ToString()); 
      bodyContent.Append("</td>"); 

      bodyContent.Append("<td>"); 
      bodyContent.Append(cItem.ModelName); 
      bodyContent.Append("</td>"); 

      bodyContent.Append("<td>"); 
      bodyContent.Append(Math.Round(cItem.UnitCost, 2)); 
      bodyContent.Append("</td>"); 

      bodyContent.Append("<td>"); 
      bodyContent.Append(cItem.Quantity.ToString()); 
      bodyContent.Append("</td>"); 

      bodyContent.Append("<td>"); 
      bodyContent.Append("$" + Math.Round(unitQtyPrice, 2)); 
      bodyContent.Append("</td>"); 

      bodyContent.Append("</tr>"); 

     } 

     Math.Round(totalPrice, 2); 

     mail.Body = "Thanks you for shopping with XXX. Your purchase details are as follow:" 
     + "<br><br>" + "Name:" + p.FirstName + p.LastName 
     + "<br>" + "Mailing Address:" + p.MailingAddress 
     + "<br>" + "Billing Address:" + p.BillingAddress 
     + "<br>" + "Contact No.:" + p.Contact 
     + "<br><br>" + mailBodyHeader + bodyContent.ToString() + "</table>" 
     + "<br>" + "Total Price:" + "$" + totalPrice 
     + "<br>" + "Additional/Special instructions:" 
     + "<br>" + SInfo 
     + "<br><br>" + "Please blah blah blah"; 

     SmtpClient client = new SmtpClient("smtp.live.com", 587); 
     client.EnableSsl = true; //ssl must be enabled for Gmail       
     NetworkCredential credentials = new NetworkCredential("[email protected]", "ABCDE"); 
     client.Credentials = credentials; 

     //Sends a message to from if email is not deliverable 
     mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; 
     mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; 

     //Create the SMTPClient object and DO NOT specify the SMTP server name, it’s being pulled from config file 
     SmtpClient SMTPServer = new SmtpClient(); 
     SMTPServer.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis; 

     try 
     { 
      client.Send(mail); 
      db.SaveChanges(); 
     } 
     catch (SmtpException) 
     { 
      Server.Transfer("/CheckOutUnsuccessful.aspx", true); 
     } 
    } 
    return (true); 
} 
+1

あなたが取得しているエラーは何ですか? – temarsden

答えて

4

あなたがバックエンドでSQL Serverを使用している場合は、メール要求を処理するために、データベース・サーバをセットアップすることができます。 ASP.NETコードとは対照的にSQL Serverを使用する利点は、失敗したときにメッセージを何度も送信するようにデータベースを再設定することができることです。ここで

は、データベースメールを設定する方法の良いリソースです:http://blog.sqlauthority.com/2008/08/23/sql-server-2008-configure-database-mail-send-email-from-sql-database/

+0

ありがとう、これをチェックします。 – k80sg

関連する問題