Skip to content

Commit

Permalink
improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
EitanBlumin committed Sep 29, 2019
1 parent 47eebd3 commit 0ff3b91
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions sql_clr_ics/clr_send_ics_invite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,16 @@ SqlString profile_name

#region get missing info from sysmail profile

string currentPhase = "";
try
{
if (from_address.IsNull || (username.IsNull && use_default_credentials.IsNull) || !profile_name.IsNull)
{
currentPhase = "Creating SqlConnection";
SqlConnection con = new SqlConnection("context connection=true"); // using existing CLR context connection
currentPhase = "Creating SqlCommand";
SqlCommand cmd = con.CreateCommand();
currentPhase = "Opening SqlConnection";
con.Open();

if (profile_name.IsNull)
Expand All @@ -71,6 +77,7 @@ INNER JOIN [msdb].[dbo].[sysmail_profile] AS p
ON pp.profile_id = p.profile_id
WHERE pp.is_default = 1";

currentPhase = "Getting default DBMail profile";
using (SqlDataReader rdr = cmd.ExecuteReader())
{
if (!rdr.HasRows)
Expand Down Expand Up @@ -102,6 +109,7 @@ INNER JOIN [msdb].[dbo].[sysmail_server] AS s
cmd.Parameters.AddWithValue("@Seq", 1);
cmd.Parameters.AddWithValue("@Profile", profile_name.Value);

currentPhase = string.Format("Getting profile settings ({0})", profile_name.Value);
using (SqlDataReader rdr = cmd.ExecuteReader())
{
if (!rdr.HasRows)
Expand All @@ -112,6 +120,7 @@ INNER JOIN [msdb].[dbo].[sysmail_server] AS s
}
else
{
rdr.Read();
if (from_address.IsNull) from_address = rdr.GetSqlString(0);
if (reply_to.IsNull) reply_to = rdr.GetSqlString(1);
if (smtp_servername.IsNull) smtp_servername = rdr.GetSqlString(2);
Expand All @@ -123,6 +132,12 @@ INNER JOIN [msdb].[dbo].[sysmail_server] AS s
rdr.Close();
}
con.Close();
}

}
catch (Exception ex)
{
throw new Exception(string.Format("Error while {0}: {1}", currentPhase, ex.Message), ex);
}

#endregion get missing info from sysmail profile
Expand Down Expand Up @@ -232,7 +247,7 @@ INNER JOIN [msdb].[dbo].[sysmail_server] AS s
}
catch (Exception e)
{
throw new Exception(string.Format("Unable to attach '{0}': {1}", att_path, e.Message));
throw new Exception(string.Format("Unable to attach '{0}': {1}", att_path, e.Message), e);
}
}
}
Expand All @@ -243,7 +258,7 @@ INNER JOIN [msdb].[dbo].[sysmail_server] AS s
}
catch (Exception e)
{
throw new Exception("@from_address is invalid: " + e.Message);
throw new Exception("@from_address is invalid: " + e.Message, e);
}

try
Expand All @@ -252,7 +267,7 @@ INNER JOIN [msdb].[dbo].[sysmail_server] AS s
}
catch (Exception e)
{
throw new Exception("@reply_to is invalid: " + e.Message);
throw new Exception("@reply_to is invalid: " + e.Message, e);
}

try
Expand All @@ -261,7 +276,7 @@ INNER JOIN [msdb].[dbo].[sysmail_server] AS s
}
catch (Exception e)
{
throw new Exception("@recipients is invalid: " + e.Message);
throw new Exception("@recipients is invalid: " + e.Message, e);
}

try
Expand All @@ -270,7 +285,7 @@ INNER JOIN [msdb].[dbo].[sysmail_server] AS s
}
catch (Exception e)
{
throw new Exception("@copy_recipients is invalid: " + e.Message);
throw new Exception("@copy_recipients is invalid: " + e.Message, e);
}

try
Expand All @@ -279,7 +294,7 @@ INNER JOIN [msdb].[dbo].[sysmail_server] AS s
}
catch (Exception e)
{
throw new Exception("@blind_copy_recipients is invalid: " + e.Message);
throw new Exception("@blind_copy_recipients is invalid: " + e.Message, e);
}

