4

I am currently working on a c# application where I am making my own Email Server that listens for SMTP traffic including attachment data and then find the MX record the email address and forward it on to the recipient.

This is all working perfectly except for one strange problem that I do not understand.

The issue only affects running the C# program on Linux under Mono, everything is fine on Windows.

The problem is if I have my email server program running on port 26 (port 25 is already in use) I then have a test c# program on my windows PC which sends a file. I then get the attachment data successfully write the attachment to a temporary file, recreate the attachment object and send the attachment with the message. When the email is received, the email content no longer exists and the attachment is called noname and if I view the attachment it has part of the email headings and the base 64 string which makes up the attachment.

However, If i then change the program to run on port 25, everything is obiously done in exactly the same way, but this time when the email arrives in my gmail account, the email is perfectly in tact with the message body and the attachment with the correct name and format. I don't understand why running my program on linux under a different port would cause this issue. I've checked the headers between working and non working and everything appears to be fine.

Is this an issue with mono. I have also tried setting the port to port 25 for the smtp client for when I am sending it on the MX record but doesn't make any different. Below is how I am sending the attachment.

FileStream fileStream = new FileStream(attachmentTempName, FileMode.Open, FileAccess.Read);
Attachment attachment = new Attachment(fileStream, attachments[0].realFileName, MediaTypeNames.Application.Octet);
message.Attachments.Add(attachment);
message.From = new MailAddress(emailInfo["EmailFrom"]);
                    message.To.Add(emailInfo["EmailTo"]);
                    message.Subject = emailInfo["Subject"];
                    if (emailInfo["Headers"] != "")
                    {
                        message.Headers.Add(getHeaders(emailInfo["Headers"]));
                    }
                    message.Body = emailInfo["Body"];
                    message.Body = "This is a test hardcoded";
                    if (emailInfo["EmailFormat"] == ManageEmail.EmailFormat.HTML.ToString())
                    {
                        message.IsBodyHtml = true;
                    }
                    else
                    {
                        message.IsBodyHtml = false;
                    }

                    SmtpClient smtp = new SmtpClient(mxRecords[0]);
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtp.Port = 25;
                    smtp.Send(message);

Thanks for any help you can provide.

Boardy
  • 35,417
  • 104
  • 256
  • 447
  • I'm taking a look, but it seems to have something to do with non-Windows OS... http://turadg.aleahmad.net/2007/10/psa-when-you-receive-noname-attachments/ http://www.drslewis.org/grant/2008/01/25/gmail-noname-attachment-bug/ , here's a possible solution: http://stackoverflow.com/questions/6657363/using-mail-mime-to-send-attachment-to-gmail-receiving-noname-attachment , here's the tool: http://www.jquery.info/noname/ – RandomUs1r Mar 07 '13 at 23:42
  • @RandomUs1r Thanks, it doesn't look as if this is exactly the same as I can't change the extension as the content is completely wrong. I've tried setting the content-type in the SO post you linked previously but doesn't make any difference. Its very strange – Boardy Mar 07 '13 at 23:47
  • Hmmm... have you tried setting the encoding? http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.bodyencoding.aspx – RandomUs1r Mar 07 '13 at 23:57
  • or even http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/d9429eb3-f4b5-40ba-8e9e-b31702110d40/ the content type's encoding? I kind of getting what's going on and that's gmail is not recognizing the base 64 string. The tool I linked you is supposed to convert those noname attachments, I just haven't found a resource explaining exactly what's going on. One consistency so far has been non-Windows systems. – RandomUs1r Mar 07 '13 at 23:59
  • and these guys are talking about MIME http://stackoverflow.com/questions/10343106/linux-mail-file-log-has-content-type-application-octet-stream-a-noname-attac all things to try :) – RandomUs1r Mar 08 '13 at 00:00

1 Answers1

0

@Boardy,

The only thing that caught my attention was the following line:

Attachment attachment = new Attachment(fileStream, attachments[0].realFileName, MediaTypeNames.Application.Octet);

The second parameter of the Attachment constructor should be the ContentType, not the file name.

 message.Attachments.Add(new Attachment(imageFile.InputStream, imageFile.ContentType, MediaTypeNames.Image.Jpeg));

For more information follow this link: C# Sending Email with attached file (image)

Cheers.

Community
  • 1
  • 1
Eulogy
  • 197
  • 1
  • 1
  • 14