在java中将html页面作为电子邮件发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23008529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
send html page as email in java
提问by user3492997
I am trying to send html page as message.
already send html email through message.setContent("<html><body><h1>This is actual message</h1></body></html>","text/html" );
these method .Now iwant to send a html page .
like message.setContent("street.html","text/html" );
我正在尝试将 html 页面作为消息发送。已经通过message.setContent("<html><body><h1>This is actual message</h1></body></html>","text/html" );
这些方法发送了 html 电子邮件。现在我想发送一个 html 页面。喜欢 message.setContent("street.html","text/html" );
how to send these below is my full code
如何在下面发送这些是我的完整代码
String to = "[email protected]";
String from = "[email protected]";
Properties properties = System.getProperties();
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "from2013");
}});
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("This is the Subject Line!");
message.setContent("street.html","text/html" );
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
回答by harun
You need to read the contents of "street.html" into a string to be able to send it with the content type "text/html".
您需要将“street.html”的内容读入一个字符串,以便能够以“text/html”的内容类型发送它。
回答by Rakesh Chouhan
You can try with the apache commons mail api
您可以尝试使用 apache commons mail api
http://commons.apache.org/proper/commons-email/apidocs/org/apache/commons/mail/HtmlEmail.html
http://commons.apache.org/proper/commons-email/apidocs/org/apache/commons/mail/HtmlEmail.html
回答by Dark Knight
MimeMessage.setText() method sets a default mime type of text/plain. But i guess you need text/html. This can be done using MimeMessage.setContent().
MimeMessage.setText() 方法设置文本/纯文本的默认 mime 类型。但我猜你需要 text/html。这可以使用 MimeMessage.setContent() 来完成。
You can make use of this code on java side.
您可以在 java 端使用此代码。
message.setContent(someHtmlMessage, "text/html; charset=utf-8");
回答by WApp
You Need to read the content of local file using one of DataHandler impementation. Need to use overloaded method in MiMeMessage Class API which will accept DataHandler object itself. MiMeMessage.SetContent(Object, type);
您需要使用 DataHandler 实现之一读取本地文件的内容。需要在 MiMeMessage 类 API 中使用重载方法,该方法将接受 DataHandler 对象本身。 MiMeMessage.SetContent(Object, type);
Refer to the API link and google for few samples of usage of method will give the required sloution.
请参阅 API 链接和 google 以获取一些方法使用示例将提供所需的解决方案。
回答by Gabriel Ruiu
A short one-liner for this one:
一个简短的单线:
StringWriter writer = new StringWriter();
IOUtils.copy(new FileInputStream(new File("home.html")), writer);
message.setContent(writer.toString(), "text/html");
NOTE:: IOUtils is available in the Apache Commons IOlibrary
注意::IOUtils 在Apache Commons IO库中可用
回答by MUKUL JHA
you need to change only
你只需要改变
String mess=""// it contains html code
message.setContent(mess,"text/html");
Transport.send(message);
it will work
它会工作