400-685-0732

WJMonitor舆情之声

企业大数据智能舆情监测管理解决方案

全网监测海量数据按需发布监测预警

实时把握舆情动态精准追溯信息源头

获取验证码
企业采购 个人使用
当前位置: 首页 > SEO博客 > 文件以附件的形式发送到邮箱

文件以附件的形式发送到邮箱

时间:2012-06-02 22:53:35
把文件以附件的形式发送到指定的邮箱以下是参考代码.

Properties pro = new Properties();
String protocol = "smtp";//邮箱选择的通信协议
String host = "smtp.163.com";//邮箱的服务器地址
int port = 25;//smtp的端口号
pro.put("mail.smtp.auth", "true");
pro.put("mail.transport.protocol", protocol);
pro.put("mail.smtp.host", host);
pro.put("mail.smtp.port", port);

String send = "hfdiand@163.com";//发件人email地址
String user = "hfdiand@163.com";//发件人帐号
String password = "*********";//密码

String recipient = "kjikad@163.com";收信人的账号

// 建立会话
Session session = Session.getInstance(pro);
Message msg = new MimeMessage(session); // 建立信息
msg.setFrom(new InternetAddress(send));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));

msg.setSentDate(new Date()); // 发送日期

msg.setSubject("Standing on the shoulders of giants"); // 主题

msg.setText("Standing on the shoulders of giants"); // 内容

//设置邮件内容,作为Multipart对象的一部分
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText("I like standing on the shoulders of giants");
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp);

//设置附件内容,作为Multipart对象的一部分
mbp = new MimeBodyPart();
String path = "d://baidu.xls";
DataSource ds = new FileDataSource(path);
mbp.setDataHandler(new DataHandler(ds));
mbp.setFileName(path);
mp.addBodyPart(mbp);

msg.setContent(mp);
// 邮件服务器进行验证
Transport tran = session.getTransport(protocol);
tran.connect(host, user, password);
tran.sendMessage(msg, msg.getAllRecipients()); // 发送
分享按钮