postal
postal copied to clipboard
BCC and CC in Razor view when ViewBag.Bcc or ViewBag.CC is empty
Hey Andrew,
let's suppose we have the following structure of an Email razor view:
To: @ViewBag.ToDisplayName @ViewBag.To CC: @ViewBag.Cc From: @ViewBag.FromDisplayName @ViewBag.From Subject: @ViewBag.Subject
Now, if our controller/service code doesn't set the CC in the ViewBag, the sending will fail because the EmailParser will try to add an empty string to the MailMessage.CC collection (inside the AssignEmailHeaderToMailMessage() method). I mean, not that the sending will fail but the request will end up in an exception:
The parameter 'addresses' cannot be an empty string. Parameter name: addresses
On quite a few occasions I want to be able to set BCC or CC recepients but sometimes those fields might even be empty in my application. In that case, we should not be adding them to the collection.
What do you suggest to correct that or should I just fork it and correct the AssignEmailHeaderToMailMessage() method myself?
Please do fork the code and have a go at fixing this. I'd be very happy to accept a pull request. Thanks :)
In case anyone else runs into this, an easy workaround (at least it works in MVC4) is to move the Bcc or CC to the bottom of your header, with a Razor @if, like so:
To: @ViewBag.To
From: @ViewBag.From
Subject: @ViewBag.Subject
Content-Type: text/html @if (@ViewBag.CC != null) {
}
If I put the CC part on its own line it wouldn't always work. And I actually included the 'CC:' in the ViewBag.CC but added it above as I'd imagine it'd work that way, too! Obviously, in your controller code, you'll want to set email.CC = null if you have no addresses to add! And I have it all on one line, GitHub is formatting it for me! It may not work on more than one line!
Why does it have to be at the end of the file? Wouldn't it work the same if it came right after the To: line?
I took a slightly different approach since I use strongly-typed emails. I use a shared layout for my email templates with the following:
@model Models.EmailBase
To: @Model.To
From: [email protected]
Subject: @Model.Subject
Content-Type: text/html; charset=utf-8
@RenderSection("bcc", required: false)
Then in my email view:
@model Models.SomeEmaliWithBcc
@section bcc {BCC: @Model.Bcc}
That could also easily be extended to handle cases where Bcc is null or empty:
@model Models.SomeEmaliWithBcc
@if (!String.IsNullOrEmpty(Model.Bcc))
{
@section bcc {BCC: @Model.Bcc}
}
I recommend taking a look here: https://github.com/andrewdavey/postal/blob/409617d49b5489e2d6c927b596171e2035299704/src/Postal/EmailParser.cs#L55 It should be possible to add some extra guards for empty email addresses.
Thanks. I will check it out.
On 8/26/2014 9:19 AM, Andrew Davey wrote:
I recommend taking a look here: https://github.com/andrewdavey/postal/blob/409617d49b5489e2d6c927b596171e2035299704/src/Postal/EmailParser.cs#L55 It should be possible to add some extra guards for empty email addresses.
Reply to this email directly or view it on GitHub https://github.com/andrewdavey/postal/issues/38#issuecomment-53418553.
@DWalkit wrote correctly in his analysis of the problem. However, does any such remedy exist for the 'Reply-To', it has a much different behaviour.