-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BCC and CC in Razor view when ViewBag.Bcc or ViewBag.CC is empty #38
Comments
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 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:
Then in my email view:
That could also easily be extended to handle cases where Bcc is null or empty:
|
I recommend taking a look here: postal/src/Postal/EmailParser.cs Line 55 in 409617d
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:
|
@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. |
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?
The text was updated successfully, but these errors were encountered: