Friday, January 8, 2021

How to send email with attachment ?

 public static boolean sendMail(String[] emailto, String sFrom, InternetAddress[] iCC, InternetAddress[] iBCC,

String sSubject, String sMessage, String sAttachment, String host, String port) throws AddressException {


String methodName = "sendMail";

System.out.println(CLASS_NAME + methodName + "Inside : FileName : " + sAttachment);

LOGGER.debug(CLASS_NAME + methodName + "Inside : FileName : " + sAttachment);


// InternetAddress[] to = iAddr;

InternetAddress[] address = new InternetAddress[emailto.length];

int i;

for (i = 0; i < emailto.length; i++) {

address[i] = new InternetAddress(emailto[i]);

System.out.println("To " + address[i]);

}

// String subject = sSubject + "-" + falloutUserVOs.size();

String subject = sSubject;

String from = sFrom;


Properties properties = System.getProperties();

properties.setProperty("mail.smtp.host", host);

properties.setProperty("mail.smtp.port", port);

Session session = Session.getDefaultInstance(properties);

System.out.println(CLASS_NAME + methodName + "Property Added Successfully");

LOGGER.debug(CLASS_NAME + methodName + "Property Added Successfully");


try {

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(from));

message.setRecipients(Message.RecipientType.TO, (Address[]) address);

if (iCC != null) {

message.addRecipients(Message.RecipientType.CC, iCC);

}

if (iBCC != null) {

message.addRecipients(Message.RecipientType.BCC, iBCC);

}

message.setSubject(subject);

if (sAttachment == null || sAttachment.length() == 0) {

message.setContent(sMessage, "text/html");

} else {

System.out.println(CLASS_NAME + methodName + "Recipents Added Successfully");

LOGGER.debug(CLASS_NAME + methodName + "Recipents Added Successfully");

MimeBodyPart mimeBodyPart = new MimeBodyPart();

mimeBodyPart.setContent(sMessage, "text/html");


MimeMultipart mimeMultipart = new MimeMultipart();

mimeMultipart.addBodyPart(mimeBodyPart);


mimeBodyPart = new MimeBodyPart();

String filename = sAttachment;


DataSource source = new FileDataSource(filename);

mimeBodyPart.setDataHandler(new DataHandler(source));


String strPath = filename.substring(filename.lastIndexOf("/") + 1, filename.length());

mimeBodyPart.setFileName(strPath);

mimeMultipart.addBodyPart(mimeBodyPart);

// Send the complete message parts

message.setContent(mimeMultipart);

}

// Send message

Transport.send(message);

System.out.println(CLASS_NAME + methodName + "Sent message successfully....");

LOGGER.debug(CLASS_NAME + methodName + "Sent message successfully....");


} catch (MessagingException mex) {

System.out.println(CLASS_NAME + methodName + " ERROR : " + mex.getMessage());

LOGGER.error(CLASS_NAME + methodName + " ERROR : " + mex.getMessage());

return false;

}

System.out.println("Exiting from the method : " + methodName);

LOGGER.debug("Exiting from the method : " + methodName);

return true;

}


-----------------------------------------------------------------------------------------------------------------------------

tcLookupOperationsIntf lookupOps = Platform.getService(tcLookupOperationsIntf.class);

String toEmails = lookupOps.getDecodedValueForEncodedValue("Lookup.Clarizen.WebService", "TO_EMAIL");

String[] to = toEmails.split(",");

boolean emailStatus1 = sendMail(to, lookupData.get("SENDER_EMAIL"), null, null,

lookupData.get("EMAIL_SUBJECT"), lookupData.get("MESSAGE"), fallout_userFilePath, lookupData.get("SMTP_PRIMARY_HOST"),

lookupData.get("SMTP_PORT"));


No comments:

Post a Comment

Fetch Members from Workgroup

  import java.util.ArrayList;   import java.util.Iterator;   import java.util.List;   import sailpoint.api.ObjectUtil;   import sailpoint.ob...