0

I have an event receiver which get fired when a new discussion board item is added. inside the event receiver i am sending email to certain users group, as follow:-

 public override void ItemAdded(SPItemEventProperties properties)
        {
                        base.ItemAdded(properties);
                        string fromField = "Sharepoint@ourcompany.net";

                        using (MailMessage myMailMessage = new MailMessage())
                        {
                            foreach (SPUser pp in properties.Web.SiteGroups.GetByID(int.Parse(groupid)).Users)
                            {
                                if (pp.Email != null && !string.IsNullOrEmpty(pp.Email))
                                    myMailMessage.To.Add(new MailAddress(pp.Email));
                            }
                            System.Text.StringBuilder mailBody = new System.Text.StringBuilder();
                            mailBody.AppendLine("<span style='font-family:Segoe UI;font-size:16px'>Hi All,</span><br/><br/>");
                            //code goes here!!                          

                            //Get the SMTP server
                            SmtpClient client = new SmtpClient();
                            client.Host = properties.Site.WebApplication.OutboundMailServiceInstance.Server.Address;                           
                           // client.Port = 25;
                            client.DeliveryMethod = SmtpDeliveryMethod.Network;
                            myMailMessage.IsBodyHtml = true;
                            myMailMessage.Subject = subject;
                            myMailMessage.Body = mailBody.ToString();
                            myMailMessage.From = new MailAddress(fromField);
                            client.Send(myMailMessage);
                        }

but no emails are being sent. now inside my sharepoint farm i can send emails from sharepoint designer (Send Email activity) + if users add notifications they can receive,, so i am not sure why my above code is not working?

Thanks

  • Why not use SPUtility.SendMail() ? – Gautam Sheth Jan 23 '17 at 13:15
  • @GautamSheth i think it is the same is this correct? except SPUtility.SendMail() have characters limit ,, while using SMTPMessage() does not .. – john Gu Jan 23 '17 at 13:21
  • @john G, do you have any error thrown by the code? Is the SMTP server the IIS local one? – Evariste Jan 23 '17 at 15:36
0

Why do you use the MailMessage API instead of the SharePoint built-in SPUtility.SendEmail method?
SPUtility.SendEmail would automatically get the current SMTP server configured at the farm or Web app level for you.

You can find a lot of examples about its usage, even if it's quite straigh forward.

Also, are you 100% sure your ER is actually called?

  • but also using MailMessage is suppose to work,, now inside our internal sharepoint i always use MailMessage and it si working well... but when i try it on one of our customer farms it is nt workinh. and yes the ER is being called. – john Gu Jan 23 '17 at 13:26
  • Is SPContext.Current.Site.WebApplication.OutboundMailServiceInstance set? What does it contain? Maybe the SMTP server had been set at the famr level (not Web app), and SPContext.Current.Site.WebApplication.OutboundMailServiceInstance is empty... – Evariste Jan 23 '17 at 13:29
  • so how i can check if the SMTP server have been set on farm level and/or on web application level? now when i went to "CA>>outgoing email" the smtp setting is there ,, also when i check the outgoing email for a web application the SMTP settings are there also ... – john Gu Jan 23 '17 at 13:36
0

Try below code:

using (MailMessage myMailMessage = new MailMessage())
{

    //Get the SMTP server
    SPOutboundMailServiceInstance smtpServer = properties.Site.WebApplication.OutboundMailServiceInstance;
    System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(smtpServer.Server.Address);

    foreach (SPUser pp in properties.Web.SiteGroups.GetByID(int.Parse(groupid)).Users)
    {
        if (pp.Email != null && !string.IsNullOrEmpty(pp.Email))
            myMailMessage.To.Add(new MailAddress(pp.Email));
    }
    System.Text.StringBuilder mailBody = new System.Text.StringBuilder();
    mailBody.AppendLine("<span style='font-family:Segoe UI;font-size:16px'>Hi All,</span><br/><br/>");
    //code goes here!!                          

    myMailMessage.IsBodyHtml = true;
    myMailMessage.Subject = subject;
    myMailMessage.Body = mailBody.ToString();
    myMailMessage.From = new System.Net.Mail.MailAddress(properties.Site.WebApplication.OutboundMailSenderAddress);
    smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
    smtp.Send(myMailMessage);
}

Ensure that Central Admin -> System Settings -> Configure outgoing e-mail settings are configured correctly.

  • so you are not defining client.Host ?? – john Gu Jan 23 '17 at 13:32
  • no i have already mentioned it in this line - System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(smtpServer.Server.Address); – Gautam Sheth Jan 23 '17 at 13:35
  • Looks like some access related issue, so have updated code to include Credentials – Gautam Sheth Jan 23 '17 at 13:47
  • will try with credentials and without credentials but i am confused as my SendEmail activity inside the workflow is sending emails correctly also user notifications are working well also .. so not sure why my event receivers are not sending emails.. – john Gu Jan 23 '17 at 13:55
  • 1
    hmm ...Credentials are necessary if the server requires the client to authenticate before it will send e-mail on the client's behalf. According to this [SmtpClient.Credentials Property] (msdn.microsoft.com/en-us/library/…) – Gautam Sheth Jan 23 '17 at 13:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.