// warning: FromAddress missing!
EmailMessage emailMessage = null;
try
{
emailMessage = new EmailMessage();
if (emailMessage.Send(smtp) == false)
{
log.Error("Unable to send mail.");
}
}
catch (MailException ex)
{
StringBuilder sb = new StringBuilder("Unable to send mail.");
if (emailMessage != null)
{
sb.Append(Environment.NewLine);
sb.Append(emailMessage.ToDataString());
}
log.Error(sb.ToString(), ex);
}
catch (Exception ex)
{
if (emailMessage != null)
{
log.Error("Unable to send mail: " + emailMessage.ToDataString(), ex);
}
else
{
log.Error("Unable to send mail.", ex);
}
}
If the EmailMessage object had a property indicating whether or not it should swallow its own exceptions it would be easier to extract this information later:
// warning: FromAddress missing!
EmailMessage emailMessage = new EmailMessage();
emailMessage.ThrowExceptions = false;
if (emailMessage.Send(smtp) == false)
{
log.Error(
"Unable to send mail."+emailMessage.ToDataString(),
emailMessage.GetLastException());
}
You would always have access to a non-null EmailMessage object that you could investigate and log if the email was not send successfully.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
One of my beefs about C# is that it the compiler doesn't force you to acknowledge potential exceptions, making it dangerous to use anything outside of a try-catch block. I feel it's a violation of the calling contract, and it means that unless you really trust the documentation and love to read it, you need to do one catch-all block somewhere with a "catch (Exception ex)".
That said, my philosophy is that an exception should be thrown for abnormal behaviour (e.g. there is a network error, a problem with the SMTP server, etc.), and I generally leave it up to the caller to catch it. It certainly makes for nicer error-handling code in the module. But that's my personal preference....
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If it's throwing a null pointer exception, that'd be a bug---I'll look at that. Hence my distaste for the looseness of the C# Exception handling. :)
-Mike
P.S. Technically, my ToDataString() method should have an "internal" declaration, but because my NUnit testing code is in a separate Project, I made it public.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sometimes its cumbersome to deal with errors:
// warning: FromAddress missing!
EmailMessage emailMessage = null;
try
{
emailMessage = new EmailMessage();
if (emailMessage.Send(smtp) == false)
{
log.Error("Unable to send mail.");
}
}
catch (MailException ex)
{
StringBuilder sb = new StringBuilder("Unable to send mail.");
if (emailMessage != null)
{
sb.Append(Environment.NewLine);
sb.Append(emailMessage.ToDataString());
}
log.Error(sb.ToString(), ex);
}
catch (Exception ex)
{
if (emailMessage != null)
{
log.Error("Unable to send mail: " + emailMessage.ToDataString(), ex);
}
else
{
log.Error("Unable to send mail.", ex);
}
}
If the EmailMessage object had a property indicating whether or not it should swallow its own exceptions it would be easier to extract this information later:
// warning: FromAddress missing!
EmailMessage emailMessage = new EmailMessage();
emailMessage.ThrowExceptions = false;
if (emailMessage.Send(smtp) == false)
{
log.Error(
"Unable to send mail."+emailMessage.ToDataString(),
emailMessage.GetLastException());
}
You would always have access to a non-null EmailMessage object that you could investigate and log if the email was not send successfully.
One of my beefs about C# is that it the compiler doesn't force you to acknowledge potential exceptions, making it dangerous to use anything outside of a try-catch block. I feel it's a violation of the calling contract, and it means that unless you really trust the documentation and love to read it, you need to do one catch-all block somewhere with a "catch (Exception ex)".
That said, my philosophy is that an exception should be thrown for abnormal behaviour (e.g. there is a network error, a problem with the SMTP server, etc.), and I generally leave it up to the caller to catch it. It certainly makes for nicer error-handling code in the module. But that's my personal preference....
This was the case that made me suggest the idea:
EmailMessage emailMessage = null;
try
{
emailMessage = new EmailMessage();
emailMessage.AddToAddress(new DotNetOpenMail.EmailAddress(toEmail));
// emailMessage.FromAddress = new DotNetOpenMail.EmailAddress(fromEmail);
emailMessage.Subject = subject;
emailMessage.TextPart = new TextAttachment(textBody);
emailMessage.Send(smtpServer);
}
catch (MailException ex)
{
if (emailMessage != null)
{
emailLog.Info(emailMessage.ToDataString(), ex);
}
}
catch (Exception ex)
{
emailLog.Error("Unable to send mail.", ex);
}
[NullReferenceException: Object reference not set to an instance of an object.]
DotNetOpenMail.EmailMessage.GetStandardHeaders(Encoding charset, IEncoder encoder) +46
DotNetOpenMail.EmailMessage.ToDataStringHeaders(Encoding charset, IEncoder encoder) +44
DotNetOpenMail.EmailMessage.ToDataString() +27
If emailMessage is not null how do I know if its safe to call ToDataString()?
If it's throwing a null pointer exception, that'd be a bug---I'll look at that. Hence my distaste for the looseness of the C# Exception handling. :)
-Mike
P.S. Technically, my ToDataString() method should have an "internal" declaration, but because my NUnit testing code is in a separate Project, I made it public.