字符串格式化在发送邮件时不起作用。

huangapple go评论72阅读模式
英文:

String formatting is not working in mail sending

问题

String content = 
    "Dear user,\n" +
    "\n" +
    "\t<font face=\"Arial\">Hello</font>\n" +
    "\n" +
    "Regards,\n" +
    "Gen;";

MimeMessage message = emailSender.createMimeMessage();
String encodingOptions = "text/html; charset=UTF-8; format=flowed";
try {
    message.setHeader("Content-Type", encodingOptions);
    message.setSentDate(new Date());
    MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
    boolean isHTML = true;
    helper.setFrom(mailFrom);
    helper.setTo(to.replaceAll("\\s",""));
    helper.setSubject(subject);
    helper.setText(content, isHTML);
}

接收邮件时,您收到的邮件内容将会是:

Dear user,
    
    Hello
    
Regards,
Gen
英文:

My string is like below

String content  = 
&quot;Dear user,

	&lt;font face=&quot;Arial&quot;&gt;Hello&lt;/font&gt;

Regards,
Gen&quot;;

I am passing this content to the mail sender.

	`MimeMessage message = emailSender.createMimeMessage();
	String encodingOptions = &quot;text/html; charset=UTF-8; format=flowed&quot;;
	try{
		message.setHeader(&quot;Content-Type&quot;, encodingOptions);
		message.setSentDate(new Date());
		MimeMessageHelper helper = new MimeMessageHelper(message, true, &quot;utf-8&quot;);
		boolean isHTML = true;
		helper.setFrom(mailFrom);
		helper.setTo(to.replaceAll(&quot;\\s&quot;,&quot;&quot;));
		helper.setSubject(subject);
		helper.setText(content, isHTML);`

And this content String I am passing to the mail send like above, and it is in the format of html. When I recieve the mail I am getting the mail like below

Dear user,Hello Regards,Gen

How can I display the content in the below format.

Dear user,

	Hello

Regards,
Gen

答案1

得分: 3

因为这是 HTML,你需要自己添加换行。我不是前端开发人员,但你可以使用&lt;p&gt;标签或&lt;br/&gt;标签。

尝试类似这样的代码:

String content = 
&quot;亲爱的用户&lt;br/&gt;

   &lt;p&gt; &lt;font face=&quot;Arial&quot;&gt;你好&lt;/font&gt;&lt;/p&gt;

致意&lt;br/&gt;
Gen&quot;;
英文:

Since it's HTML you have to add the line breaks yourself. I am not a frontend developer but you can use &lt;p&gt; tag or &lt;br/&gt;.
Try something like:

String content  = 
&quot;Dear user, &lt;br/&gt;

   &lt;p&gt; &lt;font face=&quot;Arial&quot;&gt;Hello&lt;/font&gt;&lt;/p&gt;

Regards, &lt;br/&gt;
Gen&quot;;

答案2

得分: 3

以下是翻译好的内容:

这条消息正是您创建的内容。简而言之,HTML 不关心空格和换行符。您可以在线测试您的 HTML,看看它的呈现效果,例如在这里:
https://htmledit.squarefree.com/

为了使您的消息格式正确,可以尝试在以下代码行之间插入一些内容:

&lt;html&gt;
&lt;span&gt;亲爱的用户,&lt;/span&gt;&lt;br&gt;&lt;br&gt;
&lt;font face=&quot;Arial&quot;&gt;你好&lt;/font&gt;&lt;br&gt;&lt;br&gt;

&lt;span&gt;祝好,&lt;/span&gt;&lt;br&gt;
&lt;span&gt;Gen&lt;/span&gt;
&lt;/html&gt;

您需要自行添加换行符。您可能可以省略 &lt;span&gt; 标签。

英文:

The message is exactly what you've created. In short HTML doesn't care about spaces and newlines. You can test your HTML online to see how it looks, ie. here:
https://htmledit.squarefree.com/

For your message to be properly formatted, try something between the lines of:

&lt;html&gt;
&lt;span&gt;Dear user,&lt;/span&gt;&lt;/br&gt;&lt;/br&gt;
&lt;span&gt;&lt;font face=&quot;Arial&quot;&gt;Hello&lt;/font&gt;&lt;span&gt;&lt;/br&gt;&lt;/br&gt;

&lt;span&gt;Regards,&lt;/span&gt;&lt;/br&gt;
&lt;span&gt;Gen&lt;/span&gt;
&lt;/html&gt;

You need to provide breaklines yourself. You could probably omit &lt;span&gt; tags.

huangapple
  • 本文由 发表于 2020年9月4日 20:27:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63741186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定