Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro JSP
|
Pro JSP Advanced JSP coding questions. Beginning questions will be redirected to the Beginning JSP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro JSP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 7th, 2005, 06:27 AM
Authorized User
 
Join Date: Mar 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Email using servlets

hello all,
   i want to know how to send email using servlet program?can i view an email using servlets?

bye

Sonnu

 
Old March 7th, 2005, 05:19 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can do both with beans, and am pretty sure you can do the same with servlets.

Sun have an entire forum dedicated to Java Mail, I think you will find what you are looking for there:

http://forum.java.sun.com/forum.jspa?forumID=43

 
Old March 11th, 2005, 09:29 AM
Registered User
 
Join Date: Oct 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's a bean I use to send email from a Web App. It uses an email connection defined in Tomcat, but could include the code inline (I could probably find an example if needed.) The "to" address can contain a comma delimited list.

Hope it helps

package LLBeans;

/**
 * @author EWard
 */

import java.beans.*;
import java.io.Serializable;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

public class SendMail extends Object implements Serializable
{
    public String to;
    public String from;
    public String subject;
    public String body;
    public Message message;
    public Session sessn;
    public SendMail()
    {
        try
        {
            /*
            System.out.println("****************************** *********");
            System.out.println("** Initializing SendMail **");
            System.out.println("****************************** *********");
             */
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            sessn = (Session) envCtx.lookup("mail/Session");
            /*
            System.out.println("****************************** *********");
            System.out.println("** Finished lookup **");
            System.out.println("****************************** *********");
             */
        }
        catch (Exception ex)
        {
            System.out.println("****************************** *********");
            System.out.println("From SendMail" + ex.getMessage());
            ex.printStackTrace();
            System.out.println("****************************** *********");
        }
    }

    public void setTo(String x)
    {
        to = x;
    }
    public void setFrom(String x)
    {
        from = x;
    }
    public void setSubj(String x)
    {
        subject = x;
    }
    public void setBody(String x)
    {
        body = x;
    }
    public boolean sendMail()
    {
        try
        {
            /*
            System.out.println("****************************** *********");
            System.out.println("** Trying to send mail **");
            System.out.println("****************************** *********");
             */
            message = new MimeMessage(sessn);
            message.setFrom(new InternetAddress(from));
            InternetAddress dests[] = new InternetAddress[]
            { new InternetAddress(to) };
            message.setRecipients(Message.RecipientType.TO, dests);
            message.setSubject(subject);
            message.setContent(body, "text/plain");
            Transport.send(message);
        }
        catch (Exception e)
        {
            System.out.println("From sendMail" + e.getMessage());
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
 
Old March 11th, 2005, 02:37 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried the method posted above, it was ok on my local machine but I couldnt get it to work reliably on my server so I used JavaxMail instead:

import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendApp
{

    public static void send(String from, String to, String CC, String subject, String content)
    throws AddressException, MessagingException
    {
        String smtpHost = "127.0.0.1";
        int smtpPort = 25;


        // Create a mail session
        java.util.Properties props = new java.util.Properties();
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.port", ""+smtpPort);
        props.put("mail.smtp.auth", "true");

        //If authentication is required on server you will need
        //the following line and an Authenticator class for it
        //to call
        Authenticator auth = new SimpleAuthenticator();

        Session session = Session.getDefaultInstance(props, auth);

        // Construct the message
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        msg.setRecipient(Message.RecipientType.CC, new InternetAddress(CC));
        msg.setRecipient(Message.RecipientType.BCC, new InternetAddress(CC));
        msg.setSubject(subject);
        msg.setText(content);

        // Send the message
        Transport.send(msg);
    }

}






Similar Threads
Thread Thread Starter Forum Replies Last Post
Inner classes in Servlets vikkiefd Servlets 1 April 24th, 2008 04:30 AM
Servlets achukuttan Servlets 1 March 26th, 2007 02:04 AM
a problem in servlets bala_in J2EE 1 August 10th, 2006 03:37 PM
Servlets and Tomcat Camer Servlets 1 November 25th, 2005 08:34 AM
Filters in servlets Ayusman Servlets 0 July 19th, 2005 07:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.