开发者

Unstable Sending of email in asp.net

I am building a ecommerce site and after a purchase is done, I want to send the buyer an email first before I commit my database changes but somehow, the send failure rate is about 25 - 30 percent. I am using hotmail current as a temp email account, not sure if it's hotmail's issue, anyway this is my code, any advice? Thanks:

Code:

 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("XXX@hotmail.com");
        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&开发者_如何学Clt;/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("XXX@hotmail.com", "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);
}


If you are using SQL Server on the backend you can set the database server up to handle the mail requests. The advantage of using SQL Server as opposed to ASP.NET code is the database can be configured to retry sending the messages several times on failure.

Here is a good resource on how to configure Database Mail: http://blog.sqlauthority.com/2008/08/23/sql-server-2008-configure-database-mail-send-email-from-sql-database/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