Using XOJO to email binary attachments

I’m writing a mass-mailing program using SMTPSecureSockets - I have no desire to receive email, so this is a send-only program. I read my email with Google or Apple Mail. My problem is with binary email attachments. One example in the XOJO documentation says to set the MIMEType of the attachment. This didn’t work - it was unreadable upon receipt. Another example tells me to use EncodeBase64 and explains that that’s how SMTP was designed, but they don’t give an example on how to use that with attachments. I can see only one way to use EmailAttachment, and that is with the LoadFromFile method. I can’t read a file, encode it as a string, then attach my encoded string. I’ve Googled extensively, but all of the “solutions” I’ve found use deprecated or 3rd party solutions.

Can anyone help me attach binary files?

The EmailAttachment class in Xojo can only do Base64 attachments.
But in general it works, so what are you doing?

I wrote ‘Bulk Email’ in the MAS using the MBS CURL email and it can attach as many emails as you want plus it supports secure SSL email such as via GMail and iCloud Mail. The biggest problems I have found are from people trying to use the wrong port number for secure emails from their ISP.

There’s also another mass email app created in RB/Xojo called MaxProg ( which does more than mine, but you may be able to get some hints from these.

you could also use my new CURLEmailMBS class from one of the last releases.
It helps building the email with correct text encoding and attachments.

Scott Boss is writing an article for xDev Magazine about using Xojo with Mandrill. It’s a great way to send out a lot of customized email very quickly, reliably without building up you own infrastructure. The article might be out in the next issue. In the mean time check out Mandrill, and then PM Scott if you want some Xojo code snippets.

I appreciate so many comments so quickly! First, I got the “mass mailing” part working, so I’m sure the port and such are correct. After all of that was working, I added a text field to accept a file name to attach. I pretty much copied the example from the documentation:

Dim mail As New EmailMessage
If fileField.Text <> “” Then
Dim file As New EmailAttachment
End If

That “worked” in that the email was sent and I received it with an attachment, but I got errors that either it was the wrong file type or it was corrupted. I then found another example somewhere in the Xojo documentation that simply specified a MIMEtype, so I tried something like this:

FileAttach = New EmailAttachment
FileAttach.LoadFromFile (fileField.Text)
FileAttach.MIMEType = “image/png”
MAIL.Attachments.Append FileAttach

It worked the same - I got the attachment, but it wouldn’t open with the same errors as before. I then found other articles and blogs where the EncodeBase64 was mentioned as being a necessity, but it takes a string as an argument and returns a string. The EmailMessage.Attachments.Append takes a FolderItem, not a string - so I couldn’t figure out how to make that work. What I finally did, was read the file into a string, encode the string, write that string to a temporary file, then attach that new file. I got the exact same errors. I spent some time with this, experimenting with various parameters (such as not relying on defaults but specifying that I was reading a UTF8 file and writing a Base64 file, and also playing with the line length of the EncodeBase64 routine - but nothing made a difference.

So in summary - the attachment is being sent - it’s just getting “changed/corrupted” somewhere.

I’m sending mails with attachments, too, but I’m not in front of my code at the moment. You shouldn’t have to do anything at all because this works out of the box. Especially you don’t need to do anything with base64 encoding as this is handled internally by Xojo. I’ll post my code in the evening.

Did you have a look at the mail that you got back? How does it differ from the mail you sent?

Be careful — just because it is working for one email does not mean it will work for 1000!

I’m sure you’re aware of this, but you need to check with each ISP what your SMTP email limits are: total number of emails per hour, total number of recipients per email, total number of recipients in all emails per hour and time delay between each email sent. Apart from this there are also restrictions on the size of attachments, the number of attachments, the format of attachments, the blocking of zipped zip files, and how are your attachment is embedded inside your email. This data can vary from one ISP to another. Apart from this there are also legal requirements such as including an opt-out/unsubscribe link in your email.

Even if you follow all the rules, it is possible for your email address to be blacklisted, so don’t use your main email address when sending bulk email. Even if your own SMTP server doesn’t block you, it is possible for other SMTP servers to add you to the blacklist. And if lots of your recipients mark you as junk email then you may get blacklisted as well!

Beatrix Willius - looking forward to seeing your code. I agree that everything I read says it should work out of the box, but when I had problems, I found that others have had problems, so went crazy trying lots of different things. And it sounds stupid now, but no - I never compared the sent/received versions. I know only that it was a PNG file that wasn’t a PNG file when received. Since this is the first time I’ve ever tried to write a program that uses SMTP, I just assumed the problem was all in my code.

David Cox - Thanks for the warning, but that’s under control. I’m emailing 20 teams of up to 10 people per team. I’ve been using Gmail, and most of the time I send email to individual teams which fits their SMTP limit of 99. When I’ve had to send email to the entire group, I’ve been using Gmail on the Web without issues. This program will automatically break up that email into several 90-person messages to fit the SMTP limit, so that’s all I’m really trying to do. “Mass” email sounds like SPAM, but in this case, it’s just twice their SMTP limit, and only happens about 8 times per year.

Oh blech, I forgot. Need to make a knot in the ear (write myself a mail).

Here is my code:

[code] 'create some gzip files first (not included)
dim theAttachment as new EmailAttachment
theAttachment.LoadFromFile(tempFile) 'gzip file
theAttachment.Name = theFile.Name + “.gz” 'theFile is the original folderitem
theAttachment.MIMEType = “application/gzip”

 'create a mail first (not included)
  MailToCompany.Attachments.Append theAttachment

'now process the mail[/code]

This sends me all session logs from my application, which reside in ApplicationSupport. The maximum as far as I remember was about 50 attachments I got.

The mails are sent out with SendGrid which allows you to send up to 200 mails per day on the free plan.


That looks an awful lot like what I tried originally, except I didn’t use the “.Name” parameter, and I was using a MimeType of “image/png”. I was certainly receiving my “test.png” file with the correct name - it’s just that no program I had would open the received file, only the one before sending. So you never even had to identify the original encoding (UTF-8?) on sending? I think I tried that, too - but to be honest, when it didn’t work the first time, I started adding and overriding default parameters - so I certainly could have screwed things up. I’ll try this again when I get back home. I’ll let you know - thanks!


WOO HOO! That worked! I’ve got to start keeping EVERY version of programs I write, because I swear this looks an awful lot like what I had already tried in one of my 1000+ attempts! But after you said it should work out-of-the-box, I was sure I had simply changed or missed one particularly important parameter. I just wonder what it was? (I didn’t need the “.Name” parameter, by the way - the extension was already in the file name, so it worked without needing that).

But I’m dancing in the streets tonight! Thank you very much!


the URL is

I have written Xojo classes for using their API. Well I have written code for parts of their API. Their API has a lot of calls and and can do a lot. I have not had time finish writing all the API calls.

Let me clean up the existing docs over the weekend and I will post the code on .

and if you need help using my classes and/or using their API, please let me know. I have spent a lot of time with it.