Link to home
Start Free TrialLog in
Avatar of BigSi
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

Avatar of cheekycj
cheekycj
Flag of United States of America image

Using a bean would be ideal.  It will hide the details of the implementation from the JSP and all the jsp needs to worry about is setting up the bean and setting the submitted form fields to the bean and calling a simple method to send the email.

Make sense?

CJ
Avatar of BigSi
BigSi

ASKER

thanks cheekycj,

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
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.getInstance(props,null);

          // Build our message
          Message msg= new MimeMessage(mailConn);

          // Set who the message receipients are
          msg.setRecipient(Message.RecipientType.TO,
                    new javax.mail.internet.InternetAddress(emailReceipient));
          if ( bccRecipient != null && bccRecipient.length() > 0 ) {
               msg.setRecipient(Message.RecipientType.BCC,
                    new javax.mail.internet.InternetAddress(bccRecipient));
          }

          // Set who our sender is
          msg.setFrom(new javax.mail.internet.InternetAddress(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("Execption trying to send eMail: \n"
               + em.toString());
          em.printStackTrace();
     }
ASKER CERTIFIED SOLUTION
Avatar of Binary1
Binary1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BigSi

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?
Avatar of BigSi

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.SmtpClient, java.io.*" %>
<%
String from="FROM ADDRESS HERE";
String to="TO ADDRESS HERE";
try{
SmtpClient client = new SmtpClient("mail.yoursmtpserver.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);
}
%>
Avatar of BigSi

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.yoursmtpserver.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
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.yoursmtpserver.net");
if (from == null || from.trim().length == 0) {
  System.out.println("Invalid From address");
  return false;
}
client.from(from);
if (to == null || to.trim().length == 0) {
  System.out.println("Invalid 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.EmailBean" />
<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