Java 发送邮件&发送正文&添加附件
来源:互联网 发布:携程 知乎 编辑:程序博客网 时间:2024/05/16 08:21
发送邮件案例
1.简单设置发送邮件内容
2.载体设置发送邮件内容
3.载体发送邮件内容+发送附件
public void sendEmailMsg(){
Properties props = new Properties();
props.put("mail.smtp.host","smtp.exmail.qq.com");//邮件服务器端口
props.put("mail.smtp.auth","true"); //是否验证用户名和密码正确
props.put("from_user","from_user"); //发送者
props.put("pwd","password"); //密码
props.put("to_user","to_user");//接收者
Session session = Session.getDefaultInstance(props); //创建session javax.mail.Session
// 开启debug调试
session.setDebug(false);
try {
//发送源地址
InternetAddress fromAddress =new InternetAddress(props.getProperty("from_user"));
//发送到地址
InternetAddress toAddress =new InternetAddress(props.getProperty("to_user"));
//创建邮件载体
MimeMessage message =new MimeMessage(session);
//设定发件邮箱地址
message.setFrom(fromAddress);
//设定收件邮箱地址
//RecipientType.TO -- 收件人
//RecipientType.CC -- 抄送人
//RecipientType.BCC -- 暗送人[不显示发件人信息]
message.addRecipient(RecipientType.TO,toAddress);
message.addHeader("charset","UTF-8");
message.setSentDate(Calendar.getInstance().getTime());
//标题信息 Title
message.setSubject("标题信息");
/****************最简单的内容信息发送************************/
message.setContent("<div align=\"center\">你好啊</div>","text/html;charset=utf-8");
Transport transport0 =session.getTransport("smtp");
transport0.connect(props.getProperty("mail.smtp.host"),props.getProperty("from_user"),props.getProperty("pwd"));
//发送邮件
transport0.sendMessage(message,message.getAllRecipients());
/****************************************/
//添加正文内容
/***********直接发送<不含有附件>***************/
Multipart multipart =new MimeMultipart();
BodyPart contentPart =new MimeBodyPart();
contentPart.setText("Text内容");
multipart.addBodyPart(contentPart);
message.setContent(multipart);
Transport transport =session.getTransport("smtp");
transport.connect(props.getProperty("mail.smtp.host"),props.getProperty("from_user"),props.getProperty("pwd"));
//发送邮件
transport.sendMessage(message,message.getAllRecipients());
/***********end**************/
/***********添加附件&设置正文信息**************/
//添加附件
File usFile = new File("filepath");
//添加附件内容
MimeBodyPart fileBody =new MimeBodyPart();
DataSource source =new FileDataSource(usFile);
//附件信息添加在载体内容中
fileBody.setDataHandler(new DataHandler(source));
fileBody.setText("这里是正文信息..............");
//处理中文文件名乱码
fileBody.setFileName(MimeUtility.encodeText(usFile.getName()));
multipart.addBodyPart(fileBody);
message.setContent(multipart);
message.setSentDate(new Date());
message.saveChanges();
Transport transport2 =session.getTransport("smtp");
transport2.connect(props.getProperty("mail.smtp.host"),props.getProperty("from_user"),props.getProperty("pwd"));
//发送邮件
transport2.sendMessage(message,message.getAllRecipients());
transport2.close();
/***********end**************/
} catch (Exceptione) {
e.printStackTrace();
}
}
- Java 发送邮件&发送正文&添加附件
- java发送邮件,正文图片,附件
- java实现发送邮件与添加附件
- java发送邮件并添加附件
- java 发送邮件(并添加附件)
- java 发送邮件+附件
- Java 发送邮件【附件】
- spring邮件发送,正文附件发送的源码
- PHP邮件发送类,添加发送附件
- [php]mail函数发送邮件(正文+附件+中文)
- 使用python登录邮箱并发送邮件(正文,附件)
- erlang实现smtp发送邮件(包含网页、正文、附件)
- 邮件正文及其附件的发送的C++实现
- 邮件正文及其附件的发送的C++实现
- (三)JavaMail 发送邮件之正文 && 附件
- 邮件正文及其附件的发送的C++实现
- java Mail发送附件邮件
- java发送带附件邮件
- 使用WebStrom开发设置ReactNative关于JSX中的XML部分的智能提示插件的安装办法
- Vue2+VueRouter2+webpack 构建项目实战(三)配置路由,整俩页面先(上)
- SVN服务器搭建与使用
- 算法高级进阶简述
- SQL索引一步到位
- Java 发送邮件&发送正文&添加附件
- mysql kill process解决死锁
- Android开发环境搭建
- 从零开始的DataBinding使用笔记
- (转)Android Transition框架介绍&使用
- APK安装成功后点击"打开"再按Home键应用会重启的问题
- 几种布局方式
- 合金相图与热力学计算软件
- win10主机不能ping 通虚拟机 ,虚拟机能ping 主机 onedrive 不能自动同步解决