2014-11-04 79 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(); 
    } 
} 
}