2009-08-17 49 views
0

在java中发送和接收邮件的最简单方法是什么?从java发送邮件

+0

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

+0

http://stackoverflow.com/questions/848645 /发送电子邮件在java – rahul 2009-08-17 11:30:30

+1

定义“最简单”。 – 2009-08-17 11:31:46

回答

10

不要忘记Jakarta Commons Email发送邮件。它有一个非常易于使用的API。

+0

+1。不知道它存在 – 2009-08-17 11:55:38

+0

这就是我正在寻找的.....谢谢.. .. .. .. .. – 2009-08-17 12:26:56

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(); 
}