2012-07-06 69 views
1

我無法使用Java郵件API從我的yahoo id發送電子郵件。我嘗試了Google的不同選項,但失敗。請看看我的下面的代碼,讓我知道如果我失去了一些東西。在我看來雅虎不提供免費服務發送郵件,但我不確定。請提供你的想法。使用Javamail API將yahoo id的郵件發送到其他電子郵件ID

import javax.mail.*; 
import javax.mail.internet.*; 
import java.util.Properties; 

public class MailExample { 
    private static final String SMTP_HOST_NAME = "smtp.mail.yahoo.com"; 
    private static final int SMTP_HOST_PORT = 587;//465,587,25 
    private static final String SMTP_AUTH_USER = "[email protected]"; 
    private static final String SMTP_AUTH_PWD = "my password"; 

    public static void main(String[] args) throws Exception{ 
     new MailExample().test(); 
    } 

    public void test() throws Exception{ 
     Properties props = new Properties(); 

     props.put("mail.transport.protocol", "smtp"); 
     props.put("mail.smtp.host", SMTP_HOST_NAME); 
     props.put("mail.smtp.auth", "true"); 
     // props.put("mail.smtps.quitwait", "false"); 

     Session mailSession = Session.getDefaultInstance(props); 
     mailSession.setDebug(true); 
     Transport transport = mailSession.getTransport(); 

     MimeMessage message = new MimeMessage(mailSession); 
     message.setSubject("Testing SMTP-SSL"); 
     message.setContent("This is a test", "text/plain"); 

     message.addRecipient(Message.RecipientType.TO, 
      new InternetAddress("[email protected]")); 

     transport.connect 
      (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD); 

     transport.sendMessage(message, 
      message.getRecipients(Message.RecipientType.TO)); 
     transport.close(); 
    } 
} 

上面的代碼工作正常,Gmail,但雅虎它給錯誤,如:

DEBUG: setDebug: JavaMail version 1.4.1 DEBUG: getProvider() 
    returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport, 
    Sun Microsystems, Inc.,1.4.1] DEBUG SMTP: useEhlo true, 
    useAuth true 
DEBUG SMTP: trying to connect to host "smtp.mail.yahoo.com", port 587, 
    isSSL false Exception in thread "main" 
javax.mail.MessagingException: Could not connect to SMTP 
    host: smtp.mail.yahoo.com, port: 587; nested exception is: 
java.net.ConnectException: Connection timed out: connect  
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391) 
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412) 
at javax.mail.Service.connect(Service.java:288)  
at com.sample.mailexample.MailExample.test(MailExample.java:313)  
at com.sample.mailexample.MailExample.main(MailExample.java:291) Caused by: 
    java.net.ConnectException: Connection timed out: connect  
at java.net.PlainSocketImpl.socketConnect(Native Method)  
at java.net.PlainSocketImpl.doConnect(Unknown Source) 
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)  
at java.net.PlainSocketImpl.connect(Unknown Source)  
at java.net.SocksSocketImpl.connect(Unknown Source)  
at java.net.Socket.connect(Unknown Source) 
at java.net.Socket.connect(Unknown Source) 
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)  
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)  
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359) 
... 4 more 

我怎樣才能解決這個問題?

回答

2

試試這個代碼

public class SendMail { 

    String host, port, emailid,username, password; 
    Properties props = System.getProperties(); 
    Session l_session = null; 

    public BSendMail() { 
     host = "smtp.mail.yahoo.com"; 
     port = "587"; 
     emailid = "[email protected]"; 
     username = "a"; 
     password = "pwd"; 

     emailSettings(); 
     createSession(); 
     sendMessage("[email protected]", "[email protected]","Test","test Mail"); 
    } 

    public void emailSettings() { 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.debug", "false"); 
     props.put("mail.smtp.port", port); 
//  props.put("mail.smtp.socketFactory.port", port); 
//  props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
//  props.put("mail.smtp.socketFactory.fallback", "false"); 

    } 

