Skip to content

Commit b9e1e98

Browse files
authored
feat: sanity-check email address and address lists when adding to a message (#981)
Fixes #980 When sending an email and providing an empty list of BCCs the request fails with BadRequest as outlined here: https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.bcc We want to inform the consumer that this is the case otherwise it may lead to email sending silently failing.
1 parent 8ea9556 commit b9e1e98

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/SendGrid/Helpers/Mail/SendGridMessage.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ public void AddTos(List<EmailAddress> emails, int personalizationIndex = 0, Pers
262262
/// </summary>
263263
/// <param name="email">Specify the recipient's email.</param>
264264
/// <param name="name">Specify the recipient's name.</param>
265+
/// <exception cref="System.ArgumentNullException">Thrown when the email parameter is null or whitespace</exception>
265266
public void AddCc(string email, string name = null)
266267
{
267268
if (string.IsNullOrWhiteSpace(email))
@@ -278,8 +279,14 @@ public void AddCc(string email, string name = null)
278279
/// <param name="email">An email recipient that may contain the recipient’s name, but must always contain the recipient’s email.</param>
279280
/// <param name="personalizationIndex">Specify the index of the Personalization object where you want to add the cc email.</param>
280281
/// <param name="personalization">A personalization object to append to the message.</param>
282+
/// <exception cref="System.ArgumentNullException">Thrown when the email parameter is null</exception>
281283
public void AddCc(EmailAddress email, int personalizationIndex = 0, Personalization personalization = null)
282284
{
285+
if (email == null)
286+
{
287+
throw new ArgumentNullException("email");
288+
}
289+
283290
if (personalization != null)
284291
{
285292
personalization.Ccs = personalization.Ccs ?? new List<EmailAddress>();
@@ -333,8 +340,20 @@ public void AddCc(EmailAddress email, int personalizationIndex = 0, Personalizat
333340
/// <param name="emails">A list of cc recipients. Each email object within this array may contain the recipient’s name, but must always contain the recipient’s email.</param>
334341
/// <param name="personalizationIndex">Specify the index of the Personalization object where you want to add the cc emails.</param>
335342
/// <param name="personalization">A personalization object to append to the message.</param>
343+
/// <exception cref="System.ArgumentNullException">Thrown when the emails parameter is null</exception>
344+
/// <exception cref="System.InvalidOperationException">Thrown when the emails parameter is empty</exception>
336345
public void AddCcs(List<EmailAddress> emails, int personalizationIndex = 0, Personalization personalization = null)
337346
{
347+
if (emails == null)
348+
{
349+
throw new ArgumentNullException("emails");
350+
}
351+
352+
if (emails.Count == 0)
353+
{
354+
throw new InvalidOperationException("Sequence contains no elements");
355+
}
356+
338357
if (personalization != null)
339358
{
340359
personalization.Ccs.AddRange(emails);
@@ -383,6 +402,7 @@ public void AddCcs(List<EmailAddress> emails, int personalizationIndex = 0, Pers
383402
/// </summary>
384403
/// <param name="email">Specify the recipient's email.</param>
385404
/// <param name="name">Specify the recipient's name.</param>
405+
/// <exception cref="System.ArgumentNullException">Thrown when the email parameter is null or whitespace</exception>
386406
public void AddBcc(string email, string name = null)
387407
{
388408
if (string.IsNullOrWhiteSpace(email))
@@ -399,8 +419,14 @@ public void AddBcc(string email, string name = null)
399419
/// <param name="email">An email recipient that may contain the recipient’s name, but must always contain the recipient’s email.</param>
400420
/// <param name="personalizationIndex">Specify the index of the Personalization object where you want to add the bcc email.</param>
401421
/// <param name="personalization">A personalization object to append to the message.</param>
422+
/// <exception cref="System.ArgumentNullException">Thrown when the email parameter is null</exception>
402423
public void AddBcc(EmailAddress email, int personalizationIndex = 0, Personalization personalization = null)
403424
{
425+
if (email == null)
426+
{
427+
throw new ArgumentNullException("email");
428+
}
429+
404430
if (personalization != null)
405431
{
406432
personalization.Bccs = personalization.Bccs ?? new List<EmailAddress>();
@@ -454,8 +480,20 @@ public void AddBcc(EmailAddress email, int personalizationIndex = 0, Personaliza
454480
/// <param name="emails">A list of bcc recipients. Each email object within this array may contain the recipient’s name, but must always contain the recipient’s email.</param>
455481
/// <param name="personalizationIndex">Specify the index of the Personalization object where you want to add the bcc emails.</param>
456482
/// <param name="personalization">A personalization object to append to the message.</param>
483+
/// <exception cref="System.ArgumentNullException">Thrown when the emails parameter is null</exception>
484+
/// <exception cref="System.InvalidOperationException">Thrown when the emails parameter is empty</exception>
457485
public void AddBccs(List<EmailAddress> emails, int personalizationIndex = 0, Personalization personalization = null)
458486
{
487+
if (emails == null)
488+
{
489+
throw new ArgumentNullException("emails");
490+
}
491+
492+
if (emails.Count == 0)
493+
{
494+
throw new InvalidOperationException("Sequence contains no elements");
495+
}
496+
459497
if (personalization != null)
460498
{
461499
personalization.Bccs.AddRange(emails);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using SendGrid.Helpers.Mail;
4+
using Xunit;
5+
6+
namespace SendGrid.Tests.PreSendEmailValidation
7+
{
8+
public class WhenCreatingASendGridMessage
9+
{
10+
[Fact]
11+
public void WithAnEmptyListOfBlindCarbonCopiesThenAnExceptionIsThrown()
12+
{
13+
var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty);
14+
Assert.Throws<InvalidOperationException>(() => { sendGridMessage.AddBccs(new List<EmailAddress>()); });
15+
}
16+
17+
[Fact]
18+
public void WithANullListOfBlindCarbonCopiesThenAnExceptionIsThrown()
19+
{
20+
var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty);
21+
Assert.Throws<ArgumentNullException>(() => { sendGridMessage.AddBccs(null); });
22+
}
23+
24+
[Fact]
25+
public void WithANullBlindCarbonCopyThenAnExceptionIsThrown()
26+
{
27+
var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty);
28+
Assert.Throws<ArgumentNullException>(() => { sendGridMessage.AddBcc(null, 0); });
29+
}
30+
31+
[Fact]
32+
public void WithAnEmptyListOfCarbonCopiesThenAnExceptionIsThrown()
33+
{
34+
var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty);
35+
Assert.Throws<InvalidOperationException>(() => { sendGridMessage.AddCcs(new List<EmailAddress>()); });
36+
}
37+
38+
[Fact]
39+
public void WithANullListOfCarbonCopiesThenAnExceptionIsThrown()
40+
{
41+
var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty);
42+
Assert.Throws<InvalidOperationException>(() => { sendGridMessage.AddCcs(new List<EmailAddress>()); });
43+
}
44+
45+
[Fact]
46+
public void WithANullCarbonCopyThenAnExceptionIsThrown()
47+
{
48+
var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty);
49+
Assert.Throws<ArgumentNullException>(() => { sendGridMessage.AddCc(null, 0); });
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)