Slauma
Slauma

Reputation: 177133

Sending a MailMessage with System.Net.Mail.SmtpClient: Why is the mail body attached as text file?

I am sending a mail with System.Net.Mail.SmtpClient:

MailMessage aMailMessage = new MailMessage();
aMailMessage.To.Add(aUser.Email);
aMailMessage.Subject = "aaa";
aMailMessage.Body = "bbb";

SmtpClient aSmtpClient = new SmtpClient();
aSmtpClient.Send(aMailMessage);

The Email is received as a text mail (subject "aaa" and body "bbb" as expected) but to this mail in addition a text file is attached: file name "aaa" and content "bbb".

Why is this text file attached and how can I avoid it?

Thank you for help!

Update

Ignore this question: It was apparently a temporary issue with my mail client I used to receive the test mails. The problem disappeared after a restart of the mail client.

Upvotes: 1

Views: 1145

Answers (1)

Timores
Timores

Reputation: 14589

The way an attachment is inserted in an SMTP message is the same as when there are alternate versions of the message. The message header of the main part has a MIME type meaning "multi-part" and a delimiter is defined. Then, several sub-messages are included, separated by this delimiter. Each sub-message is tagged with a MIME part defining the format of the body or of the attachment. For example, a Word file is tagged as something like "application/ms-word", while the plain text part is tagged as text/plain or HTML as text/html.

In the implementation of SmtpClient (I looked at it thanks to Reflector), if there is no attachment and no alternate version, an HTML sub-message is automatically created. Thus, an old client like Outlook 2000 thinks there is an attachment because I guess it does not support this notion of alternate views (and thus to it, the MIME type text/html is an attachment).

What is strange is that it did not happen before (or you did not notice). Maybe previous messages had an attachment or an alternate view that you had explicitly set.

Upvotes: 2

Related Questions