不稳定在asp.net中发送电子邮件

时间:2011-07-11 16:09:01

标签: c# asp.net

我正在建立一个电子商务网站,购买完成后,我想先向买家发送电子邮件,然后再提交数据库更改,但不知何故,发送失败率约为25-30%。我使用hotmail current作为临时电子邮件帐户,不确定它是否是hotmail的问题,无论如何这是我的代码,任何建议?感谢:

代码:

 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</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);
}

1 个答案:

答案 0 :(得分:4)

如果您在后端使用SQL Server,则可以设置数据库服务器以处理邮件请求。使用SQL Server而不是ASP.NET代码的优点是可以将数据库配置为在失败时多次重试发送消息。

以下是有关如何配置数据库邮件的良好资源:http://blog.sqlauthority.com/2008/08/23/sql-server-2008-configure-database-mail-send-email-from-sql-database/