Javamail换行符无法保存到数据库中

时间:2023-01-16 18:07:42

I am using javamail to read mailbox and saving all the incoming mail into sql database of my application. My problem is when the mail is in String format the line breaks are not saving into db. For example the mail I am reading (that is seen in log) is like below

我正在使用javamail读取邮箱并将所有传入的邮件保存到我的应用程序的sql数据库中。我的问题是当邮件是String格式时,换行符不会保存到db。例如,我正在阅读的邮件(在日志中看到)如下所示

Hello

This is a test mail

Regards

Sender

But in database column it is saving as a continued line as-

但在数据库专栏中,它作为一个持续的行保存 -

Hello This is a test mail Regards Sender 

I am using following code for above task

我正在使用以下代码执行上述任务

if(content instanceof String){
  String body = (String)content;

  inBox.setMailContent(body);

  inBoxDAO.save(inBox);
}

I have not used mail stuff before, any suggestion will be helpful. Note this is working perfectly with multipart html format. Problem is in the case where the sender's mail is coming as plain text

我之前没有使用过邮件,任何建议都会有所帮助。请注意,这与多部分html格式完美配合。如果发件人的邮件以纯文本形式出现,则会出现问题

1 个解决方案

#1


-1  

You can use \n for new lines. you might have to unescape the string you get from the DB. The Apache Commons Lang have a method for that:

您可以使用\ n作为新行。你可能不得不从数据库中获取字符串。 Apache Commons Lang有一个方法:

http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeJava%28java.io.Writer,%20java.lang.String%29

  • it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'.
  • 它会将'\'和'n'的序列转换为换行符,除非'\'前面有另一个'\'。

So you would have simply to call

所以你只需要打电话

String content = StringEscapeUtils.unescapeJava(rs.getString("content"));

#1


-1  

You can use \n for new lines. you might have to unescape the string you get from the DB. The Apache Commons Lang have a method for that:

您可以使用\ n作为新行。你可能不得不从数据库中获取字符串。 Apache Commons Lang有一个方法:

http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeJava%28java.io.Writer,%20java.lang.String%29

  • it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'.
  • 它会将'\'和'n'的序列转换为换行符,除非'\'前面有另一个'\'。

So you would have simply to call

所以你只需要打电话

String content = StringEscapeUtils.unescapeJava(rs.getString("content"));