Forum Moderators: coopster & phranque

Message Too Old, No Replies

Java Mail with Html code

Sending message in html format

         

senthil

6:53 am on Jan 8, 2002 (gmt 0)

10+ Year Member



how to send mail in html format from jsp.
please can any one help me.

Thanks
Senthil

sugarkane

5:23 pm on Jan 8, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi senthil, welcome to wmw

Unfortunately we don't seem to have many jsp coders here. If no one steps in with an answer then I'll have a dig around, see if I can come up with something - I keep promising myself I'll take a closer look at jsp :)

Everyman

11:02 pm on Jan 8, 2002 (gmt 0)



I don't know squat about JSP, but from what I know about Java generally, it seems like you should be able to open an output pipe to sendmail and use that to send an html email. With sendmail, as you probably know, you just go:

sendmail blah-blah@blah.com

and then you send some headers like this:

To: blah-blah
Subject: blah-blah

and then you send two newlines, and start with the body.

The last line is a solitary period, and then close the pipe. Probably don't even need the period, if I remember correctly.

Now if you want html email, send these two headers before the "To:" header:

MIME-Version: 1.0
Content-Type: text/html

Any html coding in the body should come out as html in the email. If the email routines in JSP look at all like this sendmail format, just try sticking in those two extra headers.

ppg

3:52 pm on Mar 14, 2002 (gmt 0)

10+ Year Member



Sentil, a day (or 60) late and a dollar (or two) short, but at least this will be picked up if anyone does a search for mail & jsp's -
I'm not exactly a java guru but AFAIK the easiest way to do this is to use the javax.mail package. In order for it to work, you will also need the javax.activation package. download them and stick them somewhere where your CLASSPATH can see them.

here's an excerpt to show how to then send a mail from a jsp page:

firstly I would recommend you make yourself a bean something like the following:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class MailBean{

private String mailHost;
private String fromAddy;
private String toAddy;
private String subject;
private String body;
private Properties props;

public MailBean(){
mailHost = "";
fromAddy = "";
toAddy = "";
subject = "";
body = "";
props = new Properties();
}
.
.
.
(getter and setter methods)
.
.
.
public void send(){

boolean sent = false;
Properties props = new Properties();
props.setProperty("mail.smtp.host", mailHost);

Session ssn = Session.getDefaultInstance(props);
ssn.setDebug(true);

try{
InternetAddress from = new InternetAddress(fromAddy);
InternetAddress to = new InternetAddress(toAddy);
Message msg = new MimeMessage(ssn);
msg.setFrom(from);
msg.addRecipient(Message.RecipientType.TO, to);
msg.setSubject(subject);
msg.setContent(body, "text/plain");
Transport.send(msg);
sent = true;
}
catch(Exception e){
e.printStackTrace();
}
}
}

then access it from your jsp page:

<jsp:useBean id="mailer" class="MailBean" scope="request" />

.
.
.
mailer.setMailHost("localhost");
mailer.setToAddy(to);
mailer.setFromAddy(from);
mailer.setSubject(subject);
mailer.setBody(sb.toString());
mailer.send();
.
.
.

this example is from a jsp page I had running under apache and tomcat on a linux box, accessing sendmail on the same box.

As far as I'm aware to send HTML you would just change the setContent() parameters - either way there's an html example comes with the javax.mail package I think.

If that was about as clear as mud heres a couple of links which should help:

[developer.java.sun.com...]

[jspinsider.com...]

sugarkane

4:00 pm on Mar 14, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for that ppg, and welcome to wmw :)

ppg

11:52 pm on Mar 14, 2002 (gmt 0)

10+ Year Member



cheers sugarKane, the site looks like a great resource. I can see I'll be hanging around