java mail 发送邮件
来源:互联网 发布:早教管理软件 编辑:程序博客网 时间:2024/06/09 20:35
能抄送、密送、附件
注意:1、javaMail需要一个Properties来注册Session
2、邮箱密码是用邮箱里面对stmp的授权码 而不是登录密码
3、用到两个jar包 mail.jar和activator.jar
MailSender.java
import java.io.File;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.DataSource;import javax.activation.FileDataSource;import javax.mail.BodyPart;import javax.mail.Message;import javax.mail.Multipart;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;import javax.mail.internet.MimeUtility;public class MailSender { private String host = ""; // smtp private String from = ""; // 发件地址 private String to = ""; // 收件地址 private String cc = ""; //抄送地址 private String bcc = "";//密送地址 private String affix = ""; // 附件地址 private String user = ""; // 发件邮箱名 private String pwd = ""; // 邮箱stmp授权码 private String subject = ""; // 邮件标题 private String content = "";// 邮件内容 public void send(String host, String user, String pwd,File attachment,String from, String to, String cc , String bcc , String subject,String content) { this.host = host; this.user = user; this.pwd = pwd; this.from = from; this.to = to; this.cc = cc; this.bcc = bcc; this.subject = subject; this.content = content; Properties props = new Properties(); // 设置邮箱服务器的Properties props.put("mail.smtp.host", host); // 设置通过服务器认证 props.put("mail.smtp.auth", "true"); // 创建一个 session Session session = javax.mail.Session.getInstance(props); // 开启debug模式 可以logcat上看发件时的信息 session.setDebug(true); MimeMessage message = new MimeMessage(session); try { Multipart multipart = new MimeMultipart(); // 设置发件地址 message.setFrom(new InternetAddress(from)); // 设置接收地址 //多个收件地址 String[] eachRecipients = null; eachRecipients = to.split("\\|"); for(int i=0;i<eachRecipients.length;i++){ String recipient = eachRecipients[i]; message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); } //多个抄送地址 String[] cceachRecipients = null; cceachRecipients=cc.split("\\|"); for(int i=0;i<cceachRecipients.length;i++){ String recipient = cceachRecipients[i]; message.addRecipient(Message.RecipientType.CC, new InternetAddress(to)); } //密送地址 message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc)); // 设置邮件标题 message.setSubject(subject); // 设置邮件内容 BodyPart contentPart = new MimeBodyPart(); contentPart.setText(content); multipart.addBodyPart(contentPart); // 设置附件 if (attachment != null) { BodyPart attachmentBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(attachment); attachmentBodyPart.setDataHandler(new DataHandler(source)); //防止附件发出后中文变乱码 attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName())); multipart.addBodyPart(attachmentBodyPart); } message.setContent(multipart); message.saveChanges(); Transport transport = session.getTransport("smtp"); // 连接服务器 transport.connect(host, user, pwd); // 发送 transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (Exception e) { e.printStackTrace(); } }}
调用的时候就这样
File affix = new File("D:/AAAAAA.xls"); String from = "a@126.com"; String to = "b@126.com,c@126.com"; String cc = "d@126.com,e@126.com"; String bcc = "f@126.com"; String title = "邮件标题 asddfad"; String content = "邮件内容aadsfasdf12334#!!#%¥#¥…………&(&*+——“:?"; MailSender cn = new MailSender(); // 设置smtp服务器以及邮箱的帐号和密码 cn.send("smtp.126.com", "a@126.com", "password", affix, from, to, cc, bcc, title, content);
就这么多了
0 0
- Java Mail 发送邮件
- JAVA MAIL发送邮件
- Java Mail 发送邮件
- Java mail发送邮件
- java mail 发送邮件
- Java Mail发送邮件
- java mail邮件发送
- java mail 发送邮件
- Java mail发送邮件
- java mail发送邮件
- Java mail 发送邮件
- java mail发送邮件
- java mail 发送邮件
- java mail 发送邮件
- java mail 发送邮件
- Java mail发送邮件
- Java-Mail 发送邮件
- java mail发送邮件
- jquery 给对面添加属性和值
- js菜单自适应的实现
- RHEL3:使用rdesktop远程控制windows
- 人件读书笔记(33)“邪恶”电邮
- 高德地图API的应用
- java mail 发送邮件
- EAS webservice串用户、串数据中心问题
- apache httpd/apache2配置文件小结
- [Leetcode] Palindrome Partitioning II
- 微信开发消息自动回复和自定义菜单简述。
- WEB APP、HYBRID APP与NATIVE APP 差异分析
- Python 爬虫笔记(由站内到站外爬虫)
- 在linux 桌面环境中使用windows 应用
- Web前端面试指导(十八):用纯CSS创建一个三角形的原理是什么?