What is the easiest way to send and receive mails in java.
+3
A:
Check this package out. From the link, here's a code sample:
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);
}
Pablo Santa Cruz
2009-08-17 11:28:27
+6
A:
JavaMail is the traditional answer for sending email (as everyone's pointing out).
As you also want to receive mail, however, you should check out Apache James. It's a modular mail server and heavily configurable. It'll talk POP and IMAP, supports custom plugins and can be embedded in your application (if you so wish).
Brian Agnew
2009-08-17 11:28:56
+1
A:
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();
}
Firstthumb
2009-08-17 11:29:47
FYI the link above is broken
delux247
2009-10-22 21:38:12
Link is fixed. .
Chris Kaminski
2009-10-23 20:43:51
The code from your link really works! I tried many other snippets, and this is the first that works.
True Soft
2009-12-17 09:53:05
+7
A:
Don't forget Jakarta Commons Email for sending mail. It has a very easy to use API.
teabot
2009-08-17 11:47:15