Hi Inge,
In both cases the string is just of type Text. I cannot see anything about character set anywhere.
In my codeunit that works fine, I just do this:
SMTPSetup.GET;
SMTP.CreateMessage('From me', 'from@gmail.com', 'to@gmail.com', 'Løn: Test mail', 'My body text', FALSE);
SMTP.AppendBody('Hej<br><br>');
SMTP.AppendBody('<b>Her er en lønmail</b><br>');
SMTP.TrySend;
In the codeunit from Lessor Payroll, they do this:
SMTPMail.CreateMessage(txtSenderName, txtSenderAdr, txtEmplMailAdr, txtMailSubject, txtMailText + txtMailSignature, FALSE);
SMTPMail.AddAttachment(txtSecondFileName, txtShowFileName);
IF SMTPMail.TrySend THEN BEGIN...
My codeunit works fine all the time. It does not matter if I send TRUE og FALSE as the last parameter to CreateMessage(). And it does not matter if I use the Send method or the TrySend method.
When I debug the code from Lessor Payroll, the variable "txtMailSubject" is always correct with a value from the database like "Lønseddel for perioden 01-08-22..31-08-22".
The codeunit 400 looks like this:
[External] CreateMessage(SenderName : Text;SenderAddress : Text;Recipients : Text;Subject : Text;Body : Text;HtmlFormatted : Boolean)
OnBeforeCreateMessage(SenderName,SenderAddress,Recipients,Subject,Body,HtmlFormatted);
IF Recipients <> '' THEN
CheckValidEmailAddresses(Recipients);
CheckValidEmailAddresses(SenderAddress);
SMTPMailSetup.GetSetup;
SMTPMailSetup.TESTFIELD("SMTP Server");
IF NOT ISNULL(Mail) THEN BEGIN
Mail.Dispose;
CLEAR(Mail);
END;
SendResult := '';
Mail := Mail.SmtpMessage;
Mail.FromName := SenderName;
Mail.FromAddress := SenderAddress;
Mail."To" := Recipients;
Mail.Subject := Subject; <-- at this point the variable "Subject" is correct.
Mail.Body := Body; <-- it is not possible to check if Mail.Subject is correct.
Mail.HtmlFormatted := HtmlFormatted;
IF HtmlFormatted THEN
Mail.ConvertBase64ImagesToContentId;
[External] TrySend() : Boolean
OnBeforeTrySend;
SendResult := '';
Password := SMTPMailSetup.GetPassword;
WITH SMTPMailSetup DO
SendResult :=
Mail.Send(
"SMTP Server",
"SMTP Server Port",
Authentication <> Authentication::Anonymous,
"User ID",
Password,
"Secure Connection");
Mail.Dispose;
CLEAR(Mail);
EXIT(SendResult = '');
I run my codeunit directly from the Object Designer, using the "Run" button. I run the Lessor Payroll code from inside Business Central. It is the same user.