如何通过mailsender java发送带有html内容的电子邮件

时间:2015-10-19 14:55:34

标签: java html jsp email

我使用mailsender方法从系统向用户发送电子邮件,现在我已成功发送一个带有硬编码html标签的基本电子邮件在java代码中。但是如何在java代码中发送带有html文件而不是硬编码html标签的电子邮件?对不起我是新的。

示例当前代码index.jsp

  /*---------------EMAIl---------------*/
       FileInputStream is = new FileInputStream("/test/config.prop");
       Properties prop    = new Properties();
       prop.load(is);

       String FROM      = prop.getProperty("email");
       String TO        = EMAIL;//EMAIL; 
       String border    = "0";
       String width1    = "100%";
       String face      = "Verdana, Geneva, sans-serif";
       String size1     = "2";
       String size2     = "1";

       if(!TO.equals(""))
       { 
           String SUBJECT2 = "e-cover MyPolicy";

           String BODY  = "<html><body>"+
                  "<table width="+width1+" border="+border+">"+
                  "<tr><td width="+width1+"><font face="+face+" size="+size1+">Thank you for signing up, you're almost done!</font></td></tr>"+             
                  "<tr><td width="+width1+"><font face="+face+" size="+size1+">&nbsp;</td></tr>"+
                  "<tr><td width="+width1+"><font face="+face+" size="+size1+">Please click this link to activate your account:<br/></font></td></tr>"+                           
                  "<tr><td width="+width1+"><font face="+face+" size="+size1+">https://e-cover.com.my/verify/verification.jsp<br/></font></td></tr>"+                           
                  "<tr><td width="+width1+"><font face="+face+" size="+size1+">&nbsp;</td></tr>"+
                  "<tr><td width="+width1+"><font face="+face+" size="+size1+">(If clicking the link did not work, try copying and pasting it into your browser.)<br/></font></td></tr>"+                           
                  "<tr><td width="+width1+"><font face="+face+" size="+size1+">&nbsp;</td></tr>"+
                  "<tr><td width="+width1+"><font face="+face+" size="+size2+"><i>**Please do not reply to this email as it was automatically generated.**</i></font></td></tr>"+                   
                  "</table>"+
                  "</body></html>";
            try
            {
                mailsender.setFrom(FROM.trim());             
                mailsender.sendIt(TO.trim(), SUBJECT2, BODY);
                System.out.println("Sending......");
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
      }

我有另一个html文件。但是我如何在index.jsp中应用这个html文件内容。没有依恋。是html的内容。任何帮助将不胜感激。

示例html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>MyPolicy</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body style="margin: 0; padding: 0;">
 <table align="center" border="0" cellpadding="0" cellspacing="0">
 <tr>
    <td>
       <img src="http://www.test.com/logo.png"/>
   </td>
 </tr>
 </table>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

如果您指的是附件,请尝试参考MimeMessageHelper。您可以将其与InputStreamSource结合使用,将文件附加到电子邮件中。这是你的意思,还是你在谈论将图像嵌入电子邮件本身?

评论后修改:

有几种方法可以读取文件。我见过人们最常见的方法是使用BufferedReaderFileReader

但是如果你看一下this post,你可以从JohanSjöberg的帖子中看到你可以使用Apache Commons或Guava将HTML文件更清晰地导入为字符串。我不确定它们是否可以转移到JSP(你可以测试它)但是如果有的话,你可以使用我上面提到的早期方法。

所以你打电话:

<%@page import="java.io.FileReader"%>
<%@page import="java.io.BufferedReader"%>

在你的身体里,你会使用类似的东西:

<%
    ...
    BufferedReader reader = new BufferedReader(new FileReader("file.html"));    //make sure the path is correct regarding the server
    StringBuilder sb = new StringBuilder();
    String line;

    while((line = reader.readLine())!= null){
        sb.append(line + "\n");
    }
    String BODY = sb.toString();
    try
    {
    ...
%>

如果您需要更多帮助,请与我联系!