前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java发送邮件

Java发送邮件

作者头像
xiaoxi666
发布2018-10-29 17:20:13
1.4K0
发布2018-10-29 17:20:13
举报
文章被收录于专栏:xiaoxi666的专栏

参考了“菜鸟教程”和stack overflow

准备工作

将mail.jar和activation.jar加入classpath.

说明

发送方为163邮箱,需要设置host等参数。

接收方可以是其他种类邮箱,比如qq邮箱等。

简单邮件发送

注意创建session时需要传入授权参数,否则会抛出异常 javax.mail.AuthenticationFailedException: failed to connect, no password specified?

代码语言:javascript
复制
 1 public static void Send163Email() {
 2         String to = "to@163.com";
 3         String from = "from@163.com";
 4         
 5         Properties properties = System.getProperties();
 6 
 7         properties.put("mail.host", "smtp.163.com");
 8         properties.put("mail.transport.protocol", "smtp");
 9         properties.put("mail.smtp.auth", true);
10         
11         Session session = Session.getDefaultInstance(properties, 
12                 new javax.mail.Authenticator(){
13                     protected PasswordAuthentication getPasswordAuthentication() {
14                         return new PasswordAuthentication(
15                             "from@163.com", "授权码");// Specify the Username and the PassWord
16                     }
17             });
18         session.setDebug(true);
19         
20         MimeMessage message = new MimeMessage(session);
21 
22         try {
23             message.setFrom(new InternetAddress(from));
24             message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
25             message.setSubject("This is the Subject Line!");
26             message.setText("This is actual message.");
27             Transport.send(message);
28             System.out.println("Sent message sucessfully!");
29         } catch (MessagingException mex) {
30             mex.printStackTrace();
31         }
32     }

带附件邮件发送

代码语言:javascript
复制
 1 public static void Send163EmailWithAttachment() {
 2         String to = "to@163.com";
 3         String from = "from@163.com";
 4         
 5         Properties properties = System.getProperties();
 6 
 7         properties.put("mail.host", "smtp.163.com");
 8         properties.put("mail.transport.protocol", "smtp");
 9         properties.put("mail.smtp.auth", true);
10         
11         Session session = Session.getDefaultInstance(properties, 
12                 new javax.mail.Authenticator(){
13                     protected PasswordAuthentication getPasswordAuthentication() {
14                         return new PasswordAuthentication(
15                             "from@163.com", "授权码");// Specify the Username and the PassWord
16                     }
17             });
18         session.setDebug(true);
19         
20         MimeMessage message = new MimeMessage(session);
21 
22         try {
23             message.setFrom(new InternetAddress(from));
24             message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
25             message.setSubject("This is the Subject Line(Text and file)!");
26 
27             BodyPart messageBodyPart=new MimeBodyPart();
28             messageBodyPart.setText("This is a message body.\n2017年11月28日");
29             Multipart multipart=new MimeMultipart();
30             multipart.addBodyPart(messageBodyPart);
31             //attachment
32             messageBodyPart=new MimeBodyPart();
33             String filename="文件名(带路径)";
34             DataSource source=new FileDataSource(filename);
35             messageBodyPart.setDataHandler(new DataHandler(source));
36             messageBodyPart.setFileName(filename);
37             multipart.addBodyPart(messageBodyPart);
38             
39             message.setContent(multipart);
40             Transport.send(message);
41             System.out.println("Sent message sucessfully!");
42         } catch (MessagingException mex) {
43             mex.printStackTrace();
44         }
45     }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-11-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 准备工作
  • 说明
  • 简单邮件发送
  • 带附件邮件发送
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档