Reputation: 17
Situation:
1)I want to send a email with body text, like below.
Problem:
1)I have different variables that need to insert into the mail.body. But it can only show the first variable "name
".
2)How can I leave the space or shift between lines in mail.body text?
mail.Body = string.Format("Thank you for online maintanence {0}. Your details is as following: Model:{0} colour:{0}Storage:{0}type of maintanence:Also , Your choose {0} for your phone retake location Any queries , Please contact 16681668 to our office! ", name ,clientid,model,colour,storage,type,Location,contactno);
Upvotes: 0
Views: 2999
Reputation: 32693
I would take a different approach if I were you. Generating formatted emails from code like that is just ugly. And using String.Format()
to insert your values into your template? Ugh, that's a mess if you ever need to change the order or add new items!
There is a tool called Postal that allows you to use the Razor layout syntax to generate emails. This promotes separation of concerns, your logic for sending the email is no longer coupled to the logic that determines the layout for the email body. It's easier to write, because you won't need to write HTML inside a string inside C#. I'll show you how to do it with a strongly typed model.
public class MailInfo : Email
{
public string Name {get; set;}
public string ClientId {get; set;}
public string Model {get; set;}
public string Colour {get; set;}
public string Storage {get; set;}
public string Type {get; set;}
public string Location {get; set;}
public string ContactNo {get; set;}
}
The MailInfo
will hold all the data we want to use to generate the email. We would create an instance of the class and populate it.
var email = new MailInfo();
email.Name = "Bob";
email.ClientId = "A57C";
//set rest of properties
Then we create the Razor layout (.cshtml) file, and the layout file is going to declare that it needs an instance of our MailInfo
class on the first line. Sine we did that, we can embed properties from MailInfo
class into your email by using the @Model.PropertyName
syntax.
@model MailInfo
<p>Thank you for online maintanence @Model.Name. Your details are as follows:</p>
<ul>
<li>Model: @Model.Model</li>
<li>Colour: @Model.Colour</li>
<li>Storage: @Model.Storage</li>
<li>Type: @Model.Type</li>
</ul>
<p>Also, you choose @Model.Location for your phone retake location. Any questions, please contact our office at 16681668!</p>
All that's left is to create the MailMessage
, passing the instance of MailInfo
that we created above, and send it.
MailMessage mail = new EmailService().CreateMailMessage(email);
//send mail like you would any other MailMessage, populating To, From, Subject etc.
(I did simplify the code slightly, not showing using
declarations, where to put the .cshtml file, or how to install Postal via NuGet. Let me know if you want me to flesh the answer out more.)
Upvotes: 1
Reputation: 1115
You can also use this approach:
mail.IsBodyHtml = true;
mail.Body += "This is some text";
mail.Body += "<br />";
mail.Body += "This a variable: " + variableName;
<br />
would insert the line break.
Upvotes: 0
Reputation: 592
What you can do is using '@' character before the string in order to split the text on multiple lines.
string mailTemplate = @"Thank you for online maintanence {0}. <br />
Your details is as following: <br />
Model:{0} <br />
colour:{0} <br />
Storage:{0} <br />
type of maintanence: <br />
Also , Your choose {0} for your phone retake location Any queries ,
Please contact 16681668 to our office! ";
mail.Body = string.Format(
mailTemplate,
name,
clientid,
model,
colour,
storage,
type,
Location,
contactno);
make sure that you set the IsBodyHtml property to true.
mail.IsBodyHtml = true;
Upvotes: 0
Reputation: 98740
Because you say so. You always used {0}
for every variable that's why it always shows the first one.
Increase your {0}
index every time by 1
. Like;
mail.Body = string.Format("Thank you for online maintanence {0}. Your details is as following: Model:{1} colour:{2} Storage:{3} type of maintanence: Also, Your choose {4} for your phone retake location Any queries , Please contact 16681668 to our office! ",
name ,clientid,model,colour,storage,type,Location,contactno);
In such a case, your type,Location,contactno
variables will be unnecessary since you didn't define them any {}
in your string.
Upvotes: 2