Failed to send UTF-8 email using delphi indy

Here is my code

Email body has some Unicode characters

LSMTP := TIdSMTP.Create(nil); try LMsg := TIdMessage.Create(LSMTP); try with LMsg do begin Subject := Subj; Recipients.EMailAddresses := Email; WriteToLog(cInformation,'To: '+Recipients.EMailAddresses); From.Address := ReplaceVariables(From_Address); From.Name := ReplaceVariables(From_Name); Body.Text := EmailMessage; ContentTransferEncoding := '8bit'; CharSet := 'UTF-8'; ContentType := 'text/plain'; end; 

And this is what I get

 Content-Type: text/plain; charset=us-ascii <<<<< WRONG Content-Transfer-Encoding: 8bit Date: Fri, 23 Mar 2012 17:53:19 +0000 

Using delphi 2009

+8
source share
3 answers

This is by design. When the ContentType property ContentType set, the property installer can update the CharSet property with a CharSet value if the input does not explicitly indicate the encoding. Certain types of content, especially in the text/ field, have certain default character set values ​​defined in different RFCs. Indie is trying to follow these rules as best as possible. Thus, you need to set the CharSet property CharSet desired value after you set the ContentType property, as you have already discovered:

 //LMsg.CharSet := 'UTF-8'; LMsg.ContentType := 'text/plain'; LMsg.CharSet := 'UTF-8'; 

You can also do this instead:

 LMsg.ContentType := 'text/plain; charset=UTF-8'; 

UPDATE : as of July 23, 2019, ContentType property ContentType now retain the corresponding value of the CharSet property if it is already set and the character set is not specified in the new ContentType value. Thus, the order in which the ContentType + CharSet paired properties ContentType CharSet is no longer a problem.

+14
source

Got it working. The order of events is very important.

It works:

 LMsg.ContentType:='text/plain'; LMsg.CharSet:='UTF-8'; 

This is not true:

 LMsg.CharSet:='UTF-8'; LMsg.ContentType:='text/plain'; 
+3
source

In my case, if I add an attachment, I should specify only the encoding:

pMsg->CharSet = "UTF-8";

Another email reader shows the source code of the mail.

+2
source

All Articles