What is the easiest way to send and receive mails in java.
Asked
Active
Viewed 4,888 times
0
-
http://stackoverflow.com/questions/561011/what-is-the-easiest-way-for-a-java-application-to-receive-incoming-email – rahul Aug 17 '09 at 11:29
-
http://stackoverflow.com/questions/848645/sending-email-in-java – rahul Aug 17 '09 at 11:30
-
The question is unclear and straight without trying anything – Ripon Al Wasim Jan 07 '13 at 04:31
4 Answers
10
Don't forget Jakarta Commons Email for sending mail. It has a very easy to use API.
teabot
- 15,088
- 9
- 62
- 78
7
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
- 261,477
- 36
- 323
- 432
4
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", "me@example.com");
Session session = Session.getInstance(props, null);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,
"you@example.com");
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
- 170,119
- 31
- 233
- 283
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", "test@server.com");
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("test@server.com"));
InternetAddress addressTo = null;
addressTo = new InternetAddress("test@mail.net");
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("test@server.com", "password");
t.sendMessage(msg, msg.getAllRecipients());
t.close();
} catch(Exception exc) {
exc.printStackTrace();
}
firstthumb
- 4,447
- 5
- 32
- 45