如何在asp.net中编写邮件代码

本文关键字:代码 asp net | 更新日期: 2023-09-27 18:14:52

在web应用程序中,我如何找到如果邮件没有交付。我编写了发送邮件的代码,我可以得到邮件发送失败报告的报告吗?

           objMialBe.Empid =   Session["empid"].ToString();
            string myHost = System.Net.Dns.GetHostName();
            objMialBe.IpAddress = System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();
            objMialBe.Subject = txtSubject.Text;
            objMialBe.Mbody = txtDescri.Text;
            objMialBe.ValidDays = Convert.ToInt32(txtNoDays.Text);
            objMialBe.SizeInBytes = Tsize.ToString();
            objMialBe.TempAsizebbyte = TotSize;
            objMialBe.TemsizeMb = TotSizeMB;
            objMialBe.SessionId = Session.SessionID.ToString();
            int k = ObjMailBl.Insert_Mails_Sent(objMialBe); // inserting data in table Tbl_Mails_sent
            string TO = txtTo.Text;
            string[] inputArray = TO.Split(';');
            for (int i = 0; i < inputArray.Length; i++)
            {
                int cnt = 1;
                //// Response.Write("#" + i + ": " + inputArray[i] + "<br>");
                MailMessage mail = new MailMessage();
                string t = txtTo.Text;
                mail.To.Add(inputArray[i]);
                mail.To.Add(t);
                mail.From = new MailAddress("iilwebadmin@indimmune.com");
                mail.Subject = txtSubject.Text;
                StringBuilder stbldr = new StringBuilder();
                stbldr.Append("<html>" + txtDescri.Text + "</html>");
                if (item >= 1)
                {
                    foreach (DataListItem dl in dtlstTemp.Items)
                    {
                        Label latt = (Label)dl.FindControl("lblFileAtt");
                        stbldr .Append ("<html> <a href='http://8888888/*******/mails/CheckAttachment.aspx?attid="+s+"&Eid="+txtTo.Text+"&afile="+latt.Text +"' target='_blank'>Attachment..</a> "+cnt+" <br> </html>");
                        cnt++;
                    }  
                }
                mail.Body = stbldr.ToString();
                SmtpClient smtp = new SmtpClient("**********", 25);
                smtp.Send(mail);

如何在asp.net中编写邮件代码

我想你已经知道System.Net.Mail.MailMessage上的DeriveryNotificationOptions property了。使用该属性唯一棘手的部分是它的enum类型表示位字段,因此应该将其设置为希望应用的选项的总和。

例如,如果您想要延迟、失败或成功的传递通知,您应该将属性设置为

DeliveryNotificationOptions.Delay + DeliveryNotificationOptions.OnFailure + DeliveryNotificationOptions.OnSuccess

这是在邮件未发送时捕获失败报告或任何错误的方法(失败报告)

 // Change your Try-Catch to call the new method named 'CheckExceptionAndResend'
// Error handling for sending message   
try 
{
    smtpClient.Send(message);
    // Exception contains information on each failed receipient   
}
catch (System.Net.Mail.SmtpFailedRecipientsException recExc)
{
    // Call method that will analyze exception and attempt to re-send the email
    CheckExceptionAndResend(recExc, smtpClient, message);
}
catch (System.Net.Mail.SmtpException smtpExc)
{
    // Log error to event log using StatusCode information in   
    // smtpExc.StatusCode   
    MsgBox((smtpExc.StatusCode.ToString + " ==>Procedure SmtpException"));
}
catch (Exception Exc) 
{
    // Log error to event log using StatusCode information in   
    // smtpExc.StatusCode   
    MsgBox((Exc.Message + " ==>Procedure Exception"));
}

private void CheckExceptionAndResend(System.Net.Mail.SmtpFailedRecipientsException exObj, System.Net.Mail.SmtpClient smtpClient, MailMessage emailMessage)
{
    try
    {
        for (int recipient = 0; (recipient <= (exObj.InnerExceptions.Length - 1)); recipient++)
        {
            System.Net.Mail.SmtpStatusCode statusCode;
            // Each InnerException is an System.Net.Mail.SmtpFailed RecipientException   
            statusCode = exObj.InnerExceptions(recipient).StatusCode;
            if (((statusCode == Net.Mail.SmtpStatusCode.MailboxBusy) 
                        || (statusCode == Net.Mail.SmtpStatusCode.MailboxUnavailable))) 
            {
                // Log this to event log: recExc.InnerExceptions(recipient).FailedRecipient   
                System.Threading.Thread.Sleep(5000);
                smtpClient.Send(emailMessage);
            }
            else
            {
                // Log error to event log.   
                // recExc.InnerExceptions(recipient).StatusCode or use statusCode   
            }
        }
        MsgBox((exObj.Message + " ==>Procedure SmtpFailedRecipientsException"));
    }
    catch (Exception ex) 
    {
        // At this point we have an non recoverable issue:
        // NOTE:  At this point we do not want to re-throw the exception because this method 
        // was called from a 'Catch' block and we do not want a hard error to display to the client.
        // Options: log error, report issue to client via msgbox, etc.   This is up to you.
        // To display issue as you have before:
        MsgBox((exObj.Message + " ==>Email was not sent"));
    }
}