ValidationResult CanConnectServer(EmailSource emailSource)
{
try
{
var userParts = emailSource.UserName.Split('@');
var smtp = new SmtpClient(emailSource.Host, emailSource.Port)
{
Credentials = new NetworkCredential(userParts[0], emailSource.Password),
EnableSsl = emailSource.EnableSsl,
DeliveryMethod = SmtpDeliveryMethod.Network,
Timeout = emailSource.Timeout
};
try
{
smtp.Send(emailSource.TestFromAddress, emailSource.TestToAddress, "Test Message", "This is a test message");
return new ValidationResult();
}
finally
{
smtp.Dispose();
}
}
catch(SmtpException sex)
{
RaiseError(sex);
var message = sex.Message;
if(sex.StatusCode == SmtpStatusCode.MustIssueStartTlsFirst && sex.Message.Contains("Learn more at"))
{
message = message.Replace(" Learn more at", "");
}
var errors = new StringBuilder();
errors.AppendFormat("{0} ", message);
Exception ex = sex.InnerException;
while(ex != null)
{
errors.AppendFormat("{0} ", ex.Message);
ex = ex.InnerException;
}
return new ValidationResult
{
IsValid = false,
ErrorMessage = errors.ToString()
};
}
}