Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11508

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Threading.ThreadAbortException: Thread was being aborted

$
0
0

Hi,

I am sending mail from my SharePoint webpart which sends mails to 1000's of vendors from sharepoint list.

On development it works perfectly but on production it fails from random vendor. Logs says

System.Net.Mail.SmtpException: Failure sending mail. ---> System.Threading.ThreadAbortException: Thread was being aborted

My code for sending mail is:

 public static string SendMail(string smtpFrom, string strPath, string strToMailAddress, string strSubject, string strBody, string strCCMailAddress, string strBccMailAddress)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    //my custom code
                    System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();

                    mail.From = smtpFrom;
                    mail.To = strToMailAddress;
                    mail.Cc = strCCMailAddress;
                    mail.Bcc = strBccMailAddress;
                    mail.Subject = strSubject;
                    mail.Body = strBody;
                    mail.BodyFormat = System.Web.Mail.MailFormat.Html;
                    System.Web.Mail.SmtpMail.SmtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;

                    //System.Web.Mail.MailAttachment imgObj1 = new System.Web.Mail.MailAttachment("imagePath");
                    //mail.Attachments.Add(imgObj1);

                    //Add the attachment
                    if (!string.IsNullOrEmpty(strPath))
                        mail.Attachments.Add(new System.Web.Mail.MailAttachment(strPath));

                    System.Web.Mail.SmtpMail.Send(mail);

                });
            }
            catch (Exception ex)
            {
                SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
                diagSvc.WriteTrace(0, new SPDiagnosticsCategory("CnP", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, "SendMail - Writing to the ULS log:  {0}", new object[] { ex.ToString() });
                diagSvc.WriteTrace(0, new SPDiagnosticsCategory("CnP", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, "SendMail - Writing to the ULS log:  {0}", new object[] { ex.Message });
                diagSvc.WriteTrace(0, new SPDiagnosticsCategory("CnP", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, "SendMail - Writing to the ULS log:  {0}", new object[] { ex.StackTrace });
                return ex.Message;
            }
            return null;
        }

Can anyone please help. Thanks in advance.


Viewing all articles
Browse latest Browse all 11508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>