package utils;
/*
* Mail
*/
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
public class Mail{
private static String mailhost = "mail.company.lu";//anpassen!
private static String mailer = "msgsend";
public static void sendMail(String text, String to, String subject, String from, String cc, String bcc){
try {
Properties props = System.getProperties();
props.put("mail.smtp.host", mailhost);
// Get a Session object
Session session = Session.getInstance(props, null);
// construct the message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
if (cc != null){
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
}
if (bcc != null){
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));
}
msg.setSubject(subject);
// If the desired charset is known, you can use
// setText(text, charset)
msg.setText(text);
msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new Date());
// send the thing off
Transport.send(msg);
System.out.println("\nMail was sent successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}