#endregion initialize MailMessage and recipients
Expand All @@ -296,6 +311,7 @@ INNER JOIN [msdb].[dbo].[sysmail_server] AS s

ics_contents_str.AppendLine("BEGIN:VEVENT");
ics_contents_str.AppendLine(string.Format("STATUS:{0}", (method.Value == "CANCEL") ? "CANCELLED" : "CONFIRMED"));
ics_contents_str.AppendLine("TRANSP:OPAQUE");
ics_contents_str.AppendLine(string.Format("SEQUENCE:{0}", sequence.Value));

ics_contents_str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", start_time_utc.Value));
Expand All @@ -304,7 +320,10 @@ INNER JOIN [msdb].[dbo].[sysmail_server] AS s
if (!location.IsNull) ics_contents_str.AppendLine("LOCATION: " + location.Value);
ics_contents_str.AppendLine(string.Format("UID:{0}", event_identifier.Value));
ics_contents_str.AppendLine(string.Format("DESCRIPTION:{0}", body.Value));
ics_contents_str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE={0}:{1}", body_format.Value == "HTML" ? "text/html" : "text/plain", body.Value));

if (method.Value != "CANCEL")
ics_contents_str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE={0}:{1}", body_format.Value == "HTML" ? "text/html" : "text/plain", body.Value));

ics_contents_str.AppendLine(string.Format("SUMMARY:{0}", subject.Value));
ics_contents_str.AppendLine(string.Format("ORGANIZER:SENT-BY=\"mailto:{0}\";MAILTO:{0}", msg.From.Address));
ics_contents_str.AppendLine(string.Format("CLASS:{0}", sensitivity.Value.ToUpper()));
Expand Down Expand Up @@ -379,21 +398,22 @@ INNER JOIN [msdb].[dbo].[sysmail_server] AS s
smtpclient.UseDefaultCredentials = use_default_credentials.Value;
smtpclient.EnableSsl = enable_ssl.Value;
smtpclient.Credentials = credentials;
System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");
contype.Parameters.Add("method", "REQUEST");
contype.Parameters.Add("name", "Meeting.ics");
System.Net.Mime.ContentType calendar_contype = new System.Net.Mime.ContentType("text/calendar;charset=UTF-8");
calendar_contype.Parameters.Add("method", "REQUEST");
calendar_contype.Parameters.Add("name", "Meeting.ics");

AlternateView avBody = AlternateView.CreateAlternateViewFromString(body.Value, new System.Net.Mime.ContentType(body_format.Value == "HTML" ? "text/html" : "text/plain"));
AlternateView avBody = AlternateView.CreateAlternateViewFromString(body.Value, new System.Net.Mime.ContentType(body_format.Value == "HTML" ? "text/html;charset=UTF-8" : "text/plain"));
msg.AlternateViews.Add(avBody);

AlternateView avCal = AlternateView.CreateAlternateViewFromString(ics_contents.Value, contype);
AlternateView avCal = AlternateView.CreateAlternateViewFromString(ics_contents.Value, calendar_contype);
//avCal.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;
msg.AlternateViews.Add(avCal);

msg.Headers.Add("Content-class", "urn:content-classes:calendarmessage");
}
catch (Exception e)
{
throw new Exception("SMTP Client Configuration Error: " + e.Message);
throw new Exception("SMTP Client Configuration Error: " + e.Message, e);
}

#endregion initialize and configure SmtpClient
Expand All @@ -402,13 +422,17 @@ INNER JOIN [msdb].[dbo].[sysmail_server] AS s

try
{
//if (!suppress_info_messages)
// SqlContext.Pipe.Send(string.Format("Sending message to: {0}{1}{2}", msg.To.ToString(), Environment.NewLine, msg.Body));

smtpclient.Send(msg);

if (!suppress_info_messages)
SqlContext.Pipe.Send(string.Format("Mail Sent. Event Identifier: {0}", event_identifier.Value));
}
catch (Exception e)
{
throw new Exception("Error sending mail: " + e.Message);
throw new Exception("Error sending mail: " + e.Message, e);
}

#endregion send mail
Expand Down

0 comments on commit 0ff3b91

Please sign in to comment.