2016-04-08 38 views
1

これは私のコードjavax.mail.MessagingException:SMTPホストに接続できませんでした:localhost、port:25;

public class Email { 
    static Properties props; 
    public static void main(String[] args){ 
     String from = "some email"; 
     String to= "some other email"; 
     String host = "localhost"; 
     props = System.getProperties(); 
     props.setProperty("mail.host", host); 
     props.put("mail.transport.protocol", "smtp"); 
     Session session = Session.getDefaultInstance(props,null); 
     try{ 
      MimeMessage message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(from)); 
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
      message.setSubject("This is the Subject Line!"); 
      message.setText("This is actual message"); 
      Transport.send(message); 
      System.out.println("Sent message successfully...."); 
     }catch (MessagingException mex) { 
      mex.printStackTrace(); 
     } 
    } 
} 

である私は、次の例外を取得しています:

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; 
    nested exception is: 
    java.net.ConnectException: Connection refused 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) 
    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 com.cisco.ci.support.email.Email.main(Email.java:51) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
Caused by: java.net.ConnectException: Connection refused 
    at java.net.PlainSocketImpl.socketConnect(Native Method) 
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
    at java.net.Socket.connect(Socket.java:589) 
    at java.net.Socket.connect(Socket.java:538) 
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321) 
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237) 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927) 
    ... 12 more 

私が私のMac上のSMTPサーバーを持っていないようですか?任意のソリューションですか?

+1

MacをSMTPにインストールするか、実際のSMTPサーバー(または[偽](https://nilhcem.github.io/FakeSMTP/))を指すようにコードを変更してください。 –

答えて

2

あなたのMacにsmtpサーバーをインストールする必要があります。これは簡単ではありません。既存のsmtpサーバーを使用してください。明らかに、そのサーバのアカウントが必要です。認証されていないsmtpサーバはあまりありません。

など。 googlemailアカウントを作成し、使用方法の例を確認してください:http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

+0

その他の有用な情報は、[JavaMail FAQ](http://www.oracle.com/technetwork/java/javamail/faq/index.html)を参照してください。 –

関連する問題