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 = "[email protected]"; 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

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

2 Answers 2

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?

3
  • 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. Commented Jan 23, 2017 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... Commented Jan 23, 2017 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 ... Commented Jan 23, 2017 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.

11
  • so you are not defining client.Host ?? Commented Jan 23, 2017 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); Commented Jan 23, 2017 at 13:35
  • Looks like some access related issue, so have updated code to include Credentials Commented Jan 23, 2017 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.. Commented Jan 23, 2017 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/…) Commented Jan 23, 2017 at 13:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.