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/email.properties";
  input = new FileInputStream(thePath);
  prop.load(input);
  String to  = prop.getProperty("EMAIL.TO", "123@test.com").trim();
  // Sender's email ID needs to be mentioned
  String from = prop.getProperty("EMAIL.FROM", "postMaster@abc.com").trim();
  // Assuming you are sending email through relay.jangosmtp.net
  String host = prop.getProperty("EMAIL.HOST", "localhost").trim();
  Date date = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
  sdf.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
  String strDate = sdf.format(date);
  Properties props = new Properties();
  props.put("mail.smtp.auth", "false");
  props.put("mail.smtp.starttls.enable", "false");
  props.put("mail.smtp.host", 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
  multipart.addBodyPart(messageBodyPart);
  // Part two is attachment
  messageBodyPart = new MimeBodyPart();
  boolean zipExist = false;
  boolean csvExist = false;
  File tempFile = new File(fileName2);
  if (tempFile.exists()) {
  System.out.println(fileName2);
  DataSource source = new FileDataSource(fileName2);
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName(strDate + ".zip");
  multipart.addBodyPart(messageBodyPart);
  zipExist = true;
  }
  tempFile = new File(cSVfileName);
  if (tempFile.exists()) {
  messageBodyPart = new MimeBodyPart();
  // String filename = "D:\\temp\\test.zip";
  System.out.println(cSVfileName);
  cSVfileName = createXLSFile(cSVfileName);
  DataSource source = new FileDataSource(cSVfileName);
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName(strDate + ".xls");
  multipart.addBodyPart(messageBodyPart);
  csvExist = true;
  }
  // Send the complete message parts
  message.setContent(multipart);
  Address[] allrec = message.getAllRecipients();
  for (Address ad : allrec) {
  System.out.println(ad);
  }
  // Send message
  Transport.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.setOriginator(wtpart.getModifier());
localEMailMessage.addRecipient(wtpart.getModifier()); 
localEMailMessage.setSubject("ext.customization.CustomResource","EMAIL_SUBJECT",new String[]{wtpart.getName()}); 
localEMailMessage.addPart("ext.customization.CustomResource","EMAIL_SUBJECT",new String[]{retMsg},"text/plain");
localEMailMessage.send(true);

 

 

Add Comment

Your Answer

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