Reputation: 36217
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.
Upvotes: 4
Views: 1498
Reputation: 317
@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.
Upvotes: 0