2011-03-29 459 views
3

我試圖發送使用Apache詹姆斯電子郵件與此代碼無法發送郵件 - javax.mail.NoSuchProviderException:SMTP

public static void main(String[] args) 
{ 

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

    String fromAddress = "[email protected]"; // [email protected] 
    String toAddress = "[email protected]"; 

    // 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) 

請幫助

+0

從'遠程登錄本地主機會發生什麼25'?如何從控制檯運行James? './james console'或'./james help'(至少對於James3) – Thufir 2012-08-17 05:39:05

回答

1

基於一些googling ...

你的問題聽起來李關注類路徑問題。

驗證您的類路徑中只有一個版本的mail.jar和activation.jar。

1

在我看來,你是缺少的屬性設置這將設置傳輸協議SMTP的

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