Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions Smtpapi/HeaderTests/TestHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void TestSetIpPool()
}

[Test]
public void TestSetSendAt()
public void TestSetSendAt_DateTime()
{
var test = new Header();
var now = DateTime.UtcNow;
Expand All @@ -130,13 +130,33 @@ public void TestSetSendAt()
}

[Test]
public void TestSetSendEachAt()
public void TestSetSendAt_DateTimeOffset()
{
var test = new Header();
var now = DateTimeOffset.UtcNow;
test.SetSendAt(now);
string result = test.JsonString();
Assert.AreEqual("{\"send_at\" : " + Utils.DateTimeOffsetToUnixTimestamp(now) + "}", result);
}

[Test]
public void TestSetSendEachAt_DateTime()
{
var test = new Header();
var now = DateTime.UtcNow;
test.SetSendEachAt(new List<DateTime> { now, now.AddSeconds(10) });
string result = test.JsonString();
Assert.AreEqual("{\"send_each_at\" : [" + Utils.DateTimeToUnixTimestamp(now) + "," + Utils.DateTimeToUnixTimestamp(now.AddSeconds(10)) + "]}", result);
}

[Test]
public void TestSetSendEachAt_DateTimeOffset()
{
var test = new Header();
var now = DateTimeOffset.UtcNow;
test.SetSendEachAt(new List<DateTimeOffset> { now, now.AddSeconds(10) });
string result = test.JsonString();
Assert.AreEqual("{\"send_each_at\" : [" + Utils.DateTimeOffsetToUnixTimestamp(now) + "," + Utils.DateTimeOffsetToUnixTimestamp(now.AddSeconds(10)) + "]}", result);
}
}
}
}
27 changes: 24 additions & 3 deletions Smtpapi/Smtpapi/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void SetIpPool(string pool)

/// <summary>
/// Schedule the email to be sent in the future. You can find further documentation about scheduled sends here:
/// https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html
/// https://sendgrid.com/docs/for-developers/sending-email/scheduling-parameters/
/// </summary>
/// <param name="sendTime">DateTime representing the time to send the email. See docs for limitations. </param>
public void SetSendAt(DateTime sendTime)
Expand All @@ -181,16 +181,37 @@ public void SetSendAt(DateTime sendTime)
_settings.AddSetting(keys, Utils.DateTimeToUnixTimestamp(sendTime));
}

/// <summary>
/// Schedule the email to be sent in the future. You can find further documentation about scheduled sends here:
/// https://sendgrid.com/docs/for-developers/sending-email/scheduling-parameters/
/// </summary>
/// <param name="sendTime">DateTimeOffset representing the time to send the email. See docs for limitations. </param>
public void SetSendAt(DateTimeOffset sendTime)
{
var keys = new List<string> { "send_at" };
_settings.AddSetting(keys, Utils.DateTimeOffsetToUnixTimestamp(sendTime));
}

/// <summary>
/// Schedule each email in your batch to be sent at a specific time in the future. You can find further documentation about scheduled sends here:
/// https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html
/// https://sendgrid.com/docs/for-developers/sending-email/scheduling-parameters/
/// </summary>
/// <param name="sendDateTimes">A collection of DateTimes, with each time corresponding to one recipient in the SMTP API header</param>
public void SetSendEachAt(IEnumerable<DateTime> sendDateTimes)
{
_settings.AddArray(new List<string> { "send_each_at" }, sendDateTimes.Select(Utils.DateTimeToUnixTimestamp).Cast<object>().ToArray());
}

/// <summary>
/// Schedule each email in your batch to be sent at a specific time in the future. You can find further documentation about scheduled sends here:
/// https://sendgrid.com/docs/for-developers/sending-email/scheduling-parameters/
/// </summary>
/// <param name="sendDateTimes">A collection of DateTimeOffsets, with each time corresponding to one recipient in the SMTP API header</param>
public void SetSendEachAt(IEnumerable<DateTimeOffset> sendDateTimes)
{
_settings.AddArray(new List<string> { "send_each_at" }, sendDateTimes.Select(Utils.DateTimeOffsetToUnixTimestamp).Cast<object>().ToArray());
}

#endregion
}
}
}
13 changes: 12 additions & 1 deletion Smtpapi/Smtpapi/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,16 @@ public static int DateTimeToUnixTimestamp(DateTime dateTime)
var span = (dateTime - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
return (int)span.TotalSeconds;
}

/// <summary>
/// Convert a DateTimeOffset to a UNIX Epoch Timestamp
/// </summary>
/// <param name="dateTimeOffset">Date to convert to timestamp</param>
/// <returns>Timestamp</returns>
public static int DateTimeOffsetToUnixTimestamp(DateTimeOffset dateTimeOffset)
{
var span = (dateTimeOffset - new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero));
return (int)span.TotalSeconds;
}
}
}
}