java - 使用 Java Mail API 时出现问题

标签 java jakarta-mail

我有一个需要使用 Java Mail API 发送邮件的 servlet,但是尽管密码适用于 gmail,但我没有收到指定密码的错误。

MailServiceImpl.java:

public class MailServiceImpl extends RemoteServiceServlet implements MailService {

    private static String HOST = "smtp.gmail.com";
    private static int PORT = 465;
    private String username = "foo@gmail.com";
    private String password = "foo123"; 
    private Properties props = new Properties();

    @Override
    public void sendMail(String email) {
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true"); 

        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");

        Session session = Session.getInstance(props);
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("foo@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("foo@gmail.com"));
            message.setSubject("Testing Subject");
            message.setText("Subcriber Email:," +
                    "\n\n " + email);
            Transport transport = session.getTransport("smtp");
            transport.connect(HOST, PORT, username, password);
            Transport.send(message);            
            transport.close(); // -- needed?

        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
}

但是我收到此错误:

javax.mail.AuthenticationFailedException: failed to connect, no password specified?
    at javax.mail.Service.connect(Service.java:329)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at com.mygwtapp.server.MailServiceImpl.sendMail(MailServiceImpl.java:43)

最佳答案

尝试使用 SSL 连接。它对我有用。

    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("username","password");
            }
        });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("from@test.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("to@test.com"));
        message.setSubject("Testing Subject");
        message.setText("mail text");

        Transport.send(message);

        System.out.println("OK");

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

关于java - 使用 Java Mail API 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6991594/

相关文章:

java - 使用java代码通过smtp发送电子邮件

java - 捕获 Javamail 异常

java - Java 中的计时器任务问题

java - 保持 LinkedList 始终排序

java - 如何使用方法引用在构造函数中创建的数据类型(数组)

java - 在不透露其他收件人的情况下向多个收件人发送电子邮件

java - 如何通过基于ejb 3.1注释的邮件发送

Java:可以通过 SSLSocket 发送任何字符串,但以 h/H 开头的字符串除外

java - 从父类(super class)方法返回子类对象

spring - 找不到 javax.mail.internet.MimeMessage 的类文件