2014-11-04 55 views
1

我试图用JavaMail API创建一个程序,但是,我不断收到以下错误消息。JavaMail API错误(javax.mail.NoSuchProviderException:invalid provider)

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 = "[email protected]"; 
    String from = "[email protected]"; 

    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("[email protected]", "password"); 
      } 
     }); 

    try { 
     Store store = session2.getStore("imap"); 
     store.connect("imap.videotron.ca", "[email protected]", "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("[email protected]", "password"); 
        t.sendMessage(message, message.getAllRecipients()); 
       } 
       finally { 
        t.close(); 
       } 

      } 
      } 

    } 

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

    catch(Exception exc) { 

    } 

    } 

    } 

谢谢!

+0

你没有在任何地方设置您的系统性能,他们都在自己的属性的对象。 – 2014-11-04 23:42:32

回答

1

您正在连接到本地主机来发送消息。你的本地机器上运行邮件服务器吗?可能不会。您需要设置mail.smtp.host属性。您可能还需要为您的邮件服务器提供用户名和密码;请参阅JavaMail FAQ

+0

我跟着您的链接中的说明,现在我得到这个错误信息:** javax.mail.NoSuchProviderException:无效的供应商 \t在javax.mail.Session.getTransport(Session.java:724) \t在javax.mail .Session.getTransport(Session.java:668) \t在javax.mail.Session.getTransport(Session.java:648) \t在EmailAutoResponder2.main(EmailAutoResponder2.java:57)** – 2014-11-05 08:01:37

+0

告诉我你如何更新你的码。你使用什么协议? “SMTP”? “SMTPS”?除了上述例外,其他任何情况都会失败。 – 2014-11-05 20:55:07

+0

我正在使用imap。我用新的代码和错误消息更新了我的问题。 – 2014-11-06 08:17:24

-1

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

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 = "[email protected]"; 

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(); 
    } 
} 
}