2011-04-18 78 views
0

我有一個代碼連接到郵件服務器並搜索特定的主題標籤並將回覆(REPLY)發送到該特定的郵件。無法連接到SMTP主機:本地主機,端口:25 - 同時回覆郵件

但是同時給予回覆消息我收到以下錯誤:

Could not connect to SMTP host: localhost, port: 25 

我能夠直接發送郵件出現問題只有當我使用回覆/轉發method.I正在使用JavaMail。

import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 
import javax.mail.search.SearchTerm; 
import javax.mail.search.SubjectTerm; 



public class Mail { 

    public static void main(String args[]) throws Exception { 

     Properties properties = System.getProperties(); 
     Session session = Session.getDefaultInstance(properties); 
     Store store = session.getStore("pop3"); 
     store.connect("smtp.gotouchpoint.com", "username", "password"); 
     Folder folder = store.getFolder("inbox"); 

     //Connect to the current host using the specified username and password. 



     try { 

      //Create a Folder object corresponding to the given name. 
      // Folder folder = store.getFolder("INBOX"); 
      // Open the Folder. 

      folder.open(Folder.READ_WRITE); 
      SearchTerm st1 =new SubjectTerm("**NEW-TICKET**"); 
      Message[] message = folder.search(st1); 


      //Message[] message = folder.getMessages(); 
      // Create a reply message 




      for (int i = 0; i < message.length; i++) { 


       System.out.println("------------ Message " + (i + 1) + " ------------"); 
       System.out.println("SentDate : " + message[i].getSentDate()); 
       System.out.println("From : " + message[i].getFrom()[0]); 
       System.out.println("Subject : " + message[i].getSubject()); 

       if(message[i].getSubject().equals("**NEW-TICKET**")) { 
        System.out.println("new-ticket"); 

        Message forward = new MimeMessage(session); 

        // Fill in header 
        forward.setSubject("Fwd: " + message[i].getSubject()); 
        forward.setFrom(new InternetAddress(message[i].getFrom()[0].toString())); 
        forward.addRecipient(Message.RecipientType.TO, 
          new InternetAddress("techsup[email protected]")); 

        // Create your new message part 
        BodyPart messageBodyPart = new MimeBodyPart(); 
        messageBodyPart.setText("Oiginal message:\n\n"); 

        // Create a multi-part to combine the parts 
        Multipart multipart = new MimeMultipart(); 
        multipart.addBodyPart(messageBodyPart); 

        // Create and fill part for the forwarded content 
        messageBodyPart = new MimeBodyPart(); 
        messageBodyPart.setDataHandler(message[i].getDataHandler()); 

        // Add part to multi part 
        multipart.addBodyPart(messageBodyPart); 

        // Associate multi-part with message 
        forward.setContent(multipart); 

        System.out.println("msg forward ...." +forward.getSubject()); 
        // Send message 
        Transport.send(forward); 



       } 


      } 

      System.out.println();  
      folder.close(true); 
      store.close(); 

     }catch(Exception e) { 
      e.printStackTrace(); 
     }  

    } 



} 

回答

0

不知道什麼可能會導致您的錯誤。 我只是冒險猜想這裏:不要使用靜態Transport.send()方法。 獲取傳輸對象並調用它的sendMessage)方法:

[...] 
    Transport transport = session.getTransport("smtp"); 
    transport.connect(D_HOST, D_PORT, D_USER, D_PASS); 
    transport.sendMessage(forward, forward.getAllRecipients()); 

嘗試,看看是否有所幫助。

歡呼聲。

相關問題