how to mail the html file using javamail?

hi

i have captured the output of a jsp page in a html file....

now i wish to send it in the mail ( *** not as an attachment)

ie

i wish the output of the jsp ie the html file to be displayed in

the message box (ie text area) ..... and then when recieved by the

receipient it shoud be displayed in the message area....

is there any way to do this ........

[406 byte] By [BRAVOa] at [2007-11-27 10:54:36]
# 1

Wow, what Google can find:

http://www.google.com/search?&q=javamail+html+mail

Search yourself next time.

CeciNEstPasUnProgrammeura at 2007-7-29 11:51:13 > top of Java-index,Java Essentials,Java Programming...
# 2

well as said by my fellow poster googling could have given you quick results.

Anyways,if you are unable to do so checkout the below code snippet.

SendMail.java:

===========

import java.io.File;

import java.io.FileReader;

import java.io.BufferedReader;

import java.util.Properties;

import javax.mail.Session;

import javax.mail.Message;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import javax.activation.*;

/**

* @Author RaHuL

* uses SMTP to send Email.

*/

public class SendMail {

/** Method used to read contents of html/text/XML file */

public String readFile(String fileName){

StringBuffer sb = new StringBuffer();

BufferedReader input = null;

try{

input = new BufferedReader(new FileReader(fileName));

String str = null;

while ((str = input.readLine()) != null){

sb.append(line);

}

}catch(Exception exp){

exp.printStackTrace();

}finally{

try {

if (input!= null)

input.close();

}catch (Exception ex) {

ex.printStackTrace();

}

}

return sb.toString();

}

/** Send an HTML Email with the specific subject to specfic toAddress based on specified fromAddress & Smtp Hostname

* and returns true on success.

*

*/

public boolean sendMail(String toAddress,String fromAddress,String hostName,String subject,String htmlMessage,String contentType) {

boolean flag = false;

try {

String to = toAddress;

String from = fromAddress;

String host = hostName;

// Setting Mail properties

Properties props = new Properties();

props.put("mail.smtp.host",host);

props.put("mail.debug","true");

// Getting a New Instance Generated by the API

Session session = Session.getInstance(props);

/*

NOTE: a Mail Session could be configured and could be got from the Container via JNDI

which could be a better practise.

*/

// Instantiate a message

Message msg = new MimeMessage(session);

//Set message attributes

msg.setFrom(new InternetAddress(from));

InternetAddress[] address = {new InternetAddress(to)};

msg.setRecipients(Message.RecipientType.TO, address);

// Setting Subject Type

msg.setSubject(subject);

msg.setSentDate(new Date());

// Setting Content Type

msg.setContent(contentType);

// Set message content

msg.setText(htmlMessage);

//Send the message

Transport.send(msg);

flag = true;

}catch (Exception mex) {

// Prints all nested (chained) exceptions as well

mex.printStackTrace();

}

return flag;

}

public static void main(String args[]){

SendMail sm = new SendMail();

String htmlMessage = sm.readFile("myHtmlFile.html");

boolean flag = sm.sendMail("xyz@xyz.com","zyx@zyx.com","your.smtp.server","EMail SubJect",htmlMessage,"text/html");

if(flag)

System.out.println("MAIL SENT SUCCEFULLY");

else

System.out.println("MAIL SENDING FAILURE");

}

}

NOTE:

=====

Make sure you inculde javamail & java activation libraries.

Hope that might help :)

REGARDS,

RaHuL

RahulSharnaa at 2007-7-29 11:51:13 > top of Java-index,Java Essentials,Java Programming...