Sending an email attachment

Hi all,

I have created a pdf file using MBS DynaPDF plugin. After the file is created and stored locally (final version will be stored on the Xojo server) I need to attach it to an email as an attachment and send it.

When the email arrives the “Test.pdf” is attached, but it is empty (zero bytes). I have read a couple of similar situations on the forum, and tried to incorporate their suggestions to no avail. Here is the code:

Dim mail As emailMessage
Dim file As emailAttachment

MailSocket.Address = “xxxxxxxx.com
MailSocket.Port = xxxx

MailSocket.Username = “xxxxxxxxxxx”
MailSocket.Password = “xxxxxxxxxxx”

file = New emailAttachment
file.name = “Test.pdf”
file.ContentEncoding = “base64”
file.MIMEType = “application/pdf”
file.LoadFromFile GetFolderItem("/Users/xxxxx/Desktop/Test.pdf")

mail = New emailMessage
mail.FromAddress = “OrderFullfillment@Part39.com
mail.Subject = “Test Subject”
mail.BodyPlainText = “Test Body.”
mail.Headers.AppendHeader(“X-Mailer”,“Test SMTP Demo”)
mail.addRecipient(“xxxxx@xxxxx.net”)

mail.Attachments.Append(file)

MailSocket.Messages.Append(mail)
MailSocket.SendMail

Any suggestions?

Thanks in advance.

Does mail socket go out of scope right here ?
Do you poll it ?

If not then the mail is probably not sent correctly

I am not sure what you mean by “go out of scope.” The email does arrive but the attachment is empty. How do I “poll it”?

Thank you Norman.

IF this code is in a method that sends an email and the mailsocket is defined locally then at the end of the method all the local variables are destroyed

As such the email may not be completely sent

See User Guide - Book 1 Fundamentals Page 76
Or Intro to programming with Xojo about page 117

The variable are not out of scope.

Did you verify in debugger that your emailattachment has data inside?

Also MailSocket is a property of window or global?

PS: CURLEmailMBS class is the MBS replacement for sending emails.

[quote=167917:@Christian Schmitz]…

PS: CURLEmailMBS class is the MBS replacement for sending emails.[/quote]
Second that. Plus after switching to CurlEmailMBS, I got rid of the annoying Umlaut problems. As you already have MBS, I’d suggest not to waste time with the half-baken Xojo emai class, it just does not work well enough.[quote][/quote]

I may be wrong, but it does seem your attachment does not live long enough to be sent. You dim File as emailAttachment in your code. As soon as the event terminates, File ceases to exist. Turns pumpkin. Is no more. Gone. Out of scope. Since it takes a while for the attachment to send, it is not there when smtp needs it. So the suitcase is not there on arrival of the plane.

Instead of dimming File in the event, make it a window property. So it will remain alive as long as the window exists.

You may want to do the same for Mail. It works now for one single message, but if you send several, MailSocket will queue them, and the last ones will miss the boat just the same as the attachment.

I would also test the folderitem before the LoadFromFile. It is always safer to make sure it is not nil. It will prevent a possible exception.

but the attachment object is added to the attachments array. This should work.

You are right. It works here. Could it be some mail filter discarding the attachment on the server, as it often happens for corporate mail, or in the mail client ?

Or is the path to the file correct ? From what I see, a nil folderitem does not trigger an exception. This would probably be better :

dim filetosend as folderItem = GetFolderItem("/Users/xxxxx/Desktop/Test.pdf"
if filetosend <> nil and filetosend.exists then
  file.LoadFromFile )
else
  msgbox "Attachment file invalid"
end if

dim filetosend as folderItem = GetFolderItem("/Users/xxxxx/Desktop/Test.pdf)" if filetosend <> nil and filetosend.exists then file.LoadFromFile(filetosend) else msgbox "Attachment file invalid" end if
Sorry. Typed too fast, and of course the forum edit chose not to work when needed…

I just tested an invalid folderitem, it does exactly what the OP describes : the file is attached but zero byte. I would wager his path is wrong, or the file is absent. Testing like I do would prevent that.

I had the same problem (OSX 10.10)
Have tried this? (Folderitem.Pathtypeshell)

file.LoadFromFile(GetFolderItem(fileFld.Text,FolderItem.PathTypeShell))

I have been gone for a couple of days, and just now have been able to try various suggested solutions. Andreas’ solution worked.

file.LoadFromFile(GetFolderItem(fileFld.Text,FolderItem.PathTypeShell))

Thank you everybody for your input.

Hi.

I run into the same problem with empty attachments, found this solution and it works. But now my attachment has no name in the mail and shows only a ?-sign instead of the pdf(preview)-icon. When i open it (preview on mac), it shown the right filename.

Any suggestions whats missing?

seems that it has to do with the setting in mail “show as symbol” or “show in mail”. When i choose “show as symbol”, everything looks fine. Can i choose this behavior by code, so that the attachment is always embedded as symbol?

Please open a new topic. How do you set the name of the attachment? Can you post the code and the raw data of the mail? At least the part that is from the attachment.

Hi Beatrix.

Here starts the new topic: https://forum.xojo.com/20139-mail-app-attachment-as-symbol-in-within-mail