当前位置:编程学习 > JAVA >>

Java实现发送带附件email

一、目标

用java代码实现发送带附件email的功能

二、学习地址

  

三、前期准备:

增加jar包:javamail1_4_5.zip

 [java] 
public class SendMailService { 
    private MailInformation mailInfor; 
    private static SendMailService sendMailService; 
     
    public void setMailInfor(MailInformation mailInfor) { 
        this.mailInfor = mailInfor; 
    }     
 
        public static SendMailService getService() { 
        if (sendMailService == null) { 
            sendMailService = new SendMailService(); 
        } 
        return sendMailService; 
    } 
    /**
     * 此段代码用来发送普通带附件电子邮件
     */ 
    public void send() throws Exception { 
        try { 
            Properties props = new Properties(); // 获取系统环境 
            Authenticator auth = new Email_Autherticator(); // 进行邮件服务器用户认证 
            props.put("mail.smtp.host", mailInfor.getHost()); 
            props.put("mail.smtp.auth", "true"); 
            Session session = Session.getDefaultInstance(props, auth); 
            // 用于在console中显示执行过程信息 
            session.setDebug(true); 
            // 设置session,和邮件服务器进行通讯。 
            MimeMessage message = new MimeMessage(session); 
            message.setSubject(mailInfor.getMail_subject()); // 设置邮件标题 
            message.setSentDate(new Date()); // 设置邮件发送日期 
            Address address = new InternetAddress(mailInfor.getSendEmail(), 
                    mailInfor.getSendPassword()); 
            message.setFrom(address); // 设置邮件发送者的地址 
            Address toAddress = new InternetAddress(mailInfor.getMail_to()); // 设置邮件接收方的地址 
            message.addRecipient(Message.RecipientType.TO, toAddress);// 加载收件人地址 
 
            message.setText(mailInfor.getMail_body()); // 设置邮件正文 
            // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件 
            Multipart multipart = new MimeMultipart(); 
            // 设置邮件的文本内容 
            BodyPart contentPart = new MimeBodyPart(); 
            contentPart.setText(mailInfor.getMail_body()); 
            multipart.addBodyPart(contentPart); 
            multipart.addBodyPart(contentPart); 
            if (!mailInfor.getFile().isEmpty()) {// 有附件 
                Enumeration efile = mailInfor.getFile().elements(); 
                while (efile.hasMoreElements()) { 
                    contentPart = new MimeBodyPart(); 
                    String filename = efile.nextElement().toString(); // 选择出每一个附件名 
                    FileDataSource fds = new FileDataSource(filename); // 得到数据源 
                    contentPart.setDataHandler(new DataHandler(fds)); // 得到附件本身并至入BodyPart 
                    // contentPart.setFileName(fds.getName()); 
                    // //得到文件名同样至入BodyPart,并解决中文名乱码问题 
                    contentPart.setFileName(MimeUtility.encodeText(fds 
                            .getName())); 
                    multipart.addBodyPart(contentPart); 
                } 
                mailInfor.getFile().removeAllElements(); 
            } 
            // 将multipart对象放到message中 
            message.setContent(multipart, "text/html;charset=gb2312"); // Multipart加入到信件 
            // 保存邮件 
            message.saveChanges(); 
            Transport.send(message); // 发送邮件 
             
            System.out.println("send ok!"); 
 
        } catch (Exception ex) { 
            ex.printStackTrace(); 
        

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,