Reputation: 1
Environment: Windows 10,VisualStudio 2017,C# Mailkit/Mimekit version 2.0.1
PROBLEM:Text/Plain Attachment content show up in body as well as in attachment. I do not won't attachment content to show up in body.
I am trying to create mail,using MimeKit.BodyBuilder class like this:
var mail = new MailMessage();
mail.To.Add(new MailboxAddress("name1","[email protected]");
mail.From.Add(new MailboxAddress("name2","[email protected]");
var builder = new BodyBuilder();
builder.textBody = "";
buider.Attachments.Add(file);
mail.body = builder.ToMessageBody(); //PROBLEM shows up here.
when I send this mail, iff attached file is of Text/Plain it is showing up in the body also. Note this problem doesn't occur when the attachment is xml,json.
Note, I am not explicitly setting content type as I am following the sample program provided in the MimeKit documentation.
I am enclosing BodyBuilder::ToMessageBody() relevant code below for your ready reference.
BEGIN CODE - MIMEKIT.....
public MimeEntity ToMessageBody ()
{
MultipartAlternative alternative = null;
MimeEntity body = null;
if (!string.IsNullOrEmpty (TextBody)) {
var text = new TextPart ("plain");
text.Text = TextBody;
if (!string.IsNullOrEmpty (HtmlBody)) {
alternative = new MultipartAlternative ();
alternative.Add (text);
body = alternative;
} else {
body = text;
}
}
....
if (Attachments.Count > 0) {
var mixed = new Multipart ("mixed");
if (body != null)
mixed.Add (body);
foreach (var attachment in Attachments)
mixed.Add (attachment);
body = mixed;
}
return body ?? new TextPart ("plain") { Text = string.Empty };
}
END CODE...MIMEKIT....
I'll appreciate someone can point out that problem.
thank you in advance,
regards Atish.
Upvotes: 0
Views: 1762
Reputation: 38538
The problem is that you aren't setting a text or html body, so the receiving client shows the text attachment because it has nothing else to show.
I've changed the code so that if you set the TextBody
to string.Empty
, it just adds an empty inline text part as the message body, so now receiving clients should just show an empty body.
Upvotes: 2