First be sure to check the return value of the Smtp.SemdMessage method:
ISendMessageResult result = smtp.SendMessage(mail);
switch (result.Status)
{
case SendMessageStatus.Success:
break;
case SendMessageStatus.PartialSucess:
break;
case SendMessageStatus.Failure:
break;
}
By default, sending message fails if any of the recipient is rejected by the SMTP server, so only 2 statuses may be returned: SendMessageStatus.Success and SendMessageStatus.Failure.
You can set Smtp.Configuration.AllowPartialSending property to modify this behavior, messages will be sent even if the SMTP server rejects some recipients:
IMail mail = Fluent.Mail.Text("Sample email")
.Subject("subject goes here")
.From("user@example.com")
.To("invalid@example.com")
.To("valid@example.com")
.Create();
using (Smtp smtp = new Smtp())
{
smtp.Configuration.AllowPartialSending = true; // <-- ignore rejected recipients
smtp.Connect("smtp.example.com"); // or ConnectSSL for SSL
smtp.UseBestLogin("user@example.com", "password");
ISendMessageResult result = smtp.SendMessage(mail);
Assert.AreEqual(SendMessageStatus.PartialSucess, result.Status);
switch (result.Status)
{
case SendMessageStatus.Success:
break;
case SendMessageStatus.PartialSucess:
// error handling code goes here.
break;
case SendMessageStatus.Failure:
// error handling code goes here.
CollectionAssert.AreEqual(
new[] { "invalid@example.com" },
result.RejectedRecipients);
break;
}
client.Close();
}