2011-07-19 9 views
1

JavaプログラムとJavaMailApiを使用してメールを送信しようとしています。私はこのプログラムを書いて、ローカルSMTPServerを持っています。これは問題ではありません。私はホストアドレスに何を入れるべきか分からない。私のコードを見て、どうすればいいのか教えてください。java smtpapiからメールを送信

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

// Send a simple, single part, text/plain e-mail 
    public class TestEmail { 

     public static void main(String[] args) { 

     // SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!! 
      String to = "[email protected]"; 
      String from = "[email protected]"; 
     // SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!! 
      String host = "smtp.yourisp.net"; 

     // Create properties, get Session 
      Properties props = new Properties(); 

     // If using static Transport.send(), 
     // need to specify which host to send it to 
      props.put("mail.smtp.host", host); 
     // To see what is going on behind the scene 
      props.put("mail.debug", "true"); 
      Session session = Session.getInstance(props); 

      try { 
      // Instantiatee a message 
       Message msg = new MimeMessage(session); 

      //Set message attributes 
       msg.setFrom(new InternetAddress(from)); 
       InternetAddress[] address = {new InternetAddress(to)}; 
       msg.setRecipients(Message.RecipientType.TO, address); 
       msg.setSubject("Test E-Mail through Java"); 
       msg.setSentDate(new Date()); 

      // Set message content 
       msg.setText("This is a test of sending a " + 
         "plain text e-mail through Java.\n" + 
         "Here is line 2."); 

      //Send the message 
       Transport.send(msg); 
     } 
      catch (MessagingException mex) { 
      // Prints all nested (chained) exceptions as well 
       mex.printStackTrace(); 
     } 
    } 
}//End of class 

答えて

0

私はホストアドレスに置くために何を知りません。

ホストアドレスは、SMTPサーバーのIPアドレスまたはドメイン名です。

+0

しかし、私はSMTPサーバーソフトウェアを持っていると言いました。 – atul

+0

@atul:SMTPサーバソフトウェアを持っているマシンのIPまたはドメイン名をホストアドレスとして与えてみてください。そうでなければ、私のプログラムは接続できますか? –

0

ホストは、それが動作するはずのGmail

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

ため

smtp.gmail.com 

です。 しかし、これらのメールAPIが機能することを忘れないでください。あなたのGmailアカウントからpop3とsmtpを有効にする必要があります。また、余計な設定(双方向のセキュリティログインなど)では、他のapiからのsmtp/pop3を使用できなくなります。

+0

それは私がインターネット接続を持っている場合にのみ動作しますが、私のプログラムがwrkするか、インターネットに接続されていないマシンには接続しないでください。 – atul

+0

あなたのIPアドレスを試してください。私は自分のメールシステムでそれをテストしていないので、まったく考えられません。 – peeyush

+0

okありがとう私は:) – atul