运行java SendMail时的HTTP状态404

时间:2015-11-29 16:06:42

标签: java eclipse tomcat

我收到以下错误:

address bar: http://localhost:8080/EmailNew/WEB-INF/classes/SendMail.java

HTTP Status 404 -
type Status report
message
description The requested resource is not available.

Apache Tomcat/8.0.28

从Eclipse EE和Tomcat 8运行以下代码。

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Authenticator;
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 SendMail {

    public static void main(String[] args) {
        Properties props = new Properties();

    /*  props.setProperty("mail.host", "smtp.gmail.com");
        props.setProperty("mail.smtp.port", "587");
        props.setProperty("mail.smtp.auth", "true");
    */
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "587");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");




        Authenticator auth = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("abcxyz@gmail.com","xdkasaspajokzfcr");
            }
        };

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

        Message msg = new MimeMessage(session);

        try {
            msg.setSubject("Email Test");
            msg.setText("We made it!!");
            msg.setFrom(new InternetAddress("abcxyz@gmail.com","Bob"));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress("abcxyz@gmail.com"));

            Transport.send(msg);
        } catch (MessagingException | UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Finished.");
    }

}

0 个答案:

没有答案