Advertisement
Guest User

Untitled

a guest
Nov 10th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.58 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package e.mail;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.FileNotFoundException;
  12. import java.io.FileReader;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.Properties;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import javax.activation.DataHandler;
  19. import javax.activation.DataSource;
  20. import javax.activation.FileDataSource;
  21. import javax.mail.BodyPart;
  22. import javax.mail.Message;
  23. import javax.mail.MessagingException;
  24. import javax.mail.Multipart;
  25. import javax.mail.Session;
  26. import javax.mail.Transport;
  27. import javax.mail.internet.AddressException;
  28. import javax.mail.internet.InternetAddress;
  29. import javax.mail.internet.MimeBodyPart;
  30. import javax.mail.internet.MimeMessage;
  31. import javax.mail.internet.MimeMultipart;
  32.  
  33. /**
  34.  *
  35.  * @author Michael
  36.  */
  37. public class EMail {
  38.  
  39.     private final String from;
  40.     private final String smtp;
  41.     private final String username;
  42.     private final String password;
  43.  
  44.     public EMail(String from, String smtp, String username, String password) {
  45.         this.from = from;
  46.         this.smtp = smtp;
  47.         this.username = username;
  48.         this.password = password;
  49.     }
  50.  
  51.     public void sendMail(File mail, String to) {
  52.  
  53.         Properties properties = System.getProperties();
  54.         properties.put("mail.smtp.host", smtp);
  55.         properties.put("mail.smtp.starttls.enable", "true");
  56.  
  57.         Session session = Session.getInstance(properties);
  58.  
  59.         try {
  60.             MimeMessage message = new MimeMessage(session, new FileInputStream(mail));
  61.             InternetAddress[] Recipients = {new InternetAddress(to)};
  62.             message.setRecipients(Message.RecipientType.TO, Recipients);
  63.             message.setFrom(new InternetAddress(from));
  64.  
  65.             Transport transport = session.getTransport("smtp");
  66.             transport.connect(username, password);
  67.             transport.sendMessage(message, Recipients);
  68.         } catch (MessagingException ex) {
  69.             Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
  70.         } catch (FileNotFoundException ex) {
  71.             Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
  72.         }
  73.     }
  74.  
  75.     public void sendMail(File mail, File RecipientsFile) {
  76.  
  77.         try {
  78.             Properties properties = System.getProperties();
  79.             properties.put("mail.smtp.host", smtp);
  80.             properties.put("mail.smtp.starttls.enable", "true");
  81.  
  82.             Session session = Session.getInstance(properties);
  83.  
  84.             ArrayList<String> Recipientlist = readRecipientsFromFile(RecipientsFile);
  85.             InternetAddress[] Recipients = StringArraylistToInternetadressArray(Recipientlist);
  86.  
  87.             try {
  88.                 MimeMessage message = new MimeMessage(session, new FileInputStream(mail));
  89.                 message.setFrom(new InternetAddress(from));
  90.                 message.setRecipients(Message.RecipientType.TO, Recipients);
  91.                
  92.                 Transport transport = session.getTransport("smtp");
  93.                 transport.connect(username, password);
  94.                 transport.sendMessage(message, Recipients);
  95.             } catch (MessagingException ex) {
  96.                 Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
  97.             } catch (FileNotFoundException ex) {
  98.                 Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
  99.             }
  100.         } catch (AddressException ex) {
  101.             Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
  102.         }
  103.     }
  104.    
  105.     public void sendMail(File mail, File RecipientsFile, File Attachment) {
  106.  
  107.         try {
  108.             Properties properties = System.getProperties();
  109.             properties.put("mail.smtp.host", smtp);
  110.             properties.put("mail.smtp.starttls.enable", "true");
  111.  
  112.             Session session = Session.getInstance(properties);
  113.  
  114.             ArrayList<String> Recipientlist = readRecipientsFromFile(RecipientsFile);
  115.             InternetAddress[] Recipients = StringArraylistToInternetadressArray(Recipientlist);
  116.  
  117.             try {
  118.                 MimeMessage message = new MimeMessage(session);//, new FileInputStream(mail));
  119.                 message.setFrom(new InternetAddress(from));
  120.                 message.setRecipients(Message.RecipientType.TO, Recipients);
  121.                
  122.                 Multipart multipart = new MimeMultipart();
  123.                
  124.                 BodyPart bodyPart = new MimeBodyPart();
  125.                 bodyPart.setText(readFromFile(mail));
  126.                 multipart.addBodyPart(bodyPart);
  127.                
  128.                 BodyPart attachment = new MimeBodyPart();
  129.                 DataSource file = new FileDataSource(Attachment);
  130.                 attachment.setDataHandler(new DataHandler(file));
  131.                 attachment.setFileName("db.pdf");
  132.                 multipart.addBodyPart(attachment);
  133.                
  134.                 message.setContent(multipart);
  135.                
  136.                 Transport transport = session.getTransport("smtp");
  137.                 transport.connect(username, password);
  138.                 transport.sendMessage(message, Recipients);
  139.             } catch (MessagingException ex) {
  140.                 Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
  141.             }
  142.         } catch (AddressException ex) {
  143.             Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
  144.         }
  145.     }
  146.    
  147.     private String readFromFile(File file){
  148.         String text = "";
  149.         BufferedReader reader = null;
  150.         try {
  151.             reader = new BufferedReader(new FileReader(file));
  152.             for (String empfänger = reader.readLine(); empfänger != null; empfänger = reader.readLine()) {
  153.                 text += empfänger + "\n";
  154.             }
  155.         } catch (FileNotFoundException ex) {
  156.             Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
  157.         } catch (IOException ex) {
  158.             Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
  159.         }
  160.         return text;
  161.     }
  162.  
  163.     private ArrayList<String> readRecipientsFromFile(File file) {
  164.         ArrayList<String> Recipients = new ArrayList<>();
  165.         BufferedReader reader = null;
  166.         try {
  167.             reader = new BufferedReader(new FileReader(file));
  168.             for (String empfänger = reader.readLine(); empfänger != null; empfänger = reader.readLine()) {
  169.                 Recipients.add(empfänger);
  170.             }
  171.         } catch (FileNotFoundException ex) {
  172.             Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
  173.         } catch (IOException ex) {
  174.             Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
  175.         } finally {
  176.             try {
  177.                 reader.close();
  178.             } catch (IOException ex) {
  179.                 Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
  180.             }
  181.         }
  182.         return Recipients;
  183.     }
  184.  
  185.     private InternetAddress[] StringArraylistToInternetadressArray(ArrayList<String> Recipientlist) throws AddressException {
  186.         InternetAddress[] Recipients = new InternetAddress[Recipientlist.size()];
  187.         for (int i = 0; i < Recipientlist.size(); i++) {
  188.             Recipients[i] = new InternetAddress(Recipientlist.get(i));
  189.         }
  190.         return Recipients;
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement