boolean sendMail ( ) { if ( ! this . isPropertiesSet ( ) ) { logger . debug ( LOG_PREFIX + ": Required Mail Properties are not set. Attachments will not be sent" ) ; return false ; } Properties prop = new Properties ( ) ; prop . put ( "mail.smtp.auth" , true ) ; prop . put ( "mail.smtp.starttls.enable" , "true" ) ; prop . put ( "mail.smtp.host" , this . host ) ; prop . put ( "mail.smtp.port" , this . port ) ; prop . put ( "mail.smtp.ssl.trust" , host ) ; logger . debug ( LOG_PREFIX + ": Mail properties set" ) ; Session session = Session . getInstance ( prop , new Authenticator ( ) { @ Override protected PasswordAuthentication getPasswordAuthentication ( ) { return new PasswordAuthentication ( username , password ) ; } } ) ; logger . debug ( LOG_PREFIX + ": Mail Session Created" ) ; try { // create a default mime message object Message message = new MimeMessage ( session ) ; message . setFrom ( new InternetAddress ( fromAdd ) ) ; message . setRecipients ( Message . RecipientType . TO , InternetAddress . parse ( toAdd ) ) ; message . setSubject ( subject ) ; // body part for message MimeBodyPart mimeBodyPart = new MimeBodyPart ( ) ; mimeBodyPart . setContent ( msg , "text/html" ) ; // body part for attachments MimeBodyPart attachmentBodyPart = new MimeBodyPart ( ) ; logger . debug ( LOG_PREFIX + ": " + this . attachments . length + " attachments found" ) ; for ( File file : this . attachments ) { attachmentBodyPart . attachFile ( file ) ; } // create a multipart to combine them together Multipart multipart = new MimeMultipart ( ) ; multipart . addBodyPart ( mimeBodyPart ) ; multipart . addBodyPart ( attachmentBodyPart ) ; //now set the multipart as the content of the message message . setContent ( multipart ) ; // send the message Transport . send ( message ) ; logger . debug ( LOG_PREFIX + ": MESSAGE SENT SUCCESSFULLY" ) ; return true ; } catch ( Exception e ) { logger . debug ( LOG_PREFIX + ": MESSAGE NOT SENT. " + e . getLocalizedMessage ( ) ) ; e . printStackTrace ( ) ; return false ; } }
This function will send an email and add the generated sql file as an attachment