使用javamail发送带附件的邮件

本文使用Orielly上传包实现

验证类:

package  com.test.auth;

import  javax.mail.Authenticator;
import  javax.mail.PasswordAuthentication;

public   class  Authentic  extends  Authenticator  {
    
protected PasswordAuthentication getPasswordAuthentication() {
          String username 
= "daryl715";    //大多数是你邮件@前面的部分
          String pwd = "smallfish1009";
          
return new PasswordAuthentication(username, pwd);
         }


}

 

发送页面:

<% @ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding
="GB18030"
%>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=GB18030" >
< title > Insert title here </ title >
</ head >
< body >
< form  name ="sendmail"  action ="javafilemail.jsp"  method ="post"  enctype ="multipart/form-data" >
  发信人:
< input  type ="text"  name ="from" />< br >
  收件人:
< input  type ="text"  name ="recieve" />< br >
  主题:
&nbsp;&nbsp; < input  type ="text"  name ="subject" />< br >
  内容:
< textarea  name ="content"  cols =40  rows =5 ></ textarea >< br >
  
< input  type ="submit"  value ="发送" >
  附件:
< input  type ="file"  name ="filename" />
  
</ form >
</ body >
</ html >

 发送代码:

     <% @ page import="javax.mail.*"  %>
    
<% @ page import="javax.mail.internet.*"  %>
    
<% @ page import="javax.activation.*"  %>
    
<% @ page import="java.util.*,java.io.*"  %>
    
<% @ page import="com.test.auth.*,com.oreilly.servlet.MultipartRequest"  %>
    
<% @ page contentType="text/html;charset=GB2312"  %>
    
    
< html >
    
< head >
      
< title > CH17 - JavaMail.jsp </ title >
    
</ head >
    
< body >
    
    
< h2 > 利用JavaMail来传送电子邮件  </ h2 >
    
<%
        InternetAddress[] address 
= null;
    
        request.setCharacterEncoding(
"GB2312");
     
        
String saveDirectory="/upload"//设置上传目录
        
String contextPath=request.getSession().getServletContext().getRealPath(saveDirectory);
        File a
=new File(contextPath);
       
        
if(!a.isDirectory()){
          a.mkdir();
        }
       
int maxPostSize=5*1024*1024//设定大小为5M
       
String enCoding="gb2312";
    
       
int count=0;
       MultipartRequest multi
=new MultipartRequest(request,contextPath,maxPostSize,enCoding);
       
String mailserver   = "202.108.5.142";
        
String From         = multi.getParameter("from");
        
String to           = multi.getParameter("recieve");
        
String Subject      = multi.getParameter("subject");
        
String messageText  = multi.getParameter("content");
        
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");
      
// 产生新的Session 服务
     
      
      Authentic auth 
= new Authentic();
    
     
    Session mailSession 
= Session.getDefaultInstance(props, auth);
      mailSession.setDebug(sessionDebug);
    
    
      Message msg 
= new MimeMessage(mailSession);
      
      
// 设定传送邮件的发信人
      System.out.println(From);
      msg.setFrom(
new InternetAddress(From));
      
      
// 设定传送邮件至收信人的信箱
      address 
= InternetAddress.parse(to,false);
      msg.setRecipients(Message.RecipientType.TO, address);
    
      
// 设定信中的主题 
      msg.setSubject(Subject);
      
// 设定送信的时间
      msg.setSentDate(
new Date());
      
      System.out.println(filename);
      
if(filename!=null){//如果有附件,就暂存邮件,处理附件后统一发送
        File file
=new File(filename);
        MimeBodyPart mbp1
=new MimeBodyPart();
        mbp1.setContent(messageText,
"text/html;charset=gb2312");
        
        FileDataSource fds
=new FileDataSource(filename);
        MimeBodyPart mbp2
=new MimeBodyPart();
        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,
"text/html;charset=gb2312");
      }
      
      
      
      
     
      
      
// 设定传送信的MIME Type
     
    
      
      
// 送信
      
      Transport.send(msg);
    
      out.println(
"邮件已顺利传送");
        
    }
        catch (MessagingException mex) {
    
          mex.printStackTrace();
        }
    
%>
    
    
</ body >
    
</ html >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答关于 JavaMail 发送邮件附件的问题。 在 JavaMail发送附件邮件,可以通过以下步骤实现: 1. 创建一个 Session 对象,用于与邮件服务器进行通信。 ``` Properties props = new Properties(); props.setProperty("mail.smtp.host", "smtp.example.com"); props.setProperty("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("your_username", "your_password"); } }); ``` 2. 创建一个 MimeMessage 对象,设置邮件的基本信息,如发件人、收件人、主题等。 ``` MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@example.com")); message.addRecipient(Message.RecipientType.TO, new InternetAddress("recipient@example.com")); message.setSubject("Test email with attachment"); ``` 3. 创建一个 Multipart 对象,用于包含邮件正文和附件。 ``` Multipart multipart = new MimeMultipart(); ``` 4. 创建一个 MimeBodyPart 对象,用于包含邮件正文,并将其添加到 Multipart 对象中。 ``` MimeBodyPart bodyPart = new MimeBodyPart(); bodyPart.setText("This is the email body"); multipart.addBodyPart(bodyPart); ``` 5. 创建一个 MimeBodyPart 对象,用于包含附件,并将其添加到 Multipart 对象中。 ``` MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.attachFile(new File("path/to/attachment")); attachmentPart.setFileName("attachment.txt"); multipart.addBodyPart(attachmentPart); ``` 6. 将 Multipart 对象设置为 MimeMessage 对象的内容,并发送邮件。 ``` message.setContent(multipart); Transport.send(message); ``` 以上是 JavaMail 发送附件邮件的基本步骤,你可以根据实际需求进行修改和扩展。希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值