Helma logo
helma.org » Home > docs > examples > Using JavaMail directly

Using JavaMail directly

The following code-snippet can be used to send multi-mime-parts emails and is also a great example for how to use Java-Classes directly from within Hop.

In order for this to work you need the JavaMail-library from Sun.

var from = "me@home.com";
var to = "you@home.com";
var title = req.data.title;
var msg = req.data.message;
var file = "c:\\hop\\static\\image.gif";

mail = Packages.javax.mail;
activation = Packages.javax.activation;
var p = java.lang.System.getProperties();
p.put("mail.smtp.host", getProperty("smtp"));
var s = mail.Session.getInstance(p,null);
var m = new mail.internet.MimeMessage(s);

m.setFrom(new mail.internet.InternetAddress(from));
m.addRecipient(mail.Message.RecipientType.TO, 
         new mail.internet.InternetAddress(to));
m.setSubject(title);

var mText = new mail.internet.MimeBodyPart();
mText.setText(msg);

var mImage = new mail.internet.MimeBodyPart();
var mImageFileDataSource = new activation.FileDataSource(file);
mImage.setDataHandler(
         new activation.DataHandler(mImageFileDataSource));

var mp = new mail.internet.MimeMultipart();
mp.addBodyPart(mText);
mp.addBodyPart(mImage);
m.setContent(mp);
mail.Transport.send(m);
sendmail.hac

thanks to chris for the snippet!


Up: Code Snippets
Previous: FTP an uploaded file Next: Image Processing

... comment


Page last modified on 2003-05-13 14:10 by hns