Sending a data structure as an attachment via JavaMail
Hi, I've managed to send an email with a file as an attachment through JavaMail. However, I'd like to send a data structure (array, vector, etc.) as the attachment instead. One way of course is to export the data structure as a file and then attach that file, however that seems kind of redundant. Is there a way to directly parse the data structure as an attachment? My current partial code for sending an email with an attachment is
Code:
public static void sendMail() throws Exception {
String mailhost = "mailserver.asdasdasd.com";
String from = "fred_liu@asdasdas.com";
String sendTo[] = { "fred_liu@dfasfsdfs.com" };
String sendCC = "";
String subject = "";
String message = "";
String attachment = "C:\\fred.txt";
Properties prop = System.getProperties();
Session session = Session.getInstance(prop, null);
Message msg = new MimeMessage(session);
prop.put("mail.smtp.host", mailhost);
msg.setFrom(new InternetAddress(from));
for (String to : sendTo) {
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
if (sendCC != null && sendCC.length() > 0)
msg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(sendCC, false));
msg.setSubject(subject);
msg.setSentDate(Calendar.getInstance().getTime());
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachment);
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
Transport.send(msg);
}
}
Re: Sending a data structure as an attachment via JavaMail
Re: Sending a data structure as an attachment via JavaMail
Try serializing the object...