Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static Properties properties = new Properties();
- {
- properties.put("mail.smtp.host", getText("smtp.host"));
- properties.put("mail.smtp.socketFactory.port", getText("socketFactory.port"));
- properties.put("mail.smtp.socketFactory.class", getText("socketFactory.class"));
- properties.put("mail.smtp.auth", getText("smtp.auth"));
- properties.put("mail.smtp.port", getText("smtp.port"));
- properties.put("mail.smtp.starttls.enable", "true");
- }
- private String from = getText("from");
- private String smtppw = getText("pw");
- private void initMS() {
- if (smtppw != null) {
- mailsession = Session.getDefaultInstance(properties, new Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(from, smtppw);
- }
- });
- }
- else {
- mailsession = Session.getDefaultInstance(properties, new Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(from, null);
- }
- });
- }
- }
- String attachpath = ServletActionContext.getServletContext().getRealPath("/");
- public String sendNote(String type, SendingParamsDTO sendingParams, String filename) throws /*AddressException,*/ MessagingException {
- String res = SUCCESS;
- //try {
- if (mailsession == null) initMS();
- Message message = new MimeMessage(mailsession);
- InternetAddress pobox = new InternetAddress(from);
- InternetAddress to[] = InternetAddress.parse(sendingParams.getMailto());
- message.setFrom(pobox);
- message.setRecipients( Message.RecipientType.TO, to);
- message.addHeader("Disposition-Notification-To", from);
- message.setSubject("[AN-V.C.O.] - "+sendingParams.getSubject() );
- message.setSentDate( sendingParams.getDate() );
- /**/
- logger.info(" ## FROM: "+from);
- logger.info(" ## TO: "+to);
- logger.info(" ## SUBJECT: "+sendingParams.getSubject());
- logger.info(" ## MESSAGE: "+sendingParams.getMessage());
- if (filename != null)
- logger.info(" ## ATTACH: "+attachpath + filename);
- /**/
- MimeMultipart multipart = new MimeMultipart("related");
- //Multipart multipart = new MimeMultipart();
- BodyPart messageBodyPart1 = new MimeBodyPart();
- if (type.equals("text"))
- messageBodyPart1.setText(sendingParams.getMessage());
- else if (type.equals("html"))
- messageBodyPart1.setContent(sendingParams.getMessage(), "text/html");
- BodyPart messageBodyPart2 = null;
- if (filename != null){
- DataSource source = new FileDataSource(attachpath);
- messageBodyPart2 = new MimeBodyPart();
- messageBodyPart2.setDataHandler( new DataHandler(source) );
- messageBodyPart2.setFileName( filename );
- }
- multipart.addBodyPart( messageBodyPart1 );
- if (filename != null){
- multipart.addBodyPart( messageBodyPart2 );
- }
- message.setContent(multipart);
- Transport.send(message, message.getRecipients(Message.RecipientType.TO));
- //Transport transport = mailsession.getTransport();
- //transport.connect();
- //transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
- //transport.close();
- //}
- //catch(Exception e) {
- // res = ERROR;
- // e.printStackTrace();
- //}
- return res;
- }
- ## generic using GMAIL
- smtp.host=smtp.gmail.com
- socketFactory.port=465
- socketFactory.class=javax.net.ssl.SSLSocketFactory
- smtp.auth=true
- smtp.port=465
- ## real using myprovider
- smtp.host=smtp.myprovider.com
- #socketFactory.port=465
- #socketFactory.class=javax.net.ssl.SSLSocketFactory
- smtp.auth=true
- smtp.port=465
- static Properties properties = new Properties();
- {
- properties.put("mail.smtp.host", getText("smtp.host"));
- //properties.put("mail.smtp.socketFactory.port", getText("socketFactory.port"));
- //properties.put("mail.smtp.socketFactory.class", getText("socketFactory.class"));
- properties.put("mail.smtp.auth", getText("smtp.auth"));
- properties.put("mail.smtp.port", getText("smtp.port"));
- //properties.put("mail.smtp.starttls.enable", "true");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement