Created
May 17, 2010 23:39
-
-
Save tlrobinson/404386 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var default_settings = { | |
| host : "HOST", | |
| port : "PORT", | |
| username : "USER", | |
| password : "PASSWORD", | |
| encrypt : false | |
| }; | |
| var SMTP_DEBUG = false; | |
| function mail(sender, recipients, subject, body, attachments, headers, settings) | |
| { | |
| if (!settings) | |
| var settings = default_settings; | |
| var props = new Packages.java.util.Properties(); | |
| props.put("mail.smtp.host", settings.host); | |
| props.put("mail.smtp.port", settings.port); | |
| props.put("mail.smtp.user", settings.username); | |
| props.put("mail.smtp.auth", "true"); | |
| props.put("mail.from", sender); | |
| if (SMTP_DEBUG) | |
| props.put("mail.debug", "true"); | |
| if (settings.encrypt) | |
| { | |
| if (settings.port == 465) | |
| { | |
| props.put("mail.smtp.socketFactory.port", settings.port); | |
| props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); | |
| props.put("mail.smtp.socketFactory.fallback", "false"); | |
| props.put("mail.smtp.ssl", "true"); | |
| } | |
| else if (settings.port == 587) | |
| { | |
| props.put("mail.smtp.starttls.enable", "true"); | |
| } | |
| } | |
| var session = Packages.javax.mail.Session.getInstance(props, | |
| new JavaAdapter(Packages.javax.mail.Authenticator, { | |
| getPasswordAuthentication: function() { | |
| return new Packages.javax.mail.PasswordAuthentication(settings.username, settings.password); | |
| } | |
| }) | |
| ); | |
| if (SMTP_DEBUG) | |
| session.setDebug(true); | |
| print("Mailing (sender="+sender+", recipients="+recipients+", subject="+subject+")"); | |
| try { | |
| var message = new Packages.javax.mail.internet.MimeMessage(session); | |
| message.setFrom(); | |
| message.setRecipients(Packages.javax.mail.Message.RecipientType.TO, recipients); | |
| message.setSubject(subject); | |
| if (headers) | |
| { | |
| for (var header in headers) | |
| { | |
| //mail.Headers.Add("Reply-To", "alternate_email@mycompany.com" ); | |
| message.addHeader(header, headers[header]); | |
| } | |
| } | |
| var mp = new Packages.javax.mail.internet.MimeMultipart(); | |
| var mbp = new Packages.javax.mail.internet.MimeBodyPart(); | |
| mbp.setText(body); | |
| mp.addBodyPart(mbp); | |
| if (typeof attachments == "string") | |
| attachments = [attachments]; | |
| for (var i = 0; attachments && i < attachments.length; i++) | |
| { | |
| print("attaching " + attachments[i]); | |
| var mbp = new Packages.javax.mail.internet.MimeBodyPart(); | |
| var fds = new Packages.javax.activation.FileDataSource(attachments[i]); | |
| mbp.setDataHandler(new Packages.javax.activation.DataHandler(fds)); | |
| mbp.setFileName(fds.getName()); | |
| mp.addBodyPart(mbp); | |
| } | |
| message.setContent(mp); | |
| message.setSentDate(new Date()); | |
| Packages.javax.mail.Transport.send(message); | |
| } catch (e) { | |
| print("Mailing failed, exception: " + e); | |
| return false; | |
| } | |
| return true; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment