Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sat May 27, 2017 6:24 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Encoding of multipart e-mail messages
PostPosted: Wed Jan 02, 2013 7:03 am 
Offline
User avatar

Joined: Sat Jan 29, 2011 6:24 pm
Posts: 33
Location: Germany, Europe
Hello !

I'm facing a problem with the EMailMessage class. Trying to send a plain text OR HTML message works great, even with custom headers for content type and content transfer encoding, e.g. I'm using this for plain text mails:

Mail.Headers.AppendHeader("Content-Type","text/plain; charset=UTF-8")
Mail.Headers.AppendHeader("Content-Transfer-Encoding","base64")


And later to generate the body: Mail.BodyPlainText = EncodeBase64(ReplaceLineEndings(BodyText,EndOfLine.UNIX))
Note: Replacing the line endings is required for any reason, otherwise some mail clients show the entire text as a very, very long line. When just using plain text, it is not required to do this. Any ideas why this happens ?

No additional headers are used when sending HTML mails, just Mail.BodyHTML = TABodyHTML.Text to create the message (TABodyHTML as TextArea).

However when trying to send a message that contains both, EMailMessage.TextBody and EMailMessage.HTMLBody parts, any additional headers I add myself to specify the encoding fail to work.

I see the main problem that it is impossible for me to adjust the content transfer encoding per message part (so I would use no special settings for HTML but above ones for plain text). Whatever I tried, my headers are overwritten by RB and replaced with standard ones working only with ASCII charset in a reliable way (Apple Mail works, but Thunderbird shows strange characters for German umlauts and other Unicode chars).

What have I missed ? Is there a solution to my problem ? Any notes and comments are highly appreciated (but please, no solutions involving third party plugins or classes, if possible).

Bye,
Tobias.

Happy New Year 2013 ! :-)


Top
 Profile  
 
 Post subject: Re: Encoding of multipart e-mail messages
PostPosted: Thu Jan 03, 2013 11:25 am 
Offline

Joined: Mon Jun 21, 2010 9:25 pm
Posts: 11
Location: Warfordsburg, PA
We don't rely on the RS EmailMessage class but instead use a third party control.

With that said however, we construct our headers somewhat differently have you tried:

"Content-Type: text/plain; charset=UTF-8" and
"Content-Transfer-Encoding: base64"

instead of the two separate strings in each of your append statements?

Here is a code snippet, maybe this can be of use to give you some ideas.

For X As Integer = 0 To UBound(FilesToSend)
Fi = GetFolderItem(FilesToSend(X))
Bs = BinaryStream.Open(Fi, False)
Data = Bs.Read(Bs.Length)
Data = EncodeBase64(Data)

Headers = Headers + "--" + IdString + EndOfLine.Windows
Headers = Headers + "Content-Type: " + Fi.Type + "; name=" + """" + Fi.Name + """" + EndOfLine.Windows
Headers = Headers + "Content-Transfer-Encoding: base64" + EndOfLine.Windows
Headers = Headers + "Content-Disposition: attachment; filename=" + """" + Fi.Name + """" + EndOfLine.Windows
Headers = Headers + EndOfLine.Windows + Data + EndOfLine.Windows + EndOfLine.Windows
Next X

// CLOSE THE PRIMARY BOUNDARY
Headers = Headers + "--" + IdString + "--"
Headers = ReplaceLineEndings(Headers, EndOfLine.Windows)
Headers = Headers.Trim

_________________
Regards,

Mike B.


Top
 Profile  
 
 Post subject: Re: Encoding of multipart e-mail messages
PostPosted: Fri Jan 04, 2013 9:43 pm 
Offline
User avatar

Joined: Sat Jan 29, 2011 6:24 pm
Posts: 33
Location: Germany, Europe
I see... are you going to build the e-mail from scratch ?

Well, your way solving this brought me to the idea to read the EMailMessage.Source and do a RegEx-driven search for the wrong content-type and content-transfer-encoding entries to replace them with the correct values.

But although the language reference says that EMailMessage.Source can be used to get OR set the source of an e-mail message, it appears that it is read-only, because while my RegEx-replacement works, the message's source is kept untouched. Or do you see a mistake in my example ?

Dim r As New RegEx
r.Options.Greedy = FALSE

r.SearchPattern = "Content\-type: text/plain;"
r.ReplacementPattern = "Content-Type: text/plain; charset=UTF-8" + Chr(10) + "Content-Transfer-Encoding: base64"
Mail.Source = r.Replace(Mail.Source)

MailSocket.Messages.Append Mail
...

Oh boy, why is such a great language like RB so full of small, nasty bugs making professional software development a pain ?

Tobias.


Top
 Profile  
 
 Post subject: Re: Encoding of multipart e-mail messages
PostPosted: Fri Jan 04, 2013 10:42 pm 
Offline

Joined: Mon Jun 21, 2010 9:25 pm
Posts: 11
Location: Warfordsburg, PA
Tobias,

Your code looks fine to me. Yes, we do build the entire email from scratch plus we have our own custom classes which we use for POP, IMAP and SMTP. These are based off the SSLSocket so we are not dependent on the EMailMessage class and the RS POP3 and SMTP socket classes.

Sorry I can't be of any help with the built in classes from RS.

_________________
Regards,

Mike B.


Top
 Profile  
 
 Post subject: Re: Encoding of multipart e-mail messages
PostPosted: Fri Jan 04, 2013 11:50 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
Be aware that there are some issues surrounding manipulating the message Source. Test carefully.


Top
 Profile  
 
 Post subject: Re: Encoding of multipart e-mail messages
PostPosted: Tue Jan 08, 2013 6:29 am 
Offline
User avatar

Joined: Sat Jan 29, 2011 6:24 pm
Posts: 33
Location: Germany, Europe
Thank you for your help. :-)

Yesterday, I gave it another attempt, but unfortunately I failed. Here is what I did:

I tried to read in the EMailMessage.Source into a string variable, adjusting the content type and encoding, then destroying the current EMailMessage object, initialize a new one and setting the source of this object with the previously stored one.

While anything seemed to work at first glance, RB does re-create the e-mail before sending (even a new boundary value was set), so any modifications I made were overwritten.

Tobias.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group