Hi,
in a company we are using Aspose.Email java for sending emails from our app.
Lately we have a problem with (most likely) SmtpClient and/or MailMessage classes,
basically I want to send an email which I compose from generated html (using Apose.Words), and then send it.
Problem is that while the generated HTML shows no signs of issues and basically is clean, after the email is received it contains “?” sign in the beginning right before … tag, basically this:
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">?<html><head>
instead of
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"><html><head>
One thing is that we force UTF-8 charset when setting MailMessage using
content.setSubjectEncoding(UTF8); content.setBodyEncoding(UTF8); content.setPreferredTextEncoding(UTF8);
But it arrives with Windows-1252.
I’ve debugged the code and on each line where the email body is set or somehow manipulated I never seen “?”, and because I cannot debug (obviously) the Aspose classes I assume the issue is somewhere inside them.
public void sendMessage(EmailMessage message) throws DAFException {
SmtpClient client = new SmtpClient();try { client.setHost(getGridProperties().getEmailSmtpHost()); client.setPort(getGridProperties().getEmailSmtpPort()); client.setUsername(getGridProperties().getEmailSmtpUsername()); client.setPassword(getGridProperties().getEmailSmtpPassword()); } catch (Exception fail) { } try { MailMessage content = new MailMessage(); if (getGridProperties().isEmailFromAllowOverride() && message.getFrom() != null && message.getFrom().contains("@")) { content.setFrom(getMailAddress(message.getFrom(), message.getFromName())); } else { content.setFrom(getMailAddress(getGridProperties().getEmailFromAddress(), getGridProperties().getEmailFromDisplayName())); } content.setTo( new MailAddressCollection() {{ add(message.getTo()); }} ); content.setSubject(message.getSubject().replaceAll("\\<.*?>", "")); content.setBody(message.getBody().replaceAll("\\<.*?>", "")); content.setHtmlBody(message.getHtmlBody()); content.setSubjectEncoding(java.nio.charset.StandardCharsets.UTF_8); content.setBodyEncoding(java.nio.charset.StandardCharsets.UTF_8); content.setPreferredTextEncoding(java.nio.charset.StandardCharsets.UTF_8); client.send(content); } catch (Exception fail) { } } finally { if (client != null) { client.dispose(); } }
}
I’ve removed some code unrealted to code manipulation like loggers, validation checks and such and replaced constants with magic Strings
the getGridProperties() call is just to get proper data from our application server.
I forgot about important thing, I’ve also tested adding the HTML directly instead of it being generated and the “?” wa still there when email was received.