+3 votes

I’m sending emails via SMTP. My attachment is in UNC format. The email goes out but WITHOUT the attachment. If I change the UNC name to c:...., the file goes out.

Any ideas what could cause this? I assume it has something to do with access rights. My dll (actually a service) is running on the server when I send the email.

Any help would be appreciated.


I'm using the following code:

If fAttachment1 IsNot Nothing AndAlso File.Exists(fAttachment1) Then
    builder.AddAttachment(fAttachment1)
End If

Maybe System.IO.File.Exists is causing the issue.

by
edited
How do you read those files from disk?

1 Answer

0 votes

Generally I don't think this is Mail.dll issue.

When you use MailBuilder class to create an email message:

MailBuilder builder = new MailBuilder();
builder.From.Add(new MailBox("alice@mail.com", "Alice"));
builder.To.Add(new MailBox("bob@mail.com", "Bob"));
builder.Subject = "Test";
builder.Text = "This is plain text message.";

// Read attachment from disk, add it to Attachments collection
MimeData attachment = builder.AddAttachment(@"../image.jpg");

IMail email = builder.Create();

using (Smtp smtp = new Smtp())
{
    smtp.Connect("server.example.com");   // or ConnectSSL for SSL
    smtp.UseBestLogin("user", "password");   // remove if not needed

    smtp.SendMessage(email);

    smtp.Close();
}

You use:

MimeData attachment = builder.AddAttachment(@"../image.jpg");

You can check if attachment.Data contains any data after the call to this method.

Internally MailBuilder.AddAttachment uses MimeData.LoadFromFile method, which in turn uses following code:

using (FileStream stream = File.OpenRead(path))
{
     return new BinaryReader(stream).ReadBytes(stream.Length);
}

I don't think there can be a bug in this code.

Please double check the file name (with extension), maybe this is just a simple mistake.

by (301k points)
...