Justin Maniyamprayil
Justin Maniyamprayil

Reputation: 41

Sending an email through struts with java mail API

I trying to send a test mail from my struts application. i have a simple jsp page and the action corresponding to that page, i downloaded a simple code to send an email the code is as follows.

package java4s;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class mailtest {

    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("[email protected]","********");
                }
            });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("[email protected]"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("[email protected]"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler," +
                    "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

    }

This code is working an its sending mails too. I've changed it to a class so that i can create an object and call the function from my action. Like follows

public class mailtest
{

void mailSend()
{

//Same code as above
}

}

But when i create the object of this class in my action page it is giving me an exception as follows..

root cause

java.lang.NoClassDefFoundError: javax/mail/MessagingException
    java4s.mailsender.execute(mailsender.java:50) //on this line i've created object of the     mailtest class
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)

Hope you understand, please comment for more clarifications if you needed..

Upvotes: 1

Views: 2280

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29971

First, fix all these common mistakes.

I'm guessing that you're not running in a Java EE application server; Java EE application servers include JavaMail as a standard part.

If you're just running in Tomcat, you'll need to make the JavaMail jar file available to your application, either by putting it in the WEB-INF/lib directory of your war file, or putting it in Tomcat's lib directory.

Upvotes: 1

Related Questions