0

I have an HTML Page whose contents are loaded locally from cookies.

I want to mail the page contents as it is to my gmail account using javamail

My html Page

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

When I run this page on localhost its contents are loaded from the data in cookies.

I am trying to send the loaded html page contents as email to my gmail account

But when I put the code in MimeMessage.setContent() method in javamail I receive a blank email without the contents.

I want all contents in my mail, without converting it to any other file format or as an attachment.

My Email Code

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 Answers1

0

This answer How do I send an HTML email? explains that your HTML should not contain the <html>, <head> or <body>.

Also I'm not sure the script will be supported.

Community
  • 1
  • 1
Xavier Delamotte
  • 3,519
  • 19
  • 30
  • The script contains all the values. What should I do in order to get it working? – Vinay Joshi Sep 02 '14 at 11:09
  • It is not going to be possible like that. You'll have to find a way to evaluate and build your static html before sending it. Dynamic html is not supported for an email. – Xavier Delamotte Sep 02 '14 at 11:12
  • Right. Your html page seems to require that the browser execute all the scripts referenced by the page. It's unlikely you'll get an email reader to do that, even if the email reader happens to be running in a browser. – Bill Shannon Sep 02 '14 at 19:56