Wrox Programmer Forums
|
Apache Tomcat General discussion of the Apache Tomcat servlet container. For discussions specific to the Professional Apache Tomcat book, please see the book discussion forum for that book.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Apache Tomcat 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 December 30th, 2004, 06:02 AM
Registered User
 
Join Date: Dec 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Javamail

Iam looking an example some
javamail with forms.I know how to send
a message but not forms using Api java.
Best regards.

 
Old December 31st, 2004, 03:13 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

To use the code below you will need to get a copy of the java mail and activation API's and set your compiler up to use them.

There are 2 methods, one is the method you want which accepts parameters, the other is only for testing and uses strings in place of the variables.

If using tomcat you can compile this class and use it as a Java Bean, passing values from a JSP page to the bean.

I have also included an Authenticator class, I did not need this, but you may have to use it depending on your server settings.

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
    {
        //You may need to change the IP below
        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");

        // you may need authentication on your server:
        // I have included this class below this one
        // 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));

// You will need to add another parameter for BCC, I just tested it with the cc parameter

        msg.setRecipient(Message.RecipientType.BCC, new InternetAddress(CC));
        msg.setSubject(subject);
        msg.setText(content);

        // Send the message
        Transport.send(msg);
    }
    public static void main(String[] args) throws Exception
    {
        // Send a test message with no variable parameters
        // this will let you know if the API is working

        send("From [email protected]", "To [email protected]", "cc [email protected]", "Subject re: yourSubject", "Content A test message");
    }
}

+++++++++++++++++++++++++++++++++++++++++

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

public class SimpleAuthenticator extends Authenticator
{
    public PasswordAuthentication getPasswordAuthentication()
    {
        String username, password;
        username = "user";
        password = "pass";

        return new PasswordAuthentication(username, password);
    }
}






Similar Threads
Thread Thread Starter Forum Replies Last Post
javamail instaling problem rasmikanta JSP Basics 0 August 17th, 2006 05:52 AM
Javamail + form(javabean) octave J2EE 0 February 23rd, 2006 05:37 AM
Problems with JavaMail 1.3.3 dgh BOOK: Beginning Cryptography with Java 0 September 7th, 2005 05:25 PM
I can't install javamail amou JSP Basics 2 June 23rd, 2004 09:50 PM
related to javamail anishde J2EE 0 January 17th, 2004 09:31 AM





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