0 votes

we have use this kind of code to send email:
...
MailBuilder builder = new MailBuilder();
byte[] file = ..;

MimeData attachment = new MimeFactory().CreateMimeData();
attachment.Data = file;
attachment.ContentType = ContentType.Parse("image/jpeg");
attachment.FileName = "test";
builder.AddAttachment(attachment);
...

The content type is successfully read, but the attachment generated in email does not have any file extension.

How can we fix this?

by

1 Answer

0 votes

Extension is not generated by Mail.dll - it is specified by the sender when you set the file name:

Change this line:

attachment.FileName = "test";

to:

attachment.FileName = "test.jpg";

You can also use MimeData SetFileName method:
SetFileName(string fileName, bool guessContentType)
Which sets content type useing the extension.

MailBuilder builder = new MailBuilder();
byte[] file = ..;
MimeData attachment = new MimeFactory().CreateMimeData();
attachment.Data = file;
attachment.SetFileName("test.jpg", true)
builder.AddAttachment(attachment);

Finally have in mind that you can load file directly from disk:

MailBuilder builder = new MailBuilder();
builder.AddAttachment("c:\\text.jpg");

This will set both: content-type and file name.

by (297k points)
edited by
...