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", "blah@gmail.com"));
message.Subject = "Special Request";
var builder = new BodyBuilder ();
builder.TextBody = plainBody;
builder.HtmlBody = htmlBody;
message.Body = builder.ToMessageBody ();
` each time you want a new line. – Xavier Brassoud Feb 22 '22 at 16:06