Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package e.mail;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Properties;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.activation.DataHandler;
- import javax.activation.DataSource;
- import javax.activation.FileDataSource;
- import javax.mail.BodyPart;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.AddressException;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- /**
- *
- * @author Michael
- */
- public class EMail {
- private final String from;
- private final String smtp;
- private final String username;
- private final String password;
- public EMail(String from, String smtp, String username, String password) {
- this.from = from;
- this.smtp = smtp;
- this.username = username;
- this.password = password;
- }
- public void sendMail(File mail, String to) {
- Properties properties = System.getProperties();
- properties.put("mail.smtp.host", smtp);
- properties.put("mail.smtp.starttls.enable", "true");
- Session session = Session.getInstance(properties);
- try {
- MimeMessage message = new MimeMessage(session, new FileInputStream(mail));
- InternetAddress[] Recipients = {new InternetAddress(to)};
- message.setRecipients(Message.RecipientType.TO, Recipients);
- message.setFrom(new InternetAddress(from));
- Transport transport = session.getTransport("smtp");
- transport.connect(username, password);
- transport.sendMessage(message, Recipients);
- } catch (MessagingException ex) {
- Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
- } catch (FileNotFoundException ex) {
- Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public void sendMail(File mail, File RecipientsFile) {
- try {
- Properties properties = System.getProperties();
- properties.put("mail.smtp.host", smtp);
- properties.put("mail.smtp.starttls.enable", "true");
- Session session = Session.getInstance(properties);
- ArrayList<String> Recipientlist = readRecipientsFromFile(RecipientsFile);
- InternetAddress[] Recipients = StringArraylistToInternetadressArray(Recipientlist);
- try {
- MimeMessage message = new MimeMessage(session, new FileInputStream(mail));
- message.setFrom(new InternetAddress(from));
- message.setRecipients(Message.RecipientType.TO, Recipients);
- Transport transport = session.getTransport("smtp");
- transport.connect(username, password);
- transport.sendMessage(message, Recipients);
- } catch (MessagingException ex) {
- Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
- } catch (FileNotFoundException ex) {
- Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
- }
- } catch (AddressException ex) {
- Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public void sendMail(File mail, File RecipientsFile, File Attachment) {
- try {
- Properties properties = System.getProperties();
- properties.put("mail.smtp.host", smtp);
- properties.put("mail.smtp.starttls.enable", "true");
- Session session = Session.getInstance(properties);
- ArrayList<String> Recipientlist = readRecipientsFromFile(RecipientsFile);
- InternetAddress[] Recipients = StringArraylistToInternetadressArray(Recipientlist);
- try {
- MimeMessage message = new MimeMessage(session);//, new FileInputStream(mail));
- message.setFrom(new InternetAddress(from));
- message.setRecipients(Message.RecipientType.TO, Recipients);
- Multipart multipart = new MimeMultipart();
- BodyPart bodyPart = new MimeBodyPart();
- bodyPart.setText(readFromFile(mail));
- multipart.addBodyPart(bodyPart);
- BodyPart attachment = new MimeBodyPart();
- DataSource file = new FileDataSource(Attachment);
- attachment.setDataHandler(new DataHandler(file));
- attachment.setFileName("db.pdf");
- multipart.addBodyPart(attachment);
- message.setContent(multipart);
- Transport transport = session.getTransport("smtp");
- transport.connect(username, password);
- transport.sendMessage(message, Recipients);
- } catch (MessagingException ex) {
- Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
- }
- } catch (AddressException ex) {
- Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- private String readFromFile(File file){
- String text = "";
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader(file));
- for (String empfänger = reader.readLine(); empfänger != null; empfänger = reader.readLine()) {
- text += empfänger + "\n";
- }
- } catch (FileNotFoundException ex) {
- Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
- }
- return text;
- }
- private ArrayList<String> readRecipientsFromFile(File file) {
- ArrayList<String> Recipients = new ArrayList<>();
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader(file));
- for (String empfänger = reader.readLine(); empfänger != null; empfänger = reader.readLine()) {
- Recipients.add(empfänger);
- }
- } catch (FileNotFoundException ex) {
- Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
- } finally {
- try {
- reader.close();
- } catch (IOException ex) {
- Logger.getLogger(EMail.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- return Recipients;
- }
- private InternetAddress[] StringArraylistToInternetadressArray(ArrayList<String> Recipientlist) throws AddressException {
- InternetAddress[] Recipients = new InternetAddress[Recipientlist.size()];
- for (int i = 0; i < Recipientlist.size(); i++) {
- Recipients[i] = new InternetAddress(Recipientlist.get(i));
- }
- return Recipients;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement