用JavaMail发送带附件的邮件

时间:2022-01-05 18:10:04

源代码: 下载 (右击->目标另存为, 下载后将扩展名改为.rar)

以新浪为例,发附件要用到上传的组件,O'Reilly公司的cos.jar。请将cos.jar、mail.jar和activation.jar,放到你的WEB应用程序的WEB-INF/lib目录下。
测试用的JavaMail3.html
<html>
<head>
  <title>JavaMail3.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>
<body>

<h2>利用JavaMail来发送电子邮件 - 附件</h2>
<form name="SendMessage" Method="post" action="JavaMail3.jsp" enctype="multipart/form-data">
  <p>寄信人:<input type="text" name="From" size="30" maxlength="30"></p>
  <p>收信人:<input type="text" name="To" size="30" maxlength="30"></p>
  <p>主题:<input type="text" name="Subject" size="30" maxlength="30"></p>
  <p>格式:<select name="Type" size="1">
<option value="text/plain">Text</option>
<option value="text/html">HTML</option>
           </select></p>
  <p>附件:<input type="file" name="FileName" size="20" maxlength="20"></p>         
  <p>内容:</p><p><textarea name="Message" cols=40 rows=5></textarea></p>
  
  <input type="submit" value="发送">
  <input type="reset" value="重填">
</form>

</body>
</html>
处理附件与邮件的JavaMail3.jsp
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%@ page import="java.util.*,java.io.*" %>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page contentType="text/html;charset=GB2312" %>

<html>
<head>
  <title>CH17 - JavaMail2.jsp</title>
</head>
<body>

<h2>利用JavaMail来发送电子邮件 - 附件</h2>

<%
InternetAddress[] address = null;

request.setCharacterEncoding("gb2312");
MultipartRequest multi = new MultipartRequest(request , "." , 5*1024*1024 , "GB2312");

String mailserver   = "smtp.sina.com.cn";
String From         = multi.getParameter("From");
String to           = multi.getParameter("To");
String Subject      = multi.getParameter("Subject");
String type     = multi.getParameter("Type");
String messageText  = multi.getParameter("Message");
String FileName     = multi.getFilesystemName("FileName");

   boolean sessionDebug = false;

try {

   // 设定所要用的Mail 服务器和所使用的传输协议
   java.util.Properties props = System.getProperties();
 
   props.put("mail.host",mailserver);
   props.put("mail.transport.protocol","smtp");
             props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证

   // 产生新的Session 服务
   javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(props,null);
  mailSession.setDebug(sessionDebug);
   Message msg = new MimeMessage(mailSession);
   // 设定发信人
   msg.setFrom(new InternetAddress(From));
     // 设定收信人
   address = InternetAddress.parse(to,false);
   msg.setRecipients(Message.RecipientType.TO, address);
     // 设定信中的主题 
   msg.setSubject(Subject);
     // 设定送信的时间
   msg.setSentDate(new Date());

   if (FileName != null)
   {
        File file = new File(FileName);
              
   // 如果有附件,先将邮件内容部分存起来
           MimeBodyPart mbp1 = new MimeBodyPart();
      
     // 设定邮件内容的类型为 text/plain 或 text/html
   mbp1.setContent(messageText, type + ";charset=GB2312");

           // 再来对附件作处理
           MimeBodyPart mbp2 = new MimeBodyPart();
           FileDataSource fds = new FileDataSource(FileName);
           mbp2.setDataHandler(new DataHandler(fds));
           mbp2.setFileName(MimeUtility.encodeText(fds.getName(), "GB2312", "B"));

           // 最后再将两者整合起来,当作一份邮件送出
           Multipart mp = new MimeMultipart();
           mp.addBodyPart(mbp1);
           mp.addBodyPart(mbp2);
           msg.setContent(mp);
    }
    else
    {
           // 若没有附件时,就直接存邮件内容
           msg.setContent(messageText,type + ";charset=GB2312");
    }             
              Transport transport = mailSession.getTransport("smtp");
              //请填入你邮箱的用户名和密码,千万别用我的^_^。
              transport.connect(mailserver, "zz3zcwb", "1234567");
              transport.sendMessage(msg, msg.getAllRecipients());
              transport.close();
  out.println("邮件已顺利发送");

}
     catch (MessagingException mex) 
     {
 out.println(mex);
         mex.printStackTrace();
}
%>
</body>
</html>