How to test the Mail Settings for a Sharepoint Web Application

WSS and MOSS 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 as shown below:

image

Figure 1 - Outgoing e-mail settings

 

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.

image

Figure 2 - Sending 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 KB153119. If everything seems correct and you still have problem, try the following code:

 

(UPDATED)

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 e-mail configuration tester");
Console.WriteLine("======================================");
Console.WriteLine("Written by Rodney Viana");
Console.WriteLine("More info at
https://blogs.msdn.com/rodneyviana\n");
            Console.WriteLine("This sample application is supplied \"AS IS\"");
Console.WriteLine("It is ONLY for demonstration/proof of concept purposes");
Console.WriteLine("If you DO NOT agree with the license at:\n\thttps://www.codeplex.com/rodneyviana/license\n\tPress Ctrl+C");
Console.Write("Press any other key to continue...");
Console.ReadKey();
Console.Write("\n\nPlease enter url (eg.:
https://localhost):");
            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  https://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.

 

clip_image001

Figure 3 - 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 following e-mail:

 

clip_image002

Figure 4 - E-mail will be received if everything is configured correctly

 

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).