Java发送带附件的QQ邮箱

由于腾讯公司给QQ邮箱增加了一个授权码的密码保护,导致之前网上很多代码都不能用,于是就自己敲了一份demo。

注意在密码那里可能需要授权码,具体设置:http://service.mail.qq.com/cgi-bin/help?id=28

jar:javax.mail.jar

  1 package xhw;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.BufferedWriter;
  5 import java.io.File;
  6 import java.io.FileNotFoundException;
  7 import java.io.FileOutputStream;
  8 import java.io.OutputStream;
  9 import java.io.OutputStreamWriter;
 10 import java.io.UnsupportedEncodingException;
 11 import java.util.Properties;
 12 
 13 import javax.activation.DataHandler;
 14 import javax.activation.DataSource;
 15 import javax.activation.FileDataSource;
 16 import javax.mail.BodyPart;
 17 import javax.mail.Message;
 18 import javax.mail.Multipart;
 19 import javax.mail.Session;
 20 import javax.mail.Transport;
 21 import javax.mail.internet.InternetAddress;
 22 import javax.mail.internet.MimeBodyPart;
 23 import javax.mail.internet.MimeMessage;
 24 import javax.mail.internet.MimeMultipart;
 25 
 26 public class SendMail2 {
 27 
 28     private String host = ""; // smtp服务器
 29     private String from = ""; // 发件人地址
 30     private String to = ""; // 收件人地址
 31     private String affix = ""; // 附件地址
 32     private String affixName = ""; // 附件名称
 33     private String user = ""; // 用户名
 34     private String pwd = ""; // 密码
 35     private String subject = ""; // 邮件标题
 36 
 37     public void setAddress(String from, String to, String subject) {
 38         this.from = from;
 39         this.to = to;
 40         this.subject = subject;
 41     }
 42 
 43     public void setAffix(String affix, String affixName) {
 44         this.affix = affix;
 45         this.affixName = affixName;
 46     }
 47 
 48     public void send(String host, String user, String pwd) {
 49         this.host = host;
 50         this.user = user;
 51         this.pwd = pwd;
 52 
 53         Properties props = new Properties();
 54 
 55         // 设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
 56         props.put("mail.smtp.host", host);
 57         // 需要经过授权,也就是有户名和密码的校验,这样才能通过验证
 58         props.put("mail.smtp.auth", "true");
 59         props.put("mail.smtp.port", 465);
 60         props.put("mail.smtp.ssl.enable", true);
 61         // 用刚刚设置好的props对象构建一个session
 62         Session session = Session.getDefaultInstance(props);
 63 
 64         // 有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使
 65         // 用(你可以在控制台(console)上看到发送邮件的过程)
 66         session.setDebug(true);
 67 
 68         // 用session为参数定义消息对象
 69         MimeMessage message = new MimeMessage(session);
 70         try {
 71             // 加载发件人地址
 72             message.setFrom(new InternetAddress(from));
 73             // 加载收件人地址
 74             message.addRecipient(Message.RecipientType.TO, new InternetAddress(
 75                     to));
 76             // 加载标题
 77             message.setSubject(subject);
 78 
 79             // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
 80             Multipart multipart = new MimeMultipart();
 81 
 82             // 设置邮件的文本内容
 83             BodyPart contentPart = new MimeBodyPart();
 84             contentPart.setText("第二种方法···");
 85             multipart.addBodyPart(contentPart);
 86             // 添加附件
 87             BodyPart messageBodyPart = new MimeBodyPart();
 88             DataSource source = new FileDataSource(affix);
 89             // 添加附件的内容
 90             messageBodyPart.setDataHandler(new DataHandler(source));
 91             // 添加附件的标题
 92             // 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
 93             sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
 94             messageBodyPart.setFileName("=?GBK?B?"
 95                     + enc.encode(affixName.getBytes()) + "?=");
 96             multipart.addBodyPart(messageBodyPart);
 97 
 98             // 将multipart对象放到message中
 99             message.setContent(multipart);
100             // 保存邮件
101             message.saveChanges();
102             // 发送邮件
103             Transport transport = session.getTransport("smtp");
104             // 连接服务器的邮箱
105             transport.connect(host, user, pwd);
106             // 把邮件发送出去
107             transport.sendMessage(message, message.getAllRecipients());
108             transport.close();
109         } catch (Exception e) {
110             e.printStackTrace();
111         }
112     }
113 
114     public static void main(String[] args) {
115 
116         File file = new File("D:\content.csv");
117         try {
118             OutputStream os = new FileOutputStream(file);
119             BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,
120                     "utf-8"));
121             bw.write("hello");
122             bw.close();
123             os.close();
124         } catch (Exception e) {
125             // TODO Auto-generated catch block
126             e.printStackTrace();
127         }
128             
129         
130         SendMail2 cn = new SendMail2();
131         // 设置发件人地址、收件人地址和邮件标题
132         cn.setAddress("发件人地址", "收件人地址", "一个带附件的JavaMail邮件(标题)");
133         // 设置要发送附件的位置和标题
134         cn.setAffix("D:\content.csv", "content.csv");
135         // 设置smtp服务器以及邮箱的帐号和密码
136         cn.send("smtp.qq.com", "账号", "密码(授权码)");
137     }
138 }
原文地址:https://www.cnblogs.com/xhw123xhw/p/5380067.html