javax.메일 보내기

1698 단어
   /* @param fromMail   
    * @param user   
    * @param password   
    * @param toMail   
    * @param mailTitle   
    * @param mailContent   
    * @throws Exception 
    */
    public static void sendMail(String fromMail, String user, String password, String toMail, String mailTitle, String mailContent) throws Exception {
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);

        Properties props = new Properties();
        props.put("mail.smtp.host", "mail.*****.com");
        props.put("mail.smtp.port", 465);
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getInstance(props);

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromMail));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(toMail));
        message.setSubject(mailTitle);
        message.setText(mailContent);
        message.setSentDate(new Date());
        message.saveChanges();
        Transport transport = session.getTransport();
        transport.connect(user, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

다음은 추가해야 할 패키지입니다.
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>

좋은 웹페이지 즐겨찾기