'How to get Multiline string

How does one store a statement in C# such as the following way

string EmailBody = "Request no.- ('" + objUserModel.ID + "') has been raised for special 
                            vehicle by requester-( '" + RequesterInfo + "'). " + 
                            "ORG_Unit:'"+ objUserModel.OrgUnit + "'" +
                            "TDC:'"+ objUserModel.TDC + "'" +
                            "Customer Name:'"+ objUserModel.CustName + "'" +
                            "Supply Plant:'"+ objUserModel.CustName + "'";

I am trying to show this variable in multilines in the email for that I need to store it in multiline in the string.

I have seen variations of @$"" but it is not working for me.



Solution 1:[1]

I suggest joining lines with required delimiter, e.g.

// Set "<br/>" if you want HTML delimiter  
// Put "\n" to have a new line, 
// Use Environment.NewLine to have OS specific delimiter
string delimiter = "<br/>";

string EmailBody = string.Join(delimiter,
  $"Request no.- ('{objUserModel.ID}') has been raised for special vehicle by requester-( '{RequesterInfo}'). ",
  $"ORG_Unit:'{objUserModel.OrgUnit}'",
  $"TDC:'{objUserModel.TDC}'",
  $"Customer Name:'{objUserModel.CustName}'",
  $"Supply Plant:'{objUserModel.CustName}'"
);

Solution 2:[2]

You can create a multiline string using a verbatim string, ie one starting with @. $ is used for string interpolation on both normal and verbatim strings. BUT that doesn't mean they'll appear as you expect in an email body.

Answering your specific question, this will create a multiline string:

    string EmailBody = $@"Request no.- ('{objUserModel.ID}') has been raised for special 
                        vehicle by requester-'({RequesterInfo}'). 
                        ORG_Unit:'{objUserModel.OrgUnit}'
                        TDC:'{objUserModel.TDC}'
                        Customer Name:'{objUserModel.CustName }'
                        Supply Plant:'{objUserModel.CustName}'";

Printing this produces:

Request no.- ('3') has been raised for special 
                            vehicle by requester-'(123456'). 
                            ORG_Unit:'Blah'
                            TDC:'XYZ'
                            Customer Name:'Gone'
                            Supply Plant:'Gone'

All tabs and spaces were included in the string. To avoid this, each line should start without indenting:

        string EmailBody = @$"Request no.- ('{objUserModel.ID}') has been raised for special 
vehicle by requester-'({RequesterInfo}'). 
ORG_Unit:'{objUserModel.OrgUnit}'
TDC:'{objUserModel.TDC}'
Customer Name:'{objUserModel.CustName }'
Supply Plant:'{objUserModel.CustName}'";

This produces:

Request no.- ('3') has been raised for special 
vehicle by requester-'(123456'). 
ORG_Unit:'Blah'
TDC:'XYZ'
Customer Name:'Gone'
Supply Plant:'Gone'

The order of the operators isn't significant. @$ behaves the same as $@.

Sending well formatted Emails

That's a different question, whose answer starts with Don't Use .NET's SmtpClient. The documentation page of the class warns :

We don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols. Use MailKit or other libraries instead. For more information, see SmtpClient shouldn't be used on GitHub.

The code needed to send a message with MailKit is similar to the old SmptClient.

using (var client = new SmtpClient ()) 
{
    await client.ConnectAsync("smtp.gmail.com", 465, SecureSocketOptions.SslOnConnect);
    await client.AuthenticateAsync("username", "password");
    await client.SendAsync(message);

    await client.DisconnectAsync(true);
}

The Create Messages page shows how to create plain text or HTML messages. Some older email clients don't support HTML so it's often necessary to create both a plain text and HTML message.

String interpolation and verbatim strings can be used to create the text for both parts:

string plainBody = @$"Request no.- ('{objUserModel.ID}') has been raised for special 
vehicle by requester-'({RequesterInfo}'). 
ORG_Unit:'{objUserModel.OrgUnit}'
TDC:'{objUserModel.TDC}'
Customer Name:'{objUserModel.CustName }'
Supply Plant:'{objUserModel.CustName}'";

string htmlBody = @$"<div>Request no.- ('{objUserModel.ID}') has been raised for special 
vehicle by requester-'({RequesterInfo}').
</div>
<dl>
    <dt>ORG_Unit:</dt><dd>'{objUserModel.OrgUnit}'</dd>
    <dt>TDC:</dt><dd>'{objUserModel.TDC}'</dd?
    <dt>Customer Name:</dt><dd>'{objUserModel.CustName }'</dd?
    <dt>Supply Plant:</dt><dd>'{objUserModel.CustName}'</dd>
</dl>";

These can be combined in a single email message with :

var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Blah Blah", "[email protected]"));

message.Subject = "Special Request";

var builder = new BodyBuilder ();
builder.TextBody = plainBody;
builder.HtmlBody = htmlBody;

message.Body = builder.ToMessageBody ();

Solution 3:[3]

Try to use + Environment.NewLine where you need the newline.

Solution 4:[4]

+ "vehicle by requester - ('" + RequesterInfo + "'). " + "\n" + "ORG_Unit:'" + objUserModel.OrgUnit + "'" + "\n"
+ "TDC:'" + objUserModel.TDC + "'" + "\n" + "Customer Name:'" + objUserModel.CustName + "'" + "\n"
+ "Supply Plant:'" + objUserModel.CustName + "'";```

If i have understood it correctly you just add ```+ "\n" +```

Solution 5:[5]

using string interpolation you can achieve what you want to do here for example:

string EmailBody = $"Request no.- ('\"{objUserModel.ID}\"') has been raised 

notice the \ before the "

if you would like to add a new line in the string you can just add string1 $" this string is writen on one line"; string2 $" this string is \nwriten on two lines";

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 GetGeeky
Solution 4 br0mmie
Solution 5 Palisar