如何在本地加载HTML页面并通过javamail通过电子邮件发送内容?

时间:2014-09-02 10:51:01

标签: java html javamail

我有一个HTML页面,其内容是从cookie本地加载的。

我想使用javamail将页面内容邮寄到我的Gmail帐户

我的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>

当我在localhost上运行此页面时,其内容将从cookie中的数据加载。

我正在尝试将加载的html页面内容作为电子邮件发送到我的Gmail帐户

但是当我将代码放入javamail中的MimeMessage.setContent()方法时,我会收到一封没有内容的空白电子邮件。

我想要邮件中的所有内容,而不是将其转换为任何其他文件格式或附件。

我的电子邮件代码

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();
        }
}

1 个答案:

答案 0 :(得分:0)

此回答How do I send an HTML email?说明您的HTML不应包含<html><head><body>

此外,我不确定是否会支持script