How to send emails in Windchill with Attachments

How to send emails in Windchill with Attachments

Add Comment
1 Answer(s)
public static void sendEmail( String cSVfileName, String fileName2) throws WTException, IOException {

  String thePath = null;
  Properties prop = new Properties();
  FileInputStream input = null;
  String theWTCodebaseHome = WTProperties.getLocalProperties().getProperty("wt.codebase.location");
  theWTCodebaseHome = theWTCodebaseHome.replace('\\', '/');
  thePath = theWTCodebaseHome + "/ext/";
  input = new FileInputStream(thePath);
  String to  = prop.getProperty("EMAIL.TO", "").trim();
  // Sender's email ID needs to be mentioned
  String from = prop.getProperty("EMAIL.FROM", "").trim();
  // Assuming you are sending email through
  String host = prop.getProperty("EMAIL.HOST", "localhost").trim();
  Date date = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
  String strDate = sdf.format(date);
  Properties props = new Properties();
  props.put("mail.smtp.auth", "false");
  props.put("mail.smtp.starttls.enable", "false");
  props.put("", host);
  props.put("mail.smtp.port", "25");
  // Get the Session object.
  Session session = Session.getInstance(props);
  try {
  // Create a default MimeMessage object.
  Message message = new MimeMessage(session);
  // Set From: header field of the header.
  message.setFrom(new InternetAddress(from));
  // Set To: header field of the header.
  message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  // Set Subject: header field
  message.setSubject("EMAIL SUBJECT - " + strDate);
  // Create the message part
  BodyPart messageBodyPart = new MimeBodyPart();
  // Now set the actual message
  messageBodyPart.setText("Please see the attached files.");
  // Create a multipar message
  Multipart multipart = new MimeMultipart();
  // Set text message part
  // Part two is attachment
  messageBodyPart = new MimeBodyPart();
  boolean zipExist = false;
  boolean csvExist = false;
  File tempFile = new File(fileName2);
  if (tempFile.exists()) {
  DataSource source = new FileDataSource(fileName2);
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName(strDate + ".zip");
  zipExist = true;
  tempFile = new File(cSVfileName);
  if (tempFile.exists()) {
  messageBodyPart = new MimeBodyPart();
  // String filename = "D:\\temp\\";
  cSVfileName = createXLSFile(cSVfileName);
  DataSource source = new FileDataSource(cSVfileName);
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName(strDate + ".xls");
  csvExist = true;
  // Send the complete message parts
  Address[] allrec = message.getAllRecipients();
  for (Address ad : allrec) {
  // Send message
  System.out.println("Sent message successfully....");
  } catch (MessagingException e) {
  throw new RuntimeException(e);


Or If you would like to use Windchill API’s to send an email attaching¬†windchill objects ( not external file attachments ) use below code snippet.

wt.fc.ObjectIdentifier oid = wt.fc.ObjectIdentifier.newObjectIdentifier("wt.part.WTPart:xxxxx");
wt.part.WTPart wtpart = (wt.part.WTPart)PersistenceHelper.manager.refresh(oid);
EMailMessage localEMailMessage=EMailMessage.newEMailMessage();
localEMailMessage.setSubject("ext.customization.CustomResource","EMAIL_SUBJECT",new String[]{wtpart.getName()}); 
localEMailMessage.addPart("ext.customization.CustomResource","EMAIL_SUBJECT",new String[]{retMsg},"text/plain");



Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.