服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - JavaMail实现发送邮件功能

JavaMail实现发送邮件功能

2020-12-20 14:40我俩绝配 Java教程

这篇文章主要为大家详细介绍了JavaMail实现发送邮件功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了JavaMail实现发送邮件功能的具体代码,供大家参考,具体内容如下

用java发邮件,必须要导入新的包

       mail.jar – 发邮件的核心包

       activation.jar – 对用户和密码加密.

在mail.jar中有三个核心类:

       Javax.mail.Session – 是指与邮件服务器会话。整个项目中只要一个就可以了.

       Javax.mail.Message(接口)  - 准备发送数据信息。

                      MimeMessage - 可以设置类型的数据信息。

       Transport – 它拥有一个方法可以发送Message。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@Test
 public void sendMail() throws Exception{
  //1,声明properties对象放信息
  Properties props = new Properties();
  //设置连接哪一台服务器
  props.setProperty("mail.host", "smtp.163.com");
  //设置是否认证:
  props.setProperty("mail.smtp.auth", "true");
  
  //2,声明用户名和密码
  Authenticator auth = new Authenticator(){
   //返回用户名和密码对象
   @Override
   protected PasswordAuthentication getPasswordAuthentication() {
    PasswordAuthentication pa = new PasswordAuthentication("xxxxx@163.com", "123456");
    return pa;
   }
  };
  //3,获取session对象
  Session session = Session.getDefaultInstance(props, auth);
  //设置session为调试模式
  session.setDebug(true);
  //4,声明信息
  MimeMessage mm1 = new MimeMessage(session);
  //5,设置发件人信息
  Address form = new InternetAddress("xxxxx@163.com");
  mm1.setFrom(form);
  //6,设置收件人 ,RecipientType:发送,抄送,密送 类型
  mm1.setRecipient(RecipientType.TO, new InternetAddress("xxx@qq.com"));
  //mm1.setRecipient(RecipientType.CC, new InternetAddress(""));//抄送
 
  //7,设置主题
  mm1.setSubject("拉面学习通知");
  String cont = "请点击 <a href='http://www.fsy158.com/news/31_207'>官网新闻</a>查看祝您发财";
  mm1.setContent(cont, "text/html;charset=UTF-8");
  
  //8,发送
  Transport.send(mm1);
 
 }

其中的mimeType可是text/plain纯文本。

发送附件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@Test
 public void sendMailWithFile() throws Exception{
 Properties ps = new Properties();
 ps.setProperty("mail.host", "smtp.163.com");
 ps.setProperty("mail.smtp.auth", "true");
 
 Authenticator auth = new Authenticator(){
  @Override
  protected PasswordAuthentication getPasswordAuthentication() {
  return new PasswordAuthentication("xxx@163.com","xxx.");
  }
 };
 Session session = Session.getDefaultInstance(ps, auth);
 session.setDebug(true);
 MimeMessage msg = new MimeMessage(session);
 Address address = new InternetAddress("xxx@163.com");
 msg.setFrom(address);
 //发送给
 msg.setRecipient(RecipientType.TO, new InternetAddress("xxx@qq.com"));
 msg.setRecipient(RecipientType.BCC, new InternetAddress("xxx@qq.com"));//密送
 msg.setRecipient(RecipientType.CC, new InternetAddress("xxx@qq.com"));//抄送
 msg.setSubject("$激情小视频*免费观看");
 
 //声明多部件处理
 MimeMultipart mm = new MimeMultipart();
 
 MimeBodyPart body1 = new MimeBodyPart();
 //设置附件
 DataSource ds1 = new FileDataSource(new File("./img/a.jpg"));
 DataHandler dh1 = new DataHandler(ds1);
 body1.setDataHandler(dh1);
 //必须设置名称
 body1.setFileName(MimeUtility.encodeText("美女.jpg"));
 
 MimeBodyPart body2 = new MimeBodyPart();
 DataSource ds2 = new FileDataSource(new File("./img/b.jpg"));
 DataHandler dh2 = new DataHandler(ds2);
 body2.setDataHandler(dh2);
 body2.setFileName(MimeUtility.encodeText("美女2.jpg"));
 
 MimeBodyPart body3 = new MimeBodyPart();
 DataSource ds3 = new FileDataSource(new File("./img/m.mp3"));
 DataHandler dh3 = new DataHandler(ds3);
 body3.setDataHandler(dh3);
 body3.setFileName(MimeUtility.encodeText("小电影.mp3"));
 
 //添加body到mm
 mm.addBodyPart(body1);
 mm.addBodyPart(body2);
 mm.addBodyPart(body3);
 
 msg.setContent(mm);
 
 //设置发送日期
 Calendar c = Calendar.getInstance();
 c.add(Calendar.DATE, -5);
 msg.setSentDate(c.getTime());
 
 //发送
 Transport.send(msg);
 }

JavaMail实现发送邮件功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/lihaoyang/p/7421795.html

延伸 · 阅读

精彩推荐