javamail发送邮件带多个附件

<%@ page contentType="text/html;charset=GB2312"%>
<%@page import="javax.activation.FileDataSource"%>
<%@page import="javax.activation.DataHandler"%>
<%
 request.setCharacterEncoding("gb2312");
%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>发送成功</title>
</head>

<body>
<%
 try {
  String tto = request.getParameter("to");
  String ttitle = request.getParameter("title");
  String tcontent = request.getParameter("content");

  Properties props = new Properties();
  props.put("mail.smtp.host", "168.33.112.27");//发件人使用发邮件的电子信箱服务器
  props.put("mail.smtp.auth", "true"); //这样才能通过验证
  Session s = Session.getInstance(props);
  s.setDebug(true);

  MimeMessage message = new MimeMessage(s);

  //给消息对象设置发件人/收件人/主题/发信时间
  InternetAddress from = new InternetAddress("aps-jcj@cnaps.com.cn"); //发邮件的出发地(发件人的信箱)
  message.setFrom(from);
  InternetAddress to = new InternetAddress(tto);//发邮件的目的地(收件人信箱)
  message.setRecipient(Message.RecipientType.TO, to);
  message.setSubject(ttitle);
  message.setSentDate(new Date());

  //给消息对象设置内容
  BodyPart mdp = new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
  mdp.setContent(tcontent, "text/html;charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
  Multipart mm = new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对
  //象(事实上可以存放多个)
  mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
  //把mm作为消息对象的内容

  
  
  MimeBodyPart   mimebodypart1;
  FileDataSource
        filedatasource;
        //逐个加入附件
        for   (int   i   =   0;   i   <   2;   i++)   {
            mimebodypart1   =   new   MimeBodyPart();
            filedatasource   =   new   FileDataSource("D:\\1.txt");
            mimebodypart1.setDataHandler(new   DataHandler(filedatasource));
            mimebodypart1.setFileName(filedatasource.getName());
            mm.addBodyPart(mimebodypart1);
        }
        message.setContent(mm);
  
  
  
  
  message.saveChanges();
  Transport transport = s.getTransport("smtp");
  transport.connect("168.33.112.27", "aps-jcj@cnaps.com.cn", "1234567");//发邮件人帐户密码
  transport.sendMessage(message, message.getAllRecipients());
  transport.close();
%>
<div align="center">
<p><font color="#FF6600">发送成功!</font></p>
</div>
<%
 } catch (MessagingException e) {
  out.println(e.toString());
 }
%>

</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值