    public void createSession() { 

     l_session = Session.getInstance(props, 
       new javax.mail.Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(username, password); 
        } 
       }); 

     l_session.setDebug(true); // Enable the debug mode 

    } 

    public boolean sendMessage(String emailFromUser, String toEmail, String subject, String msg) { 
     //System.out.println("Inside sendMessage 2 :: >> "); 
     try { 
      //System.out.println("Sending Message *********************************** "); 
      MimeMessage message = new MimeMessage(l_session); 
      emailid = emailFromUser; 
      //System.out.println("mail id in property ============= >>>>>>>>>>>>>> " + emailid); 
      //message.setFrom(new InternetAddress(emailid)); 
      message.setFrom(new InternetAddress(this.emailid)); 

      message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail)); 
      message.addRecipient(Message.RecipientType.BCC, new InternetAddress(AppConstants.fromEmail)); 
      message.setSubject(subject); 
      message.setContent(msg, "text/html"); 

      //message.setText(msg); 
      Transport.send(message); 
      System.out.println("Message Sent"); 
     } catch (MessagingException mex) { 
      mex.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     }//end catch block 
     return true; 
    } 

} 
+0

謝謝拉胡爾,它工作正常。 – user1182217 2012-07-06 07:10:39

+0

你好Rahul,早上它工作正常,但現在它給了我以下錯誤:任何想法,爲什麼這會來,請讓我知道。 'DEBUG SMTP:嘗試連接到主機「smtp.mail.yahoo.com」,端口587,isSSL錯誤 javax.mail.MessagingException:無法連接到SMTP主機:smtp.mail.yahoo.com,端口:587; ' – user1182217 2012-07-06 13:55:32

+1

除了端口號(不是587)而是465之外,上面的所有內容都是正確的。希望這有助於。 – Intotito 2014-04-03 21:07:34

4

完整的代碼使用如下的JavaMail API從雅虎發送電子郵件:

package ripon.java.mail; 
import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 

public class SendFromYahoo 
{ 
public static void main(String [] args) 
{  
    // Sender's email ID needs to be mentioned 
    String from = "[email protected]"; 
    String pass ="test123"; 
    // Recipient's email ID needs to be mentioned. 
    String to = "[email protected]"; 
    String host = "smtp.mail.yahoo.com"; 

    // Get system properties 
    Properties properties = System.getProperties(); 
    // Setup mail server 
    properties.put("mail.smtp.starttls.enable", "true"); 
    properties.put("mail.smtp.host", host); 
    properties.put("mail.smtp.user", from); 
    properties.put("mail.smtp.password", pass); 
    properties.put("mail.smtp.port", "587"); 
    properties.put("mail.smtp.auth", "true"); 

    // Get the default Session object. 
    Session session = Session.getDefaultInstance(properties); 

    try{ 
     // 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("This is actual message"); 

     // Send message 
     Transport transport = session.getTransport("smtp"); 
     transport.connect(host, from, pass); 
     transport.sendMessage(message, message.getAllRecipients()); 
     transport.close(); 
     System.out.println("Sent message successfully...."); 
    }catch (MessagingException mex) { 
     mex.printStackTrace(); 
    } 
} 
} 
+0

這對我有用,謝謝!順便說一句,你可以刪除'props.put(「mail.smtp.host」,host); props.put(「mail.smtp.port」,「587」); props.put(「mail.smtp.user」,用戶); props.put(「mail.smtp.password」,pass)',並簡單地在'transport.connect(host,port,user,pass)中提及';它應該同樣工作。 – Yura 2013-07-16 14:22:53

+0

因錯誤而失敗: com.sun.mail.smtp.SMTPSendFailedException:554事務失敗:由於可能的濫用而無法發送消息;請com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)參觀http://postmaster.yahoo.com/abuse_smtp.html瞭解更多信息 \t \t在com.sun.mail.smtp .SMTPTransport.finishData(SMTPTransport.java:1889) \t在com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1120) \t在SendFromYahoo.main(SendFromYahoo.java:46) – 2013-12-26 05:25:41

1

那麼對於諾塔因爲這將是

發送郵件(SMTP )服務器 服務器SMTP:smtp.nauta.cu,端口:25,身份驗證(SI)

In email app

+0

我不認爲這提供了一個問題的答案。 – mmushtaq 2016-12-19 17:38:24

相關問題