【发布时间】:2010-11-20 05:19:12
【问题描述】:
在java中发送和接收邮件的最简单方法是什么。
【问题讨论】:
-
问题不明确,直截了当
标签: java email email-client
在java中发送和接收邮件的最简单方法是什么。
【问题讨论】:
标签: java email email-client
不要忘记Jakarta Commons Email 发送邮件。它有一个非常易于使用的 API。
【讨论】:
JavaMail 是发送电子邮件的传统答案(正如每个人都指出的那样)。
由于您还想接收邮件,但是,您应该查看Apache James。它是一个模块化的邮件服务器并且高度可配置。它会讨论 POP 和 IMAP,支持自定义插件,并且可以嵌入到您的应用程序中(如果您愿意的话)。
【讨论】:
检查this 打包。从链接中,这是一个代码示例:
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);
}
【讨论】:
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();
}
【讨论】: