`

java发送邮件(html格式,包括纯文本)

 
阅读更多

public void sendMail()
            throws Exception {

        try {

            SubAuthenticator subauth = getSubAuthenticator(username, password);
            // authenticator
            props.put("mail.smtp.host", smtphost);
            props.put("mail.smtp.auth ", "true");
            session = Session.getInstance(props, subauth);
            MimeMessage message = new MimeMessage(session);
           
            Iterator i = toAddress.iterator();
            while (i.hasNext()){
                InternetAddress address = new InternetAddress((String)i.next());
                message.addRecipient(Message.RecipientType.TO, address);
            }
            if (ccAddress != null) {
                Iterator it = ccAddress.iterator();
                while (it.hasNext()){
                    InternetAddress address = new InternetAddress((String)it.next());
                    message.addRecipient(Message.RecipientType.CC, address);
                }
            }
           
            //add table style liubinbin 20120803
            MimeBodyPart mbp = new MimeBodyPart(); 
            mbp.setContent(content, "text/plain;charset=UTF-8");
            mp.addBodyPart(mbp,0);
            message.setFrom(from);
            message.setSubject(subject);
//            message.setText(content);
            message.setContent(mp);
           
            Transport.send(message);
            log.info("Send an email ");
        } catch (AuthenticationFailedException e1) {
            System.out.println("SMTP认证出错! ");
        } catch (MessagingException e) {
            throw new Exception(e.getMessage());
        }
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics