Emails received have "?" sign at the beginning


#1

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.


#2

@Jan89,

I have observed the issue shared by you and request you to please first try using latest Aspose.Email for Java 19.7 on your end. In case the issue still persist then share the working sample example along with source file (if any) reproducing the issue on your end that we may try to reproduce on our end.


#3

Thank you for your response, I’ve tested it with Aspose.Email 19.7 but the issue is still there.

Can you tell me what exactly do you need for your investigation? I’ve posted the code we are using in my original post so perhaps something else?


#4

@Jan89,

Can you please source email file which you used on your end so that we may further investigate to help you out.


#5

beforeAndAfterFiles.zip (1.6 MB)

I’ve added .zip file with one .txt and one .html files, these represent the data we use in
MailMessage content = new MailMessage(); where content is content.setHtmlBody(message.getHtmlBody());

the “message” is just POJO, getHtmlBody() returns String, however the String is exactly what I send in those two files, no ‘?’ added to it anywhere in method until
client.send(content); where client is SmtpClient client = new SmtpClient();
which I obviously cannot debug.

I’ve also attached email as it arrives already with broken html.
We use Aspose.Words for file conversion/s but this seems to not cause the problem as the html I’ve sent is being generated before the POJO/ htmlBody field even receives it and is correct.


#6

@Jan89,

I have worked with sample code shared by you. Can you please share complete working sample project. The code snippet you have shared includes some undeclared variables so would you please share SSCCE code reproducing the issue so that we may try to reproduce and investigate it in our environment.


#7

Here is pastebin with code https://pastebin.com/uF0wUR5M

I’ve noticed one thing tho, there is deprecated method in SmtpClient - setEncoding(),
when I use this like this: .setEncoding(java.nio.charset.StandardCharsets.UTF_8);
and together with the HTML hardcoded the “?” is not showing anymore.

But it still shows the “?” when I just parse generated HTML (identical to one I sent before or the one in the pasteBin code) using getter method from EmailMessage class which returns String and nothing else…


#8

@Jan89,

I have worked with your sample code for further investigation can you please share temporarily the test account credentials so that we may help you out.


#9

Unfortunately I cannot give you our test email server credentials, not only it is accessible only with VPN but I am also not allowed to share them.


#10

@Jan89,

I can understand the limitation on your end. We have created an issue with ID EMAILJAVA-34573 as investigation on our end to possibly reproduce and resolve the issue on our end. This thread has been linked with the issue so that you may be notified once the issue will be fixed.