用HTML发送电子邮件内容 [英] Sending Email Content in HTML
问题描述
我必须发送一封电子邮件,其中包含所有html内容,并且可以HTML形式显示在电子邮件中.我可以使用Spring Framework的 JavaMailSenderImpl
和 SimpleMailMessage
发送电子邮件,但是我发送的电子邮件显示为纯HTML文本,如下所示:
I have to send an email having all content in html that can be displayed in email as a HTML. I am able to send the email with JavaMailSenderImpl
of Spring Framework with SimpleMailMessage
but the email I send is displayed in plain html text like following
<html><body><h1>Hello</h1></body></html>
,而不是以HTML页面的形式.
and not in form of HTML page.
请说明如何将其作为HTML发送以及如何以HTML形式显示.
Please tell the way how can i send it as HTML and how it can be displayed in form of HTML.
推荐答案
If you are using java mail directly, you need to set the content type to html using the setContent() method.
MimeMessage.setContent("<html> <body><h1>Hello </h1> </body></html>", "text/html");
或者如果您使用的是Spring框架的 MimeMessageHelper ,您可以使用
Or if you are using Spring framework's MimeMessageHelper you can use MimeMessageHelper.setText(emailContent,true) method. The boolean true
flag indicates html content. For instance:
mimeMessageHelper.setTo("some@someone");
mimeMessageHelper.setReplyTo("some@someone");
mimeMessageHelper.setFrom("some@someone");
mimeMessageHelper.setSubject("someSubject");
mimeMessageHelper.setText("<html> <body><h1>Hello </h1> </body></html>",true);
这篇关于用HTML发送电子邮件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!