javamail初试

需要引用的包:

mail.jar,imap.jar,mailapi.jar,pop3.jar,smtp.jar,activation.jar

只有activation.jar不在javamail包里,需要另外下载

int result = 0;
        Properties props = System.getProperties();
        props.put("mail.smtp.host", MAIL_SMTP_HOST);
        if (MAIL_SMTP_AUTH)
            props.put("mail.smtp.auth", "true");
        Session session = Session.getInstance(props, null);
        Message msg = new MimeMessage(session);
        try {
            msg.setFrom(new InternetAddress(MAIL_FROM));
            msg.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(MAIL_TO, false));
            msg.setSubject(title);
            msg.setHeader("X-Mailer", "smtpsend");
            msg.setSentDate(new Date());
            msg.setText(body);
            SMTPTransport t = (SMTPTransport) session.getTransport("smtp");
            if (MAIL_SMTP_AUTH)
                t.connect(MAIL_SMTP_HOST, MAIL_FROM_USERNAME, MAIL_FROM_PASSWORD);
            else
                t.connect();
            t.sendMessage(msg, msg.getAllRecipients());
            t.close();
        } catch (MessagingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            result = -1;
        }
        return result;
    }

你可能感兴趣的:(javamail)