Sending an email through struts with java mail API

风格不统一 提交于 2020-01-05 07:23:30

问题


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("snapshareadm@gmail.com","********");
                }
            });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("snapshareadm@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("justinpayyans@gmail.com"));
            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..


回答1:


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.



来源:https://stackoverflow.com/questions/26815382/sending-an-email-through-struts-with-java-mail-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!