How to test the email settings for a Sharepoint

SharePoint enable outgoing e-mail configuration and most of the time this configuration is straightforward. As of matter of fact you do not have much to configure

This same configuration is used for alerts and notifications as it is for invitations. Most of the e-mail sending is accomplished by OWSTIMER.EXE (The Sharepoint Timer Service) which run as the Farm Administrator Account in all servers in the farm. When you have alerts sending e-mail most of the time and failing just occasionally, it is possible that one of the servers is not able to deliver the e-mail. Testing e-mail delivery in WFEs (Web Front Ends) is easier, since adding someone to a site and sending a welcome (see screenshot below) mail will force Sharepoint to send the e-mail from the WFE serving the request and the result is shown immediately when you press OK. It may bring some extra configuration to identify the WFE if you are using NLB (Network Load Balancer), but nothing compared to OWSTIMER.EXE identification problem.

By adding a user to your SharePoint Web application and sending a Welcome mail is a fast to way to identify connection problems as the result is in the next page

However if you still have problems to identify why e-mails are not being delivered using the Welcome Page or the server you suspect is not serving pages you may need to run a few other tests. First, you have to check if the suspect server is reaching the SMTP server. You can use the basic ping and then try the telnet connection to see if you get the right response as specified in http://support.microsoft.com/kb/153119. If everything seems correct and you still have problem, try the following code:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using System.Net.Mail;

namespace SendMail
{
public class MailServerInfo
{
protected string server;

        public string Server
{
get { return server; }
}
protected string from;

        public string From
{
get { return from; }
}
protected string replyTo;

        public string ReplyTo
{
get { return replyTo; }
}

        public MailServerInfo(SPSite spSite)
{
server = “(empty)”;
from = “(empty)”;
replyTo = “(empty)”;
SPWebApplication spWebApplication = spSite.WebApplication;
if (spWebApplication.OutboundMailServiceInstance != null)
{
from = spWebApplication.OutboundMailSenderAddress;
replyTo = spWebApplication.OutboundMailReplyToAddress;
SPOutboundMailServiceInstance smtpServer = spWebApplication.OutboundMailServiceInstance;
server = smtpServer.Server.Address;
}

        }

        public string ServerInformation
{
get
{

               return String.Format(“Site Specific – server: {0}, From: {1}, ReplyTo: {2}”,
server, from, replyTo);

            }
}
}

    class Program
{

        static void Main(string[] args)
{
Console.WriteLine(“Sharepoint Mail Configuration Tester”);
Console.WriteLine(“======================================”);
Console.WriteLine(“Written by Joseph”);
Console.WriteLine(“More info at
http://sprider.org”);
Console.Write(“Press any other key to continue…”);
Console.ReadKey();
Console.Write(“\n\nPlease enter url (eg.:
http://contoso):”);
            string siteUrl = Console.ReadLine();

            try
{
using (SPSite spSite = new SPSite(siteUrl))
{
using (SPWeb web = spSite.OpenWeb())
{
if (web == null)
{
Console.WriteLine(“Web could not be found (OpenWeb returned null). Aborting.”);
return;
}
Console.WriteLine(“The site has been found at ” + web.Url);
Console.Write(“Please enter recipient login name (eg. domain\\administrator): “);
string login = Console.ReadLine();
string displayName = “”;
string emailAddress = “”;
try
{
SPUtility.GetFullNameandEmailfromLogin(web, login, out displayName,
out emailAddress);
}
catch (Exception ex)
{
Console.WriteLine(“Unable to get user information.”);
Console.WriteLine(“Error: “+ex.Message);
Console.Write(“Press any key…”);
Console.ReadKey();

                            return;
}
Console.WriteLine(“Display Name: {0}\nE-mail: {1}”, displayName,
emailAddress);
string subject = “Test message”;
string body = “Testing app message”;
MailServerInfo serverInfo = new MailServerInfo(spSite);

                        string server = serverInfo.Server;
string replyTo = serverInfo.ReplyTo;
string from = serverInfo.From;

                        Console.WriteLine(serverInfo.ServerInformation);
Console.WriteLine(“Trying to send e-mail via Sharepoint…”);

                        if (!SPUtility.SendEmail(web, true, true, emailAddress, subject, body))
{
Console.WriteLine(“Error: Unable to send e-mail via Sharepoint”);
Console.WriteLine(“Trying via SMTP…”);

                            SmtpClient client;
try
{
client = new SmtpClient(server);
}
catch (Exception ex)
{
Console.WriteLine(“Could not contact server”);
Console.WriteLine(“Error: ” + ex.Message);
Console.Write(“Press any key…”);
Console.ReadKey();

                                return;
}
try
{

                                client.Send(from, emailAddress, subject, body);

                            }
catch (Exception ex)
{
Console.WriteLine(“Unable to send via SMTP client”);
Console.WriteLine(“Error: ” + ex.Message);
Console.Write(“Press any key…”);
Console.ReadKey();

                                return;
}
Console.WriteLine(“E-mail has been sent successfully via SMTP client”);

                        }
else
{
Console.WriteLine(“E-mail has been sent successfully via Sharepoint”);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(“Unable to find url.”);
Console.WriteLine(“Error: ” + ex.Message);

            }
Console.Write(“Press any key…”);
Console.ReadKey();

        }
}
}

 

You can download project and executable at  http://www.codeplex.com/rodneyviana/Release/ProjectReleases.aspx?ReleaseId=19103.

If you are not much in software development you can extract only the .exe file (SendMail\SendMail\bin\Debug\SendMail.exe) and run it without installation. This application will send an e-mail using the farm’s configuration. Enter the url of a valid site and a valid user login name which will be the recipient of the test message. The application will retrieve the e-mail information from the user login name. You must run the application from one of the servers in the farm.

Now the application will resolve the recipient e-mail address as well as the farm’s outgoing e-mail configuration

If the configuration is correct the intended recipient will receive the e-mail

If you can receive this e-mail and still are unable to receive e-mail from sharepoint check the Application Pool credential (in case of Welcome page) or web farm administrator account (in case of alert and other e-mails sent by OWSTIMER.EXE).

Advertisements

About Joseph Velliah
As a SharePoint Developer my professional interests tend to be technical and SharePoint focused. I run a blog at "SP RIDER" where you can expect to read HOW TOs and scenarios that I run into during my day to day job. I hope my posts will give back a little to the community that is helped me.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: