Quantcast
Channel: PTC Community: Message List - Windchill
Viewing all articles
Browse latest Browse all 8876

Re: How can send Email attachments through code with API wt.mail.EMailMessage.addAttachments ?

$
0
0

You can use javax.mail.* API instead of wt.mail.*  to send email with attachment below is sample method i used to send email with attachment 

 

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", "shreyas.atre@bwir.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\\atest.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);  }  }

 

Hope it helps!!!!

 

Thanks

Shreyas


Viewing all articles
Browse latest Browse all 8876

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>