打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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>

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JavaMail API详解
JavaMail:在Web应用下完整接收、解析复杂邮件
在JSP中使用JavaMail(2)
javamail的几个实用知识点.
Java邮件开发-----JavaMail(三)
用JavaMail API编写可带附件的邮件发送程序
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服