2014-09-02 49 views
0

我有一個HTML頁面,其內容從Cookie本地加載。如何在本地加載HTML頁面並通過javamail通過電子郵件發送內容?

我要寄的頁面內容,因爲它是使用我的Gmail帳戶的JavaMail

我的HTML頁面

<html> 
<head> 
    <script src="js/simplecartcheckout.js" type="text/javascript" charset="utf-8"></script> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
</head> 
<body> 
       <h2>Order</h2> 
       <div class="simpleCart_items" > 
       </div> 
       <br></br> 

      SubTotal: <span id="simpleCart_total" class="simpleCart_total"></span> <br /> 
    Tax Rate: <span id="simpleCart_taxRate" class="simpleCart_taxRate"></span> <br /> 
    Tax: <span id="simpleCart_tax" class="simpleCart_tax"></span> <br /> 
    Shipping: <span id="simpleCart_shipping" class="simpleCart_shipping"></span> <br /> 
    -----------------------------<br /> 
    Final Total: <span id="simpleCart_grandTotal" class="simpleCart_grandTotal"></span> 
</body> 
</html> 

當我在本地主機上的內容運行這個頁面是從餅乾中的數據加載。

我想作爲電子郵件發送加載的HTML頁面內容到我的Gmail帳戶

但是,當我把代碼中MimeMessage.setContent()方法JavaMail;我收到一個空白郵件沒有內容。

我想要我的郵件中的所有內容,而不將其轉換爲任何其他文件格式或作爲附件。

我的電子郵件代碼

package com.kunal.servlet; 

import java.io.IOException; 
import java.util.*; 

import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 





/** 
* Servlet implementation class CartCheckout 
*/ 
@WebServlet("/CartCheckout") 
public class CartCheckout extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public CartCheckout() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 

    } 

    /** 
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
// TODO Auto-generated method stub 
     try 
     { 
      Properties props = new Properties(); 
      props.put("mail.smtp.host", "smtp.gmail.com"); 
      props.put("mail.smtp.socketFactory.port", "465"); 
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
      props.put("mail.smtp.auth", "true"); 
      props.put("mail.smtp.port", "465"); 

      Session session = Session.getDefaultInstance(props, 
        new javax.mail.Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(user,password); 
        } 
         }); 

      Message message=new MimeMessage(session); 
      message.setFrom(new InternetAddress(user)); 
      message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(Receiver)); 
      message.setSubject("Agro Test"); 
      message.setContent("<html>\n" + 
        "<head>\n"+ 
        "<script src=\"js/simplecartcheckout.js\" type=\"text/javascript\" charset=\"utf-8\"></script>\n"+ 
        "<script src=\"//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js\"></script>\n"+ 
        "</head>\n"+ 
        "<body>\n" + 
        "<h2>Order</h2>" + 
        "<div class=\"simpleCart_items\" >\n"+ 
        "</div>" + 
        "<br></br>\n" + 
        "SubTotal: <span id=\"simpleCart_total\" class=\"simpleCart_total\"></span> <br />\n" + 
        "Tax Rate: <span id=\"simpleCart_taxRate\" class=\"simpleCart_taxRate\"></span> <br />\n"+ 
        "Tax: <span id=\"simpleCart_tax\" class=\"simpleCart_tax\"></span> <br />\n"+ 
        "Shipping: <span id=\"simpleCart_shipping\" class=\"simpleCart_shipping\"></span> <br />\n"+ 
        "-----------------------------<br />\n"+ 
        "Final Total: <span id=\"simpleCart_grandTotal\" class=\"simpleCart_grandTotal\"></span>"+ 
        "</body>\n" + 
        "</html>", "text/html"); 
      Transport.send(message); 
      System.out.println("message sent...."); 
     } 
     catch(MessagingException ex) 
     { 
      ex.printStackTrace(); 
     } 
     catch(Exception ex) 
     { 
      ex.printStackTrace(); 
     } 
} 
+0

它只適用於平面文本嗎? – Stultuske 2014-09-02 10:54:59

+0

是的!但是顯示了整個未處理的代碼。我想要處理所有值的代碼。 – 2014-09-02 11:02:42

回答

0

這個答案How do I send an HTML email?解釋你的HTML不應該包含<html><head><body>

另外我不確定script將被支持。

+0

該腳本包含所有值。 我該怎麼做才能使它工作? – 2014-09-02 11:09:55

+0

這是不可能的。在發送之前,您必須找到一種方法來評估和構建您的靜態html。 電子郵件不支持動態html。 – 2014-09-02 11:12:20

+0

沒錯。您的html頁面似乎要求瀏覽器執行頁面引用的所有腳本。即使電子郵件閱讀器恰好在瀏覽器中運行,也不太可能讓電子郵件閱讀器執行此操作。 – 2014-09-02 19:56:27

相關問題