cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

We are happy to announce the new Windchill Customization board! Learn more.

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

jchan
3-Visitor

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

wt.mail.EMailMessage message = wt.mail.EMailMessage.newEMailMessage();

message.setSubject("Mail Subject");

message.addRecipient(user,1);     //recipients paramInt = 1

message.addRecipient(boss,2); //ccRecipients paramInt = 2

message.addAttachments(??????); //Help Me

message.setOriginator((WTPrincipal)wtsender);

message.addPart("Mail Content","text/html");

message.send(true);

6 REPLIES 6
syadala
5-Regular Member
(To:jchan)

Hi James Chan,

            You need to define a String array and populate your object references in there. then try.

String[] attachments = new String[1];

attachments[0] = "add reference to your object";

wt.mail.EMailMessage message = wt.mail.EMailMessage.newEMailMessage();

message.setSubject("Mail Subject");

message.addRecipient(user,1);     //recipients paramInt = 1

message.addRecipient(boss,2); //ccRecipients paramInt = 2

message.addAttachments(attachments);

message.setOriginator((WTPrincipal)wtsender);

message.addPart("Mail Content","text/html");

message.send(true);

I have not tried this one. If you think it is not working then you can try using javax.mail.internet.MimeMessage

Regards

Sudhakar

jchan
3-Visitor
(To:syadala)

String[] strArrayOfString = new String[1]; ;

strArrayOfString[0] = "VR:wt.doc.WTDocument:488784";               // What data type could add reference to the object? It can't work.

strArrayOfString[0] = "D://ptc//Windchill//Windchill//null.log";        // What data type could add reference to the object? It also can't work.

WTUser wtsender = getUserFromUserName("Administrator");

wt.mail.EMailMessage message = wt.mail.EMailMessage.newEMailMessage();

message.setSubject("Mail Subject");

message.addRecipient(user,1);     //recipients paramInt = 1

message.addRecipient(boss,2); //ccRecipients paramInt = 2

message.addAttachments(strArrayOfString);

message.addPart("Mail Content","text/html");

Test fail!

syadala
5-Regular Member
(To:jchan)

Hi James Chan,

           Please let me know what you are intending to send? A Windchill Object or a file?

Regards

Sudhakar

jchan
3-Visitor
(To:syadala)

Hi Sudhakar,

We need to send email for user with attached file.

These files will be the primary content and attachments from the WTDocument, or database query results after export to excel file.

I want to use Windchill OOTB API to implement for this user requirement.

Thanks !

Best Regards,

James Cha

Hi All,

Any update on this thread?

EMailMessage mail=EMailMessage.newEMailMessage();

mail.setSubject("For Windchill test");

mail.addRecipient(user);

mail.addPart("Hellllllllllllllllo ","text/html");

String attachement[] = new String[1];

attachement[0] = "C://Users//660588//Desktop//a.pdf";

mail.addAttachments(attachement);

mail.send(true);

Mail is delivered but  without attachement. pls provide help on this

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

Top Tags