package com.ese.ils.beta.util;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
/**
* Hilfsklasse zum Versenden von E-Mails
* Verarbeitet jeweils eine Adresse und einen Dateianhang
* Server-Eigenschaften muessen festgelegt werden
* @author eduard walter
*
*/
public class SendEmail {
public static void sendFilePerMail(File file, String eMail) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.1und1.de");
props.put("mail.from", "admin@handmadeweb.de");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.user", "USER");
props.put("mail.password", "PASSWORD");
javax.mail.Authenticator auth = new javax.mail.Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("USER", "PASSWORD");
}
};
Session session = Session.getInstance(props, auth);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("USER", "the ILS-team"));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(eMail, false));
msg.setSubject("group content");
msg.setSentDate(new Date());
msg.setText("Please find attached your wished group-content.");
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(file);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
// create the Multipart and add its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
Transport.send(msg);
System.out.println("DONE");
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}