博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaMail 精萃
阅读量:4050 次
发布时间:2019-05-25

本文共 2504 字,大约阅读时间需要 8 分钟。

-- Start

JavaMail 简介

JavaMail 是一个用来发送电子邮件的包,Java EE (j2ee.jar) 中包含了该包,但是如果你使用的是Java SE,那么首先你必须下载 JavaMail 包。

 

下载 JavaMail

你可以在 Google 中搜索 JavaMail download,然后到官网去下载最新的 JavaMail 包。然后把 mail.jar 添加到自己的 classpath 中就可以了。值得注意的是,如果你使用的是 Java SE 6 以下的版本,你还需要下载 JavaBeans Activation Framework 包, 然后将 activation.jar 也添加到自己的 classpath 中。

 

邮件协议

通常邮件服务器使用以下一种或多种协议来收发电子邮件。

  • SMTP(Simple Mail Transfer Protocol)
  • POP(Post Office Protocol)
  • IMAP(Internet Message Access Protocol)
  • MIME(Multipurpose Internet Mail Extensions)
  • NNTP (Network News Transport Protocol)

事实上,正是由于 JavaMail 封装了这些协议,从而使我们在收发邮件的时候只需面对 API, 而无需关心协议细节。

 

一个最简单的例子

import java.util.Date;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class Test {	// 修改此处即可利用自己的邮件服务器来发送邮件	public static final String HOST = "smtp.163.com";	public static final String USER = "wave0409";	public static final String PASSWORD = "*******************";		public static final String ADDRESS = "wave0409@163.com"; // 注意修改此处,千万不要害我	public static final String SUBJECT = "Test";	public static final String TEXT = "This is a test mail.";	public static void main(String[] args) throws Exception {		try {			// 设置javaMail属性			Properties props = new Properties();			props.put("mail.smtp.host", HOST); // 邮件服务器			props.put("mail.transport.protocol", "smtp"); // 协议			props.put("mail.smtp.auth", "true"); // 是否需要认证(用户名,密码)						// 认证 			Authenticator auth = new Authenticator() {				@Override				protected PasswordAuthentication getPasswordAuthentication() {					return new PasswordAuthentication(USER, PASSWORD);				}							};			// 获取session实例			Session sendMailSession = Session.getInstance(props, auth);			// 构造邮件			Message newMessage = new MimeMessage(sendMailSession);			newMessage.setFrom(new InternetAddress(ADDRESS));			newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(ADDRESS));			newMessage.setSubject(SUBJECT);			newMessage.setSentDate(new Date());			newMessage.setText(TEXT);			// 发送邮件			Transport.send(newMessage);			System.out.println("发送成功,请注意查收。");		} catch (MessagingException m) {			m.printStackTrace();		}	}}

 

更多例子

事实上,利用 JavaMail 还可以做很多事情,如:发送 html 邮件,发送附件,阅读邮件等等。在下载的 JavaMail 包的 demo 文件夹下可以找到更多例子。

 

-- 更多参见:-- 声 明:转载请注明出处

-- Last Updated on 2012-08-16
-- Written by ShangBo on 2012-07-28
-- End
 

你可能感兴趣的文章
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Clone Graph(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
java自定义容器排序的两种方法
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
AngularJS2中最基本的文件说明
查看>>
从头开始学习jsp(2)——jsp的基本语法
查看>>
使用与或运算完成两个整数的相加
查看>>
备忘:java中的递归
查看>>
DIV/CSS:一个贴在左上角的标签
查看>>
Solr及Spring-Data-Solr入门学习
查看>>
Vue组件
查看>>
python_time模块
查看>>
python_configparser(解析ini)
查看>>