java发送邮件(1)
来源:互联网 发布:网络大电影发行流程 编辑:程序博客网 时间:2024/06/05 09:25
在实际的开发过程中,java发送邮件有着非常广泛的应用。例如:注册账户时激活账户;忘记密码时取回密码;发送欢迎邮件等等。
今天,我们就来看一下利用java mail发送邮件。
我们在开发java mail发送邮件的时候,必须有这activation.jar,additionnal.jar以及mail.jar这三个jar包。这几个jar包包含我们开发发送邮件程序所需要的类。
java mail发送邮件的大概步骤如下:
1、创建一个java.util.Properties类,此类中,存放mail服务器地址、mail服务所用的协议、服务器的端口号、是否需要登录验证等相关的配置信息。
2、创建一个javax.mail.Authenticator类的子类,用来存储登录mail服务器的用户名和密码。
3、利用1、2所创建的类,调用javax.mail.Session类的静态方法getDefaultInstance()来获取Session对象。此Session对象就是程序客户端与邮件服务器之间的连接。
4、利用3中所取得的Session对象,创建一个新的javax.mail.Message的实现类对象。并在对象中设置发件人、收件人、主题、发送内容等相关。
5、从Session对象中获取javax.mail.Transport对象。
6、利用5中所获取的Transport对象,发送邮件,发送完毕后,关闭Transport对象。
java mail发送邮件中所使用的类及相互之间的关系:
接下来我们看看java mail发送邮件的几个实例。
一、java mail发送文本邮件
public class MailDemo { public Session createSession() { Properties p = new Properties(); p.setProperty("mail.transport.protocol", "smtp"); p.setProperty("mail.host", "smtp.163.com"); p.setProperty("mail.smtp.port","25"); p.setProperty("mail.smtp.auth", "true"); p.setProperty("mail.debug", "true"); Session s = Session.getDefaultInstance(p, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("choukakou@163.com", "BRZJH1SJHHRHL"); } }); return s; } public Message createMessage(Session session) throws AddressException, MessagingException { Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("choukakou@163.com")); msg.setSubject("message"); msg.setText("This is a test message for mail"); msg.setSentDate(new Date()); return msg; } public static void main(String[] args) throws AddressException, MessagingException { MailDemo demo = new MailDemo(); Session session = demo.createSession(); Message message = demo.createMessage(session); Transport port = session.getTransport(); port.connect(); port.sendMessage(message, new Address[]{new InternetAddress("zhao.jiahong@yahoo.com")}); port.close(); }
}
二、发送HTML邮件
<pre name="code" class="java"> public class MailDemo { public Session createSession() { Properties p = new Properties(); p.setProperty("mail.transport.protocol", "smtp"); p.setProperty("mail.host", "smtp.163.com"); p.setProperty("mail.smtp.port","25"); p.setProperty("mail.smtp.auth", "true"); p.setProperty("mail.debug", "true"); Session s = Session.getDefaultInstance(p, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("choukakou@163.com", "BRZJH1SJHHRHL"); } }); return s; } public Message createMessage(Session session) throws AddressException, MessagingException { Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("choukakou@163.com")); msg.setSubject("html message"); msg.setContent("<a href=''>链接</a>","text/html;charset=gbk"); msg.setSentDate(new Date()); return msg; } public static void main(String[] args) throws AddressException, MessagingException { MailDemo demo = new MailDemo(); Session session = demo.createSession(); Message message = demo.createMessage(session); Transport port = session.getTransport(); port.connect(); port.sendMessage(message, new Address[]{new InternetAddress("zhao.jiahong@yahoo.com")}); port.close(); } }
三、发送带有附件的邮件
public class MailDemo { public Session createSession() { Properties p = new Properties(); p.setProperty("mail.transport.protocol", "smtp"); p.setProperty("mail.host", "smtp.163.com"); p.setProperty("mail.smtp.port","25"); p.setProperty("mail.smtp.auth", "true"); p.setProperty("mail.debug", "true"); Session s = Session.getDefaultInstance(p, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("choukakou@163.com", "BRZJH1SJHHRHL"); } }); return s; } public Message createMessage(Session session) throws AddressException, MessagingException { Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("choukakou@163.com"));<span style="white-space:pre"></span><span style="white-space:pre"></span> MimeMultipart mimeMultipart = new MimeMultipart();<span style="white-space:pre"></span><span style="white-space:pre"></span> MimeBodyPart bodyPart = new MimeBodyPart();<span style="white-space:pre"></span><span style="white-space:pre"></span> bodyPart.setText("message");<span style="white-space:pre"></span><span style="white-space:pre"></span> MimeBodyPart attech1 = new MimeBodyPart();<span style="white-space:pre"></span><span style="white-space:pre"></span> mimeMultipart.addBodyPart(bodyPart);<span style="white-space:pre"></span><span style="white-space:pre"></span> DataSource ds1 = new FileDataSource("F:\\hello.txt");<span style="white-space:pre"></span><span style="white-space:pre"></span> DataHandler dh = new DataHandler(ds1);<span style="white-space:pre"></span><span style="white-space:pre"></span> attech1.setDataHandler(dh);<span style="white-space:pre"></span><span style="white-space:pre"></span> attech1.setFileName("hello");<span style="white-space:pre"></span><span style="white-space:pre"></span> mimeMultipart.addBodyPart(attech1);<span style="white-space:pre"></span><span style="white-space:pre"></span> msg.setContent(mimeMultipart);<span style="white-space:pre"></span><span style="white-space:pre"></span> msg.setSentDate(new Date()); return msg; } public static void main(String[] args) throws AddressException, MessagingException { MailDemo demo = new MailDemo(); Session session = demo.createSession(); Message message = demo.createMessage(session); Transport port = session.getTransport(); port.connect(); port.sendMessage(message, new Address[]{new InternetAddress("zhao.jiahong@yahoo.com")}); port.close(); } }这就是java mail发送邮件的过程,仔细的看上述过程,你会发现,其实java mail发送各种邮件的过程是一致的,只是创建各种不同的Message过程稍有不同而已。
- java发送邮件(1)
- 发送邮件(java)
- java发送邮件(一)
- java发送邮件(2)
- java发送邮件(3)
- java发送邮件(发一封)
- Java发送邮件(二)
- 基于JavaMail的Java邮件发送(简单邮件发送)
- 基于JavaMail的Java邮件发送(复杂邮件发送)
- java发送邮件(javaMail通过SMTP发送邮件)
- Java学习笔记(1)----Java中发送邮件
- [java]Java发送邮件
- java 邮件发送 --- 简单的邮件发送
- JAVA发送邮件及springboot发送邮件
- 1分钟学会用java发送邮件
- java mail 实践二(邮件发送)
- 利用java mail发送邮件(转)
- java邮件(带附件发送)
- Touch
- hdoj-1171-Big Event in HDU【动态规划】
- JAVA 程序 对给定整数60,获取其16进制表现形式
- C++客户端 如何通过gsoap调用WebService接口(以及返回结果乱码解决办法)
- cosme大赏卸妆部门第二位:Peau Blanche卸妆水之体验报告
- java发送邮件(1)
- zoj 3612 ——Median
- 一行字符中寻找英文单词个数
- url
- 指纹识别的基本原理
- java - (04) 域名
- “The run destination iOS Device is not valid for running the scheme”
- spring、mybatis、mvc 整合简单例子
- poj 1383 Labyrinth 【迷宫图 BFS实现 树的直径裸题】