我有一個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();
}
}
它只適用於平面文本嗎? – Stultuske 2014-09-02 10:54:59
是的!但是顯示了整個未處理的代碼。我想要處理所有值的代碼。 – 2014-09-02 11:02:42