UPDATE I tested this with Excel documents instead of PDFs, and it worked. Why won't it work with PDFs??
I am trying to add X amount of attachments to an e-mail by iterating over a list of file path strings. Instead I get X attachments, but they're all copies of the same file. So the amount of attachments is correct, but their content is not.
This happens even though:
- the PDF files are saved correctly on disk.
- whilst debugging, the list of file paths is correct.
- whilst debugging, file names and file paths are distinct.
I am using FluentEmail to send e-mails. Also, I am building upon the following example (see 'Multiple attachments' section).
I added Debug.WriteLine(path) just to check if I am able to correctly output the strings in the loop, which I were.
My method:
public async void SendInvoicesEmail() {
try {
using (SqlConnection con = new(ConnectionString.connectionString))
using (SqlCommand cmd = new("query text", con)) {
con.Open();
SmtpSender sender = new SmtpSender(() => new SmtpClient(host: "smtp.office365.com") {
EnableSsl = true,
UseDefaultCredentials = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("email", "password"),
Port = 587
});
Email.DefaultSender = sender;
IFluentEmail newEmail = Email
.From("email")
.To("email")
.Subject("subject text")
.Body("body text");
var attachList = new List<FluentEmail.Core.Models.Attachment>();
// SourcePathList contains all the file paths I need to reference.
foreach (string path in SourcePathList) {
string fileName = Path.GetFileName(path);
var attachment = new FluentEmail.Core.Models.Attachment {
Data = File.OpenRead(path),
ContentType = "application/pdf",
Filename = $"{fileName}"
};
attachList.Add(attachment);
Debug.WriteLine(path);
}
newEmail.Attach(attachList);
SourcePathList.Clear();
FluentEmail.Core.Models.SendResponse result = await newEmail.SendAsync();
if (result.Successful) {
cmd.ExecuteNonQuery();
}
}
} catch (Exception ex) {
MessageBox.Show(ex.Message, "Message", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
I have tried:
- Using
newEmail.AttachFromFilename. - Using
newEmail.Sendinstead of.SendAsync. - Iterating over array instead of list.
- Changed my SMTP host.
- Tried using
ContentTypefor older PDFs (even though I have updated software):"application/x-pdf".
But none of the above made any difference.
The three items present are distinct, yet only the last item is added as file in the 3 attachments. Does ContentId matter?

