TLS/SSL 자바 메 일 사용

7318 단어 javamail
// get the mail configuration options String mailHost     = servletConfig.getServletContext().getInitParameter( "mailHost" ); String mailTo       = servletConfig.getServletContext().getInitParameter( "mailTo" ); String mailFrom     = servletConfig.getServletContext().getInitParameter( "mailFrom" ); String mailUser     = servletConfig.getServletContext().getInitParameter( "mailUser" ); String mailPassword = servletConfig.getServletContext().getInitParameter( "mailPassword" ); String mailTls      = servletConfig.getServletContext().getInitParameter( "mailTls" ); String mailSsl      = servletConfig.getServletContext().getInitParameter( "mailSsl" ); String mailPort     = servletConfig.getServletContext().getInitParameter( "mailPort" ); String mailDebug    = servletConfig.getServletContext().getInitParameter( "mailDebug" );
 // check on the parameters if (mailHost == null || mailTo == null || mailFrom == null ) {    throw new ServletException( "Unable to get mail init parameters" ); } // Build the message StringBuilder message = new StringBuilder( "Start messagen" );
 // add the rest of the message message.apppend( "line by line construct the message" );
 // set the base properties for sending email Properties mailProperties = System.getProperties(); mailProperties.put( "mail.transport.protocol" , "smtp" ); mailProperties.put( "mail.smtp.host" , mailHost);
 // check to see if we need to use a non standard port if (mailPort != null ) {    mailProperties.put( "mail.smtp.port" , mailPort); } // check to see if we need to use SSL if (mailSsl != null ) {    mailProperties.put( "mail.smtp.ssl.enable" , "true" );    mailProperties.put( "mail.smtp.ssl.trust" , "*" );
    if (mailPort != null ) {      mailProperties.put( "mail.smtp.socketFactory.port" , mailPort);    } else {      // assume default standard port for SSL SMTP      mailProperties.put( "mail.smtp.socketFactory.port" , "465" );    }
    mailProperties.put( "mail.smtp.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );    mailProperties.put( "mail.smtp.socketFactory.fallback" , "false" ); } // check to see if we need to output debug messages if (mailDebug != null && mailDebug.equals( "yes" )) {    mailProperties.put( "mail.debug" , "true" ); } // do we need to use TLS? if (mailTls != null && mailTls.equals( "yes" )) {    mailProperties.put( "mail.smtp.starttls.enable" , "true" ); } // start the email session Session mailSession; // do we need to do authentication if (mailUser != null ) {    mailProperties.put( "mail.smtp.auth" , true );    mailSession = Session.getInstance(mailProperties, new SMTPAuthenticator(mailUser, mailPassword)); } else {    mailSession = Session.getInstance(mailProperties, null ); } try {    // construct the message    Message mailMessage = new MimeMessage(mailSession); // base object    mailMessage.setFrom( new InternetAddress(mailFrom)); // set from address    mailMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailTo, false )); // set to address    mailMessage.setSubject( "Email subject" ); // subject    mailMessage.setText(message.toString()); // body of the message
    // add additional headers    mailMessage.setHeader( "X-Mailer" , "JavaMail" );    mailMessage.setSentDate( new Date());
    // send the message    Transport.send(mailMessage);
 } catch (javax.mail.internet.AddressException ex) {    throw new ServletException( "Unable to prepare mail message" , ex); } catch (javax.mail.MessagingException ex) {    throw new ServletException( "Unable to send mail message" , ex); }

좋은 웹페이지 즐겨찾기