Sending Email with Attachments from C#, Attachments Arrive as Part 1.2 in Thunderbird

Sending email with attachments from C#, attachments arrive as Part 1.2 in Thunderbird

Explicitly filling in the ContentDisposition fields did the trick.

if (attachmentFilename != null)
{
Attachment attachment = new Attachment(attachmentFilename, MediaTypeNames.Application.Octet);
ContentDisposition disposition = attachment.ContentDisposition;
disposition.CreationDate = File.GetCreationTime(attachmentFilename);
disposition.ModificationDate = File.GetLastWriteTime(attachmentFilename);
disposition.ReadDate = File.GetLastAccessTime(attachmentFilename);
disposition.FileName = Path.GetFileName(attachmentFilename);
disposition.Size = new FileInfo(attachmentFilename).Length;
disposition.DispositionType = DispositionTypeNames.Attachment;
message.Attachments.Add(attachment);
}

BTW, in case of Gmail, you may have some exceptions about ssl secure or even port!

smtpClient.EnableSsl = true;
smtpClient.Port = 587;

Sending e-mail with attachment C#

I have found a solution

The problem was in Russian letters in the file name. Without it everything works fine.

Sending email with attachments; attachment arrives without extension in Microsoft Outlook

Okay, after some debugging I realized the mistake was with the name being passed as an argument in Attachment(...). The second argument should recieve the name of the file with the extension (e.g. "file.txt"), and my variable, ma.FileName, only had the name by itself. So even if I specified the MIME type in the third argument, the method didn't know the type of file it's supposed to handle. Or at least Outlook didn't.

att = new Attachment(new MemoryStream(ma.FileContent), ma.FileName + ma.FileType, ma.FileType.GetMimeType());

Adding the extension solved the problem.

Plain Text not showing when sending an email with an attached file in C#

Though your question doesn't seem to contain example code that demonstrates how you set the plaintext, try

  • setting it as the main body + the HTML as an alternateview
  • setting both the text and the HTML as alternate views, no main body
  • setting the text as both the main body and a text alternateview as well as having an HTML alternateview

I'd be interested to know which of these works out for thunderbird..

Send MailMessage With MailMessage as attachments

So at the end of the day it was the Transfer Encoding that caused the problem. Specifying it as Eight bit and ensuring the attachment also had the right media type solved the problem.

MailMessage mm1 = new MailMessage();
mm1.IsBodyHtml = true;
mm1.Body = ReportMsgBody;
mm1.Subject = "Home Owner's Insurance Policy Proofs: " + lData.Select(x => x.FileName).First();
mm1.From = new MailAddress("insurance@email.com", "FromName");
mm1.ReplyTo = new MailAddress("insurance@email.com");
mm1.To.Add(receiver.EmailAddress);

foreach (NewBusinessData item in lData)
{
MailMessage mm = new MailMessage();
mm.IsBodyHtml = true;
mm.Body = HTMLBody;
mm.Subject = "Home Owner's Insurance Policy";
mm.From = new MailAddress("insurance@email.com", "FromName");
mm.ReplyTo = new MailAddress("insurance@email.com");
mm.To.Add(item.EmailAddress);

byte[] thisAttachment;
thisAttachment = Common.Attach(Settings.Default.FileWriterPath + item.PolicyNumber + "_" + item.MortgageLoanAccountNumber + ".pdf");
Stream ClientPDF = new MemoryStream(thisAttachment);
Attachment attClientPDF = new Attachment(ClientPDF, item.Pr + ".pdf", "application/pdf");
mm.Attachments.Add(attClientPDF);

byte[] thisAttachment2;
thisAttachment2 = Common.Attach(Settings.Default.StaticAttatchmentPath + "Home Owner's Insurance Policy.pdf");
Stream StaticPDF = new MemoryStream(thisAttachment2);
Attachment attStaticPDF = new Attachment(StaticPDF, "Home Owner's Insurance Policy.pdf", "application/pdf");
mm.Attachments.Add(attStaticPDF);

Assembly assembly = typeof(SmtpClient).Assembly;
Type mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
MemoryStream stream = new MemoryStream();

ConstructorInfo mailWriterContructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream) }, null);
object mailWriter = mailWriterContructor.Invoke(new object[] { stream });
MethodInfo sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic);
sendMethod.Invoke(mm, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true, true }, null);

