email encoding

Hi everyone

I’m having a hard time encoding my email body and subject.
whatever I encode, the are missrepresented. It appears as if it where re-encoded further down the line.
My code:

[code]// Send the Email
MailSocket.Address = “asmtp.mail.xxx.ch”
MailSocket.Port = 465
MailSocket.ConnectionType = SMTPSecureSocket.TLSv1
MailSocket.SMTPConnectionMode = SMTPSecureSocket.ModeSSLTLS
MailSocket.Secure = True

MailSocket.Username = Username
MailSocket.Password = Password

// Create EmailMessage
Dim mail As New EmailMessage
mail.FromAddress = “info@me.ch”

mail.AddRecipient(sMailTo)
mail.Subject = sSubject.ConvertEncoding(Encodings.UTF8) // and tried other encodings
mail.BodyPlainText = sMessage.DefineEncoding(Encodings.UTF8)
mail.Headers.AppendHeader(“X-Mailer”,“SMTP Test”)

// AttachmentVar mail As New EmailMessage
for each oAttachment as Emailattachment in aroAttachment
mail.Attachments.Append(oAttachment)
next

// Send it
MailSocket.Messages.Append(mail)
MailSocket.SendMail
[/code]

thanks for tips

where is the header defining UTF-8 encoding for the body?

Did you try CURLEMailMBS class instead?

The email functionality in Xojo is kinda simple. UTF8 isn’t enough for emails. CURL is a good idea or the Chilkat plugin.

The subject for instance needs a special encoding like this one here:

emails are supposed to contain ASCII only, pretty much. If you want to send UTF8 you need to encode things such as the subject lines as Beatrix suggests. See here:

https://en.wikipedia.org/wiki/MIME

and read the section on encoded words.

The same applies to the body of your mail, you’ll have to encode it as base64 or quoted-printable, and send a Content-Transfer-Encoding: header (see the MIME reference above) to tell the other end what you’ve done. I’ve not looked at the Xojo email class, so I don’t know what it supports regarding all this. For my email client, written in Xojo, I handled all this myself.

thanks everyone. I got it working:

[code]// Send the Email
MailSocket.Address = “asmtp.xxx.ch”
MailSocket.Port = 465
MailSocket.ConnectionType = SMTPSecureSocket.TLSv1
MailSocket.SMTPConnectionMode = SMTPSecureSocket.ModeSSLTLS
MailSocket.Secure = True

MailSocket.Username = Username
MailSocket.Password = Password

// Create EmailMessage
Dim mail As New EmailMessage
mail.Headers.AppendHeader(“Mime-Version:”,“1.0”) // for subject line
mail.Headers.AppendHeader(“Content-Transfer-Encoding:”, “quoted-printable”) // for body
mail.FromAddress = “info@xxx.ch”
mail.AddRecipient(sMailTo)

mail.Subject = “= ?iso-8859-1?Q?” + EncodeForMail(sSubject) + “?=” // Subject needs start and end for encoding
mail.BodyPlainText = EncodeForMail(sMessage)

// Send it
MailSocket.Messages.Append(mail)
MailSocket.SendMail[/code]

then the method that does the encoding:

[code]Public Function EncodeForMail(sEMailText as string) as String
// encode

// disassemble to Characters
Var chArray() As String
chArray = sEMailText.Split("") // produces array of individual characters

for i as integer = 0 to chArray.Ubound
select case chArray(i)
case “”
chArray(i) = “=C4”
case “”
chArray(i) = “=E4”
case “”
chArray(i) = “=F6”
case “”
chArray(i) = “=D6”
case “”
chArray(i) = “=DC”
case “”
chArray(i) = “=FC”
'…

  'case else  // all others stay untouched
end select

next ’ Char

// re-assemble String
sEMailText = String.FromArray(chArray, “”)

return sEMailText
End Function
[/code]

it’s not quite done. “” and “” need distinguishing, that’s next

Thanks all!

String comparison is case insensitive. You may need to code it like

select case 0
case strcomp(chArray(i), "", 0)
   chArray(i) = "=C4"
case strcomp(chArray(i), "", 0)
    chArray(i)= "=E4"
...
end select

That will evaluate all the strcomp until one results in zero.

Why don’t you use one of the methods Xojo provides for encoding such as EncodeBase64 or EncodeQuotedPrintable? Then you won’t need to write your own method.