BigSi
asked on
Sending Email in jsp?
Hi,
I have just successfully set up a email server on my pc which is working with the tomcat send mail example. I would like to know what is the best (preferably simplest) way to send email by using jsp pages? Is it possible to do it by accessing a java bean, as I am using one which stores the properties of the email I would like to send out (such as the to, from addresses & also the content.
Thanks for your help,
Si
I have just successfully set up a email server on my pc which is working with the tomcat send mail example. I would like to know what is the best (preferably simplest) way to send email by using jsp pages? Is it possible to do it by accessing a java bean, as I am using one which stores the properties of the email I would like to send out (such as the to, from addresses & also the content.
Thanks for your help,
Si
ASKER
thanks cheekycj,
you wouldn't happen to know the methods I should be looking at to do this?
-Si
you wouldn't happen to know the methods I should be looking at to do this?
-Si
are you familiar with the email code itself?
What you want to do is create an EmailerBean.java that will have attributes like to, from, subject, body, cc, bcc and then a method called send()
send() should actually send the email using the attributes set.
CJ
What you want to do is create an EmailerBean.java that will have attributes like to, from, subject, body, cc, bcc and then a method called send()
send() should actually send the email using the attributes set.
CJ
There are a couple of ways of setting up addresses and the body of the message. Here is one way:
String emailReceipient = "someone@anywhere.com";
String bccRecipient = null;
String from = "senders.address@anywhere. com";
String subject = "Email Message";
String Txtmessage = "Some message here";
try {
// Configure our mailhost
java.util.Properties props=new java.util.Properties();
props.put("mail.host","127 .0.0.1");
props.put("mail.debug", "false");
Session mailConn=Session.getInstan ce(props,n ull);
// Build our message
Message msg= new MimeMessage(mailConn);
// Set who the message receipients are
msg.setRecipient(Message.R ecipientTy pe.TO,
new javax.mail.internet.Intern etAddress( emailRecei pient));
if ( bccRecipient != null && bccRecipient.length() > 0 ) {
msg.setRecipient(Message.R ecipientTy pe.BCC,
new javax.mail.internet.Intern etAddress( bccRecipie nt));
}
// Set who our sender is
msg.setFrom(new javax.mail.internet.Intern etAddress( from));
// Set the subject line
msg.setSubject(subject);
// Add our message content
BodyPart bp=new MimeBodyPart();
bp.setText(Txtmessage);
Multipart mp=new MimeMultipart();
mp.addBodyPart(bp);
// Send our message
msg.setContent(mp);
javax.mail.Transport.send( msg);
} catch(Exception em) {
System.out.println("Execpt ion trying to send eMail: \n"
+ em.toString());
em.printStackTrace();
}
String emailReceipient = "someone@anywhere.com";
String bccRecipient = null;
String from = "senders.address@anywhere.
String subject = "Email Message";
String Txtmessage = "Some message here";
try {
// Configure our mailhost
java.util.Properties props=new java.util.Properties();
props.put("mail.host","127
props.put("mail.debug", "false");
Session mailConn=Session.getInstan
// Build our message
Message msg= new MimeMessage(mailConn);
// Set who the message receipients are
msg.setRecipient(Message.R
new javax.mail.internet.Intern
if ( bccRecipient != null && bccRecipient.length() > 0 ) {
msg.setRecipient(Message.R
new javax.mail.internet.Intern
}
// Set who our sender is
msg.setFrom(new javax.mail.internet.Intern
// Set the subject line
msg.setSubject(subject);
// Add our message content
BodyPart bp=new MimeBodyPart();
bp.setText(Txtmessage);
Multipart mp=new MimeMultipart();
mp.addBodyPart(bp);
// Send our message
msg.setContent(mp);
javax.mail.Transport.send(
} catch(Exception em) {
System.out.println("Execpt
+ em.toString());
em.printStackTrace();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for responding, sorry im a little slow to responf back to the thread :(
- I have been trying to use java beans to send the mail, but I dont know where to start as im usure about which classes to import.
Ive noticed that refrences to the 'javax.mail' has been made.
I've tried importing this class into my bean for use but it cant be found when I compile it - is there anything i need to allow it to be recognised?
- I have been trying to use java beans to send the mail, but I dont know where to start as im usure about which classes to import.
Ive noticed that refrences to the 'javax.mail' has been made.
I've tried importing this class into my bean for use but it cant be found when I compile it - is there anything i need to allow it to be recognised?
ASKER
The following code sends mail when used in a jsp page, allthough im still trying to get it to work using a bean :(
<%@ page import="sun.net.smtp.SmtpC
<%
String from="FROM ADDRESS HERE";
String to="TO ADDRESS HERE";
try{
SmtpClient client = new SmtpClient("mail.yoursmtps
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("To: " + to);
message.println("Subject: Sending email from JSP!");
message.println("This was sent from a JSP page!");
message.println();
message.println("some text");
message.println();
message.println();
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e);
}
%>
ASKER
YAY - I've converted that code to work in a bean now.
I appreciate your help with this topic, but I will award Binary1 the points as he provided code which was similar to what I was looking for - I hope thats ok!
For future refrence my bean looked something like:
import sun.net.smtp.SmtpClient;
import java.io.*;
public boolean SendMail()
{
String from="FROM ADDRESS HERE";
String to="TO ADDRESS HERE";
try{
SmtpClient client = new SmtpClient("mail.yoursmtps erver.net" );
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("To: " + to);
message.println("Subject: Sending email from JSP!");
message.println("This was sent from a JSP page!");
message.println();
message.println("some text");
message.println();
message.println();
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e);
}
return true
}
(theres probably errors in there as I just quickly modified it but I hope it helps anyone intrested!)
Si
I appreciate your help with this topic, but I will award Binary1 the points as he provided code which was similar to what I was looking for - I hope thats ok!
For future refrence my bean looked something like:
import sun.net.smtp.SmtpClient;
import java.io.*;
public boolean SendMail()
{
String from="FROM ADDRESS HERE";
String to="TO ADDRESS HERE";
try{
SmtpClient client = new SmtpClient("mail.yoursmtps
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("To: " + to);
message.println("Subject: Sending email from JSP!");
message.println("This was sent from a JSP page!");
message.println();
message.println("some text");
message.println();
message.println();
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e);
}
return true
}
(theres probably errors in there as I just quickly modified it but I hope it helps anyone intrested!)
Si
try this:
package com.yourcompany
import sun.net.smtp.SmtpClient;
import java.io.*;
public class EmailBean {
private String from
private String to;
EmailBean() { }
public void setFrom(String from) {
this.from = from;
}
public void setTo(String to) {
this.to = to;
}
public String getFrom() {
return this.to;
}
public String getTo() {
return this.from;
}
public boolean sendMail()
{
String from="FROM ADDRESS HERE";
String to="TO ADDRESS HERE";
try{
SmtpClient client = new SmtpClient("mail.yoursmtps erver.net" );
if (from == null || from.trim().length == 0) {
System.out.println("Invali d From address");
return false;
}
client.from(from);
if (to == null || to.trim().length == 0) {
System.out.println("Invali d to address");
return false;
}
client.to(to);
PrintStream message = client.startMessage();
message.println("To: " + to);
message.println("Subject: Sending email from JSP!");
message.println("This was sent from a JSP page!");
message.println();
message.println("some text");
message.println();
message.println();
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e.getMessage());
return false;
}
return true
}
Usage:
<jsp:useBean id="emailer" scope="page" class="com.yourcompany.Ema ilBean" />
<jsp:setProperty name="to" property="you@yahoo.com" />
<jsp:setProperty name="from" property="me@yahoo.com" />
<%
if (emailer.sendMail()) {
out.print("success");
}
%>
HTH,
CJ
package com.yourcompany
import sun.net.smtp.SmtpClient;
import java.io.*;
public class EmailBean {
private String from
private String to;
EmailBean() { }
public void setFrom(String from) {
this.from = from;
}
public void setTo(String to) {
this.to = to;
}
public String getFrom() {
return this.to;
}
public String getTo() {
return this.from;
}
public boolean sendMail()
{
String from="FROM ADDRESS HERE";
String to="TO ADDRESS HERE";
try{
SmtpClient client = new SmtpClient("mail.yoursmtps
if (from == null || from.trim().length == 0) {
System.out.println("Invali
return false;
}
client.from(from);
if (to == null || to.trim().length == 0) {
System.out.println("Invali
return false;
}
client.to(to);
PrintStream message = client.startMessage();
message.println("To: " + to);
message.println("Subject: Sending email from JSP!");
message.println("This was sent from a JSP page!");
message.println();
message.println("some text");
message.println();
message.println();
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e.getMessage());
return false;
}
return true
}
Usage:
<jsp:useBean id="emailer" scope="page" class="com.yourcompany.Ema
<jsp:setProperty name="to" property="you@yahoo.com" />
<jsp:setProperty name="from" property="me@yahoo.com" />
<%
if (emailer.sendMail()) {
out.print("success");
}
%>
HTH,
CJ
I would still award the pts to Binary1
CJ
CJ
Make sense?
CJ