Skip to content
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

Open
brgrz opened this issue Aug 12, 2012 · 7 comments
Open

BCC and CC in Razor view when ViewBag.Bcc or ViewBag.CC is empty #38

brgrz opened this issue Aug 12, 2012 · 7 comments

Comments

@brgrz
Copy link

brgrz commented Aug 12, 2012

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?

@andrewdavey
Copy link
Owner

Please do fork the code and have a go at fixing this. I'd be very happy to accept a pull request. Thanks :)

@DWalkit
Copy link

DWalkit commented Nov 21, 2013

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) {
CC: @ViewBag.CC
}

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!

@jfrank14
Copy link

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?

@petersondrew
Copy link

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: foo@bar.com
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}
}

@andrewdavey
Copy link
Owner

I recommend taking a look here:

void AssignCommonHeaders(MailMessage message, Email email)

It should be possible to add some extra guards for empty email addresses.

@jfrank14
Copy link

Thanks. I will check it out.

On 8/26/2014 9:19 AM, Andrew Davey wrote:

I recommend taking a look here:

void AssignCommonHeaders(MailMessage message, Email email)

It should be possible to add some extra guards for empty email addresses.


Reply to this email directly or view it on GitHub #38 (comment).

@petrosmm
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants