Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Properties;
- import javax.activation.DataHandler;
- import javax.activation.DataSource;
- import javax.activation.FileDataSource;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- public class SendMessage implements Runnable {
- private final String line, alphanumeric;
- public SendMessage(String line, String alphanumeric) {
- this.line = line;
- this.alphanumeric = alphanumeric;
- }
- @Override
- public void run() {
- System.out.println(line);
- String HOST = line.split( "," )[ 0 ];
- final String USER = line.split( "," )[ 1 ];
- final String PASS = line.split( "," )[ 2 ];
- String RECPT = line.split( "," )[ 3 ];
- String FIRST = line.split( "," )[ 4 ];
- String LAST = line.split( "," )[ 5 ];
- String DOB = line.split( "," )[ 6 ];
- String PASSPORT = line.split( "," )[ 7 ];
- String PURPOSE = line.split( "," )[ 8 ];
- String PHONE = line.split( "," )[ 9 ];
- String EMAIL = line.split( "," )[ 10 ];
- String FILE1 = line.split( "," )[ 11 ];
- String FILE2 = line.split( "," )[ 12 ];
- String MSG = "\r\n" + "Dear Sir/Madam" + "\r\n";
- MSG += "\r\n" + "Please check attached file below: " + " \r\n";
- MSG += "\r\n" + "FIRST AND SUR NAME: " + FIRST + " " + LAST + "\r\n";
- MSG += "DATE OF BIRTH: " + DOB + "\r\n";
- MSG += "PASSPORT NUMBER: " + PASSPORT + "\r\n";
- MSG += "PURPOSE OF TRAVEL: " + PURPOSE + "\r\n";
- MSG += "PHONE: " + PHONE + "\r\n";
- MSG += "EMAIL: " + EMAIL + "\r\n";
- MSG += "\r\n" + "Thanks";
- MSG += "\r\n" + FIRST + " " + LAST + "\r\n \r\n \r\n";
- System.out.println( MSG );
- try
- {
- Properties props = new Properties();
- props.put("mail.smtp.host", HOST );
- props.put("mail.smtp.socketFactory.port", "465" );
- props.put("mail.smtp.socketFactory.class",
- "javax.net.ssl.SSLSocketFactory" );
- props.put("mail.smtp.auth", "true" );
- props.put("mail.smtp.port", "587" );
- Session session = Session.getDefaultInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication( USER, PASS );
- }
- });
- try {
- Message message = new MimeMessage(session);
- message.setFrom( new InternetAddress( EMAIL ) );
- message.setRecipients( Message.RecipientType.TO,
- InternetAddress.parse( RECPT ) );
- message.setSubject( alphanumeric );
- message.setText( "" );
- MimeBodyPart messageBodyPart = new MimeBodyPart();
- Multipart multipart = new MimeMultipart();
- MimeBodyPart textPart = new MimeBodyPart();
- textPart.setText( MSG, "utf-8" );
- multipart.addBodyPart(textPart);
- message.setContent(multipart);
- messageBodyPart = new MimeBodyPart();
- String file = FILE1;
- String fileName = FILE1;
- DataSource source = new FileDataSource(file);
- messageBodyPart.setDataHandler(new DataHandler(source));
- messageBodyPart.setFileName(fileName);
- multipart.addBodyPart(messageBodyPart);
- MimeBodyPart messageBodyPart2 = new MimeBodyPart();
- String file2 = FILE2;
- String fileName2 = FILE2;
- DataSource source2 = new FileDataSource(file2);
- messageBodyPart2.setDataHandler(new DataHandler(source2));
- messageBodyPart2.setFileName(fileName2);
- multipart.addBodyPart(messageBodyPart2);
- Transport.send(message);
- System.out.println("Done");
- } catch (MessagingException e) {
- throw new RuntimeException(e);
- }
- }
- catch( Exception e )
- {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment