2009-08-17 12 views
0

Javaでメールを送受信する最も簡単な方法は何ですか?Javaからメールを送信する

+0

http://stackoverflow.com/questions/561011/what-is-the-easiest-way-for-a-java-application-to-receive-incoming-email – rahul

+0

http://stackoverflow.com/questions/848645/sending-email-in-java – rahul

+1

"最も簡単"を定義します。 –

答えて

10

メールを送信するためにJakarta Commons Emailを忘れないでください。それは非常に使いやすいAPIを持っています。

+0

+1。 –

+0

これは私が探していたものであることを知らなかった.....ありがとう..a..lot .. –

4

thisパッケージを確認してください。リンクから、ここでのサンプルコードです:

Properties props = new Properties(); 
props.put("mail.smtp.host", "my-mail-server"); 
props.put("mail.from", "[email protected]"); 
Session session = Session.getInstance(props, null); 

try { 
    MimeMessage msg = new MimeMessage(session); 
    msg.setFrom(); 
    msg.setRecipients(Message.RecipientType.TO, 
         "[email protected]"); 
    msg.setSubject("JavaMail hello world example"); 
    msg.setSentDate(new Date()); 
    msg.setText("Hello, world!\n"); 
    Transport.send(msg); 
} catch (MessagingException mex) { 
    System.out.println("send failed, exception: " + mex); 
} 
7

JavaMailは(誰もが指摘だとして)電子メールを送信するための伝統的な答えです。

また、にはメールを受信したいので、Apache Jamesをチェックアウトする必要があります。それはモジュラーメールサーバーであり、大幅に構成可能です。それはPOPとIMAPを話し、カスタムプラグインをサポートし、あなたのアプリケーションに埋め込むことができます(望むならば)。

1
try { 
Properties props = new Properties(); 
props.put("mail.smtp.host", "mail.server.com"); 
props.put("mail.smtp.auth","true"); 
props.put("mail.smtp.user", "[email protected]"); 
props.put("mail.smtp.port", "25"); 
props.put("mail.debug", "true"); 

Session session = Session.getDefaultInstance(props); 

MimeMessage msg = new MimeMessage(session); 

msg.setFrom(new InternetAddress("[email protected]")); 

InternetAddress addressTo = null; 
addressTo = new InternetAddress("[email protected]"); 
msg.setRecipient(javax.mail.Message.RecipientType.TO, addressTo); 

msg.setSubject("My Subject"); 
msg.setContent("My Message", "text/html; charset=iso-8859-9"); 

Transport t = session.getTransport("smtp"); 
t.connect("[email protected]", "password"); 
t.sendMessage(msg, msg.getAllRecipients()); 
t.close(); 
} catch(Exception exc) { 
    exc.printStackTrace(); 
}