import java.util.Properties;
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.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* @param to
* @param cc
* @param from
* @param subject
* @param text
*/
public static void getUtilityWithAttachment(String to, String cc, String from, String subject, String text) {
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
ResourceBundle rb = ResourceBundle.getBundle("com.incbidea.appName.view.bundles.AppUIBundle");
String host = rb.getString("SMTP_HOST");
properties.setProperty("mail.smtp.host", host);
// properties.setProperty("mail.smtp.port", "25");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
// message.addRecipient(Message.RecipientType.TO, new InternetAddress.parse(to));
if (to.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
if (cc.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
else
message.setRecipient(Message.RecipientType.CC, new InternetAddress(cc));
// Set Subject: header field
message.setSubject(subject);
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Now set the actual message
//messageBodyPart.setText(text);
messageBodyPart.setContent(text, "text/html");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
String filename = "//home//app//USER-GUIDE.doc";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("NAME_USER-GUIDE.doc");
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
// Now set the actual message
// message.setText(text);
// Send message
Transport.send(message);
//System.out.println("Sent message successfully....");
} catch (MessagingException e) {
String messageText =
"Warning/INFO : EMail works on the SB1/VMQ/PROD . Cannot be sent from local machine; The host is " +
host + "";
FacesMessage fm = new FacesMessage(messageText);
fm.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, fm);
e.printStackTrace();
}
}