stream.Seek(0, SeekOrigin.Begin);
Attachment emailAtt = new Attachment(stream, "Home Owner's Insurance Policy", "message/rfc822");
emailAtt.TransferEncoding = System.Net.Mime.TransferEncoding.EightBit;
mm1.Attachments.Add(emailAtt);
}

Sending email with attachments using Amazon AWS SMTP

I think your problem probably is that you are not adding your attachments correctly to your message.

I was successful sending a single message with an attachment. I started with the code that was taken directly from your source link above. I then added the code from another SO article about the missing content type problem.

The attachment is a Word Document Lebowski.docx from my Documents folder. I suggest you create a similar simple Word document and run the following code to verify you can send a simple attachment in a single email.

The following code worked successfully for me:

namespace SESTest
{
using System;
using System.Net.Mail;

namespace AmazonSESSample
{
class Program
{
static void Main(string[] args)
{
const String FROM = "from-email"; // Replace with your "From" address. This address must be verified.
const String TO = "to-email"; // Replace with a "To" address. If your account is still in the
// sandbox, this address must be verified.

const String SUBJECT = "Amazon SES test (SMTP interface accessed using C#)";
const String BODY = "This email and attachment was sent through the Amazon SES SMTP interface by using C#.";

// Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials.
const String SMTP_USERNAME = "user-creds"; // Replace with your SMTP username.
const String SMTP_PASSWORD = "password"; // Replace with your SMTP password.

// Amazon SES SMTP host name.
const String HOST = "your-region.amazonaws.com";

// The port you will connect to on the Amazon SES SMTP endpoint. We are choosing port 587 because we will use
// STARTTLS to encrypt the connection.
const int PORT = 2587;

// Create an SMTP client with the specified host name and port.
using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT))
{
// Create a network credential with your SMTP user name and password.
client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);

// Use SSL when accessing Amazon SES. The SMTP session will begin on an unencrypted connection, and then
// the client will issue a STARTTLS command to upgrade to an encrypted connection using SSL.
client.EnableSsl = true;

// Send the email.
try
{
Console.WriteLine("Attempting to send an email through the Amazon SES SMTP interface...");

var mailMessage = new MailMessage()
{
Body = BODY,
Subject = SUBJECT,
From = new MailAddress(FROM)
};
mailMessage.To.Add(new MailAddress(TO));

//REMOVE THIS CODE
//System.IO.MemoryStream ms = new System.IO.MemoryStream(attchmentRec.ssSTAttachment.ssBinary);
//Attachment attach = new Attachment(ms, attchmentRec.ssSTAttachment.ssFilename);
//mailMessage.Attachments.Add(attach);

System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Octet;
contentType.Name = "Lebowski.docx";
mailMessage.Attachments.Add(new Attachment("C:\\users\\luis\\Documents\\Lebowski.docx", contentType));

client.Send(mailMessage);

Console.WriteLine("Email sent!");
}
catch (Exception ex)
{
Console.WriteLine("The email was not sent.");
Console.WriteLine("Error message: " + ex.Message);
}
}

Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
}
}

Once you prove you can send an email with one attachment from your account, then you can start working on how to get the binaries from your ssAttachmentsList into an your emails and the correct content type assigned to each. But you didn't provide enough code or context for me to determine that at this time. Im hoping this code will help you get over your attachment issue.

Thunderbird doesn't show embedded images in email

As Scarnet pointed out, this is the right way to place an embedded image into an html email:

AlternateView view = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html);
LinkedResource inline = new LinkedResource(filename, MediaTypeNames.Image.Jpeg);
inline.ContentId = "image@email";
view.LinkedResources.Add(inline);

MailMessage mail = new MailMessage();
mail.From = new MailAddress(username);
mail.To.Add(address);
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.AlternateViews.Add(view);
await smtpClient.SendMailAsync(mail);


Related Topics



Leave a reply



Submit