Search code examples
c#asp.net-mvcemailpostal

How to prevent email subject, from, cc and bcc from embedding in email body in asp.net mvc


I am sending emails in my asp.net mvc application using postal. The emails are getting sent but the subject, from, cc and bcc fields are getting embedded in the email body instead of showing up in the appropriate areas.

Here are my smtp settings in web.config

<smtp from="support@domain.com" deliveryMethod="Network">
    <network host="mail.domain.com" userName="support@domain.com" password="mypassword" defaultCredentials="false" port="25" />
 </smtp>

Here is my email view

@{
    Layout = null;
 }

To: @ViewBag.To
From: support@domain.com
Bcc: support@domain.com
Subject: Welcome to @ViewBag.ClientName reporting system

Hello @ViewBag.Firstname,

Welcome.

Regards

My smtp settings in SmarterMail are correct and testing works perfectly. The problem is the formatting. How can I fix this?

The email gets sent but shows up like this:

From: support@domain.com
To:
Date: Thu, 21 May 2015 16:40:34 +0300
Subject:

--email body starts here--

From: support@domain.com

Bcc: support@domain.com

Subject: Welcome to Client Name reporting system

Hello user,

Welcome.

Regards

UPDATE: Here is the code I'm using to send the email. I'm using postal

dynamic email = new Email("WelcomeEmail");
email.To = user.Email;
email.FirstName = user.FirstName;
email.ClientName = clientName;
email.Send();

Solution

  • Found the solution. The problem was Postal for some reason not picking up the email headers from my email view. So I set up all headers in the controller and only left the body of the email in the view. This is my updated email view.

    @{
       Layout = null;
    } 
    
    Hello @ViewBag.Firstname,
    
    Welcome.
    
    Regards
    

    and here is my updated setup in the controller

    dynamic email = new Email("WelcomeEmail");
    email.To = user.Email;
    email.Bcc = "admin@domain.com";
    email.From = "support@domain.com";
    email.Subject = "Welcome";
    email.FirstName = user.FirstName;
    email.ClientName = clientName;
    email.Send();