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

1.3k Views Asked by At

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="[email protected]" deliveryMethod="Network">
    <network host="mail.domain.com" userName="[email protected]" password="mypassword" defaultCredentials="false" port="25" />
 </smtp>

Here is my email view

@{
    Layout = null;
 }

To: @ViewBag.To
From: [email protected]
Bcc: [email protected]
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: [email protected]
To:
Date: Thu, 21 May 2015 16:40:34 +0300
Subject:

--email body starts here--

From: [email protected]

Bcc: [email protected]

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();
3

There are 3 best solutions below

0
On BEST ANSWER

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 = "[email protected]";
email.From = "[email protected]";
email.Subject = "Welcome";
email.FirstName = user.FirstName;
email.ClientName = clientName;
email.Send();
0
On

Something must be preventing Postal from seeing the headers.

Could there be any weird whitespace or text encoding issues? Maybe try re-saving the cshtml file with a specific encoding (e.g. UTF-8).

Without having the exact cshtml it's hard to diagnose further. Could you upload it somewhere public?

0
On

Try like this,.

     MailMessage msg = new MailMessage();
     msg.From = new MailAddress("from mail id");
     msg.To.Add(" to mail id");
     msg.CC.Add("Mail id");
     msg.Bcc.Add("Mail id");
     msg.Subject = "enter subject";
     msg.Body="enter text";