【问题标题】:JavaMail API Error (javax.mail.NoSuchProviderException: invalid provider)JavaMail API 错误(javax.mail.NoSuchProviderException:无效的提供者)
【发布时间】:2014-11-04 23:28:54
【问题描述】:

我正在尝试使用 JavaMail API 创建程序,但是,我不断收到以下错误消息。

javax.mail.NoSuchProviderException: invalid provider
at javax.mail.Session.getTransport(Session.java:738)
at javax.mail.Session.getTransport(Session.java:682)
at javax.mail.Session.getTransport(Session.java:662)
at EmailAutoResponder2.main(EmailAutoResponder2.java:56)

我无法通过在线阅读来解决它,因为他们所有的解决方案仍然给我同样的信息。

这是Java代码:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class EmailAutoResponder2 {

public static void main(String[] args) {

    String to = "username@videotron.ca";
    String from = "username@videotron.ca";

    Properties properties = System.getProperties();

    properties.setProperty("mail.store.protocol", "imaps");

    Session session1 = Session.getInstance(properties);

    //If email received by specific user, send particular response.
    Properties props = new Properties();

    props.put("mail.imap.auth", "true");
    props.put("mail.imap.starttls.enable", "true");
    props.put("mail.imap.host", "imap.videotron.ca");
    props.put("mail.imap.port", "143");

    Session session2 = Session.getInstance(props, new Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("username@videotron.ca", "password");
            }
        });

    try {
        Store store = session2.getStore("imap");
        store.connect("imap.videotron.ca", "username@videotron.ca", "password");
        Folder fldr = store.getFolder("Inbox");
        fldr.open(Folder.READ_ONLY);
        Message msgs[] = fldr.getMessages(); 
            for(int i = 0; i < msgs.length; i++){
                System.out.println(InternetAddress.toString(msgs[i].getFrom()));

            if (InternetAddress.toString(msgs[i].getFrom()).startsWith("Name")){

                MimeMessage message = new MimeMessage(session1);

                message.setFrom(new InternetAddress(from));
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                message.setSubject("Subject");
                message.setText("Message");

                String protocol = "imap";
                props.put("mail." + protocol + ".auth", "true");

                Transport t = session2.getTransport("imap");
                try {
                    t.connect("username@videotron.ca", "password");
                    t.sendMessage(message, message.getAllRecipients());
                }
                finally {
                    t.close();
                }

            }
            }

    }

    catch(MessagingException mex){
        mex.printStackTrace();
    }

    catch(Exception exc) {

    }

    }

    }

谢谢!

【问题讨论】:

  • 您没有在任何地方设置系统属性,它们都在您自己的属性对象中。

标签: java email jakarta-mail server


【解决方案1】:

您正在连接到 localhost 以发送消息。您是否在本地计算机上运行了邮件服务器?可能不是。您需要设置 mail.smtp.host 属性。您可能还需要为您的邮件服务器提供用户名和密码;见JavaMail FAQ

【讨论】:

  • 我按照您链接中的说明进行操作,现在我收到此错误消息:javax.mail.NoSuchProviderException: invalid provider at javax.mail.Session.getTransport(Session.java:724)在 javax.mail.Session.getTransport(Session.java:668) 在 javax.mail.Session.getTransport(Session.java:648) 在 EmailAutoResponder2.main(EmailAutoResponder2.java:57)
  • 告诉我你是如何更新你的代码的。你使用什么协议? “smtp”? “smtps”?除上述异常外,其他任何事情都会失败。
  • 我正在使用 imap。我已使用新代码和错误消息更新了我的问题。
  • 您不能将“imap”用于传输,它只是一个存储协议。
【解决方案2】:

以下代码可以解决你的问题

 import java.util.*;
 import javax.mail.*;
 import javax.mail.internet.*;

 public class Email {

 private static String USER_NAME = "username";  // GMail user name (just the part before "@gmail.com")
 private static String PASSWORD = "password"; // GMail password

private static String RECIPIENT = "xxxxx@gmail.com";

public static void main(String[] args) {
String from = USER_NAME;
String pass = PASSWORD;
String[] to = { RECIPIENT }; // list of recipient email addresses
String subject = "Java send mail example";
String body = "hi ....,!";

sendFromGMail(from, pass, to, subject, body);
 }

private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
Properties props = System.getProperties();
String host = "smtp.gmail.com";

props.put("mail.smtp.starttls.enable", "true");

props.put("mail.smtp.ssl.trust", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");


Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);

try {


    message.setFrom(new InternetAddress(from));
    InternetAddress[] toAddress = new InternetAddress[to.length];

    // To get the array of addresses
    for( int i = 0; i < to.length; i++ ) {
        toAddress[i] = new InternetAddress(to[i]);
    }

    for( int i = 0; i < toAddress.length; i++) {
        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
    }



    message.setSubject(subject);
    message.setText(body);


    Transport transport = session.getTransport("smtp");


    transport.connect(host, from, pass);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();

 }
 catch (AddressException ae) {
    ae.printStackTrace();
 }
 catch (MessagingException me) {
    me.printStackTrace();
  }
 }
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 2016-12-15
    • 2014-03-31
    • 2021-08-29
    • 1970-01-01
    • 2013-07-09
    • 2016-01-26
    相关资源
    最近更新 更多