请问:
JavaMail如何转发带有附件的邮件?
谢谢

解决方案 »

  1.   

    Properties props = new Properties();
            
            //设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
            props.put("mail.smtp.host", host);
            //需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)
            props.put("mail.smtp.auth", "true");
            
            //用刚刚设置好的props对象构建一个session
            Session session = Session.getDefaultInstance(props);
            
            //有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使
            //用(有的时候网络连通性不够好,发送邮件可能会有延迟,在这里面会有所
            //提示,所以最好是加上这句,避免盲目的等待)
            session.setDebug(true);
            
            //用session为参数定义消息对象
            MimeMessage message = new MimeMessage(session);
            try{
             //加载发件人地址
                message.setFrom(new InternetAddress(from));
               //加载收件人地址
                message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
               //加载标题
                message.setSubject(subject);
                
                // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
                Multipart multipart = new MimeMultipart();           
                
                
                //   设置邮件的文本内容
                BodyPart contentPart = new MimeBodyPart();
                contentPart.setText("邮件的具体内容在此");
                multipart.addBodyPart(contentPart);
                //添加附件
                BodyPart messageBodyPart= new MimeBodyPart();
                DataSource source = new FileDataSource(affix);
                //添加附件的内容
                messageBodyPart.setDataHandler(new DataHandler(source));
                //添加附件的标题,这里进行了base64的解码
                sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
                messageBodyPart.setFileName("=?GBK?B?"+enc.encode(affixName.getBytes())+"?=");//          messageBodyPart.setFileName(affixName);
                multipart.addBodyPart(messageBodyPart);
                
                
                //将multipart对象放到message中
                message.setContent(multipart);
    给你一段代码做参考
      

  2.   

    哈哈,共同研究,我现在也在研究javamail,我现在在收邮件时附件的中文乱码问题没有解决,你的中文乱码是怎么解决的?