无法发送邮件 - javax.mail.NoSuchProviderException: smtp

3
我正在尝试使用以下代码通过Apache James发送电子邮件:
public static void main(String[] args)
{

    String user = "sumit";  // Newly created user on JAMES
    String password = "sumit"; // user password

    String fromAddress = "sumit@localhost"; // newlycreateduser@localhost
    String toAddress = "sumitjain91@gmail.com";

    // Create a mail session
    Properties properties = new Properties();
    properties.put("mail.smtp.host", "localhost");
    properties.put("mail.smtp.port", "25");
    properties.put("mail.smtp.username", user);
    properties.put("mail.smtp.password", password);
    Session session = Session.getDefaultInstance(properties, null);

    try
    {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromAddress));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));

        message.setSubject("Email from our JAMES Server");
        message.setText("Luke, I'm your father!!");
        Transport.send(message);

        System.out.println("Email sent successfully");
    }
    catch (MessagingException e)
    {
        e.printStackTrace();
    }
}

我遇到了以下异常:
javax.mail.NoSuchProviderException: smtp
    at javax.mail.Session.getService(Session.java:784)
    at javax.mail.Session.getTransport(Session.java:720)
    at javax.mail.Session.getTransport(Session.java:660)
    at javax.mail.Session.getTransport(Session.java:640)
    at javax.mail.Session.getTransport(Session.java:697)
    at javax.mail.Transport.send0(Transport.java:192)
    at javax.mail.Transport.send(Transport.java:124)
    at mail.Main.main(Main.java:44)

请帮忙


telnet localhost 25 发生了什么?从控制台运行 James 呢?是 ./james console 还是 ./james help(至少对于 James3)? - Thufir
2个回答

1

我觉得你可能缺少一个属性设置,该设置将传输协议设置为smtp

  properties.put("mail.transport.protocol", "smtp");

1

根据一些谷歌搜索...

你的问题听起来像是一个类路径问题。

请确认你的类路径中只有一个版本的mail.jar和activation.jar。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接