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

本文关键字:电子邮件 不稳定 asp net | 更新日期: 2023-09-27 18:01:50

我正在建立一个电子商务网站,购买完成后,我想先给买家发一封电子邮件,然后再提交数据库更改,但不知何故,发送失败率约为25 - 30%。我正在使用hotmail当前作为临时电子邮件帐户,不确定这是否是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);
}

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

如果你在后端使用SQL Server,你可以设置数据库服务器来处理邮件请求。与ASP相比,使用SQL Server的优势在于:. NET代码是可以配置为在失败时重试发送消息几次的数据库。

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