Home > Back-end >  How to put my C# code in string to pass to an e-mail?
How to put my C# code in string to pass to an e-mail?

Time:03-17

I am using Mailkit to send an email.

So in the body of the email, I am passing data like this:

message.Body = new TextPart("plain")
{
    foreach (var item in model.Transaction)
    {
        Console.WriteLine(item.Account  "-" item.Amount "-" item.Date);
    }

    Text = @"";
};

but I wanted to put item.Account, item.Amount, item.Date in @""

How can I do that?

CodePudding user response:

You won't be able to access item.* outside of foreach*.

To create a single string from multiple strings you could use string.Join:

List<string> l = new ();
foreach (var item in model.Transaction)
{
   var fromSingleItem = $"{item.Account}-{item.Amount}-{item.Date}";
   l.Append(fromSingleItem);
}
var fromAllItems = string.Join(", ", l);

* = What would time outside of foreach mean? Would it be the first item's data, or the last one's, or from the middle?

CodePudding user response:

you should use $

$"{item.Account}, {item.Amount}, {item.Date}";

Because @ is used to escaping specials symbols

  • Related