0

Previously, my SPUtility.SendMail function is working.

But recently no email was being sent out.

Base on several articles, I tried setting HttpContext.Current to null before invoking SendMail and change HttpContext.Current back, and using elevated rights. But they did not work.

try
{
    using (var elevatedSite = new SPSite(TestConfig.GetString(TestConfig.Config.SiteUrl).TrimEnd('/')))
    {
        using (var elevatedWeb = elevatedSite.OpenWeb())
        {
            var to = new StringBuilder();
            foreach (var grp in grpNames)
            {
                var group = elevatedWeb.SiteGroups[grp];
                var groupValue = new SPFieldUserValue(elevatedWeb, group.ID, group.Name);
                var inbox = new Inbox
                {
                    Title = String.Format("Report generated@{0}", fileURL),,
                };

                foreach (SPUser user in group.Users.Cast<SPUser>().Where(usr => !usr.Email.IsNullOrEmpty()))
                {
                    to.AppendFormat("{0},", user.Email);
                }
            }

            bool isSent = SPUtility.SendEmail(elevatedWeb, true, false, "test@mail.com", ...);
        }
    }
}
catch (Exception ex)
{
    ex.PreserveStackTrace();
    throw;
}

try-catch and event viewer returned no errors.

also checked the sent status isSent and it returned true:


References:

https://social.technet.microsoft.com/Forums/office/en-US/16a62597-6076-4598-9799-dd6ea874156f/cannot-send-mail-using-sputilitysendmail?forum=sharepointadminlegacy

Why is HttpContext.Current set to null?

http://emailfromsharepoint.blogspot.com/2012/04/send-email-from-sharepoint.html

  • any entries in ULS log on the server? any recent changes made to smtp / relays? Most of the cases there are infrastructure reasons 4 this kind of error, not coding. – Celophysis Jan 14 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.