2013-03-18 50 views
1

我尝试使用下面的代码来SNED邮件发送邮件: -无法使用JavaMail API

java.awt.EventQueue.invokeLater(
       new Runnable() { 

        @Override 
        public void run() { 

         new SwingWorker<Void, Integer>() { 

          String to = "[email protected]"; 
          // Sender's email ID needs to be mentioned 
          String from = "[email protected]"; 
          // Assuming you are sending email from localhost 
          String host = "localhost"; 
          // Get system properties 
         // Properties properties = System.getProperties(); 

          // Setup mail server 


          protected Void doInBackground() throws Exception { 
           try { 
Properties properties = new Properties(); 
properties.put("mail.smtp.socketFactory.port", "465"); 
properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); 
properties.put("mail.smtp.starttls.enable", "true"); 
     // Get the default Session object. 
     Session session = Session.getDefaultInstance(properties); 

            // Create a default MimeMessage object. 
            MimeMessage message = new MimeMessage(session); 

            // Set From: header field of the header. 
            message.setFrom(new InternetAddress(from)); 

            // Set To: header field of the header. 
            message.addRecipient(Message.RecipientType.TO, 
              new InternetAddress(to)); 

            // Set Subject: header field 
            message.setSubject("This is the Subject Line!"); 

            // Now set the actual message 
            message.setText("Message" + jTextField1.getText() + " xx " + jTextField2.getText()); 

            // Send message 
            Transport.send(message); 

           } catch (Exception x) { 
            x.printStackTrace(); 
           } 
           return null; 
          } 

          protected void process() { 

          } 

          protected void done() { 
           try { 

            JOptionPane.showMessageDialog(null, "You'll get a response from our team shortly !"); 

           } catch (Exception x) { 
           } 

          } 
         }.execute(); 
        } 

在上面的代码中我发现了一个异常的呼叫:

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; 
    nested exception is: 
    java.net.SocketException: Permission denied: connect 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638) 
    at javax.mail.Service.connect(Service.java:295) 
    at javax.mail.Service.connect(Service.java:176) 
    at javax.mail.Service.connect(Service.java:125) 
    at javax.mail.Transport.send0(Transport.java:194) 
    at javax.mail.Transport.send(Transport.java:124) 
    at ims.Activation$5$1.doInBackground(Activation.java:374) 
    at ims.Activation$5$1.doInBackground(Activation.java:327) 
    at javax.swing.SwingWorker$1.call(SwingWorker.java:296) 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:166) 
    at javax.swing.SwingWorker.run(SwingWorker.java:335) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
    at java.lang.Thread.run(Thread.java:722) 
Caused by: java.net.SocketException: Permission denied: connect 
    at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method) 
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) 
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) 
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391) 
    at java.net.Socket.connect(Socket.java:579) 
    at java.net.Socket.connect(Socket.java:528) 
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288) 
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231) 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900) 
    ... 15 more 

我该如何解决这个问题?

+0

您的本地计算机是否设置为有效的“SMTP”主机? – Reimeus 2013-03-18 19:16:23

+0

@ c.pramod是否使用JDK 7 – 2013-03-18 19:19:57

回答

3

除非你正在运行在您的本地环境中的邮件服务器,那么你将需要通过其他邮件属性来指定一个:

properties.put("mail.smtp.host", aHost); 

举个例子,如果你使用Gmail的继电器,您将具有:

properties.put("mail.smtp.host", "smtp.gmail.com"); 

此外,您需要为会话设置mail.smtp.auth属性。

properties.put("mail.smtp.auth", true); 

根据您最终使用哪个主机,您可能还需要提供认证信息。例如,再次使用Gmail中继:

Session session = Session.getDefaultInstance(props, 
    new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication("someUserName", "somePassword"); 
     } 
    } 
); 
+0

完全相同。现在我收到com.sun.mail.smtp.SMTPSendFailedException:530-5.5.1需要身份验证。例外 – 2013-03-18 19:30:40

+0

@ c.pramod - 您实际上需要提供有效的凭据(我假设您使用的是Gmail中继?)。填写示例中显示的帐户用户名和密码。 – Perception 2013-03-18 19:36:16

+0

我添加了这行properties.put(“mail.smtp.auth”,“true”); 和它的工作.. Thx的答复BTW! – 2013-03-18 19:40:16