Skip to content

Instantly share code, notes, and snippets.

@jsorge
Created February 27, 2014 00:50
Show Gist options
  • Save jsorge/9242032 to your computer and use it in GitHub Desktop.
Save jsorge/9242032 to your computer and use it in GitHub Desktop.
SendGroovyMail
/*
SendGroovyMail ( smtpHost; username; password; to; from; subject; body; cc; bcc; attachments; html )
Params:
smtpHost: the smtp server
username: the smtp username
password: the smtp password
to: list of email addresses for the to field
from: the email address to be sent from
subject: the subject of the message
body: the body of the message
OPTIONAL cc: list of email addresses for the cc field
OPTIONAL bcc: list of email addresses for the bcc field
OPTIONAL attachments: list of paths for files to attach
OPTIONAL html: BOOL - set to 1 if sending html email
*/
if (smtpHost == null || username == null || password == null || to == null || from == null || subject == null || body == null) {
throw new IllegalArgumentException("One of the required parameters is missing");
}
import javax.mail.*;
import javax.mail.internet.*;
/*******************************Set properties and create the message*******************************/
Properties props = new Properties();
props.setProperty("mail.smtp.host", smtpHost);
props.setProperty("mail.smtp.auth", "true");
props.setProperty(":", "true");
MimeMessage msg = new MimeMessage(Session.getDefaultInstance(props));
/**************************************************************************************************/
/*******************************Set the to/from/cc/bcc/subject*******************************/
msg.setSubject(subject);
msg.setFrom(new InternetAddress(from));
//To
toArray = to.tokenize('\n');
for (int i = 0; i < toArray.size; i++) {
toAddress = toArray[i];
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
}
//CC
if (cc != null) {
ccArray = cc.tokenize('\n');
for (int i = 0; i < ccArray.size; i++) {
ccAddress = ccArray[i];
msg.setRecipient(Message.RecipientType.CC, new InternetAddress(ccAddress));
}
}
//BCC
if (bcc != null) {
bccArray = bcc.tokenize('\n');
for (int i = 0; i < bccArray.size; i++) {
bccAddress = bccArray[i];
msg.setRecipient(Message.RecipientType.BCC, new InternetAddress(bccAddress));
}
}
/********************************************************************************************/
/*******************************Set the message content*******************************/
Multipart content = new MimeMultipart();
String contentType = (Integer.parseInt(html) == 1) ? "text/html" : "text/plain"; //if html == 1, then set to text/html, otherwise text/plain
//Message body
BodyPart messageBody = new MimeBodyPart();
messageBody.setContent(body, contentType);
content.addBodyPart(messageBody);
return "html: " + html + ", content type actual: " + messageBody.getContentType() + ", content type should be: " + contentType;
//Attachments
if (attachments != null) {
attachmentArray = attachments.tokenize('\n');
for ( int i=0; i < attachmentArray.size; i++) {
selectedAttachment = attachmentArray[i];
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(selectedAttachment);
content.addBodyPart(attachmentPart, i); //adds the body part at the index of the array.
}
}
msg.setContent(content);
/*************************************************************************************/
/*******************************Send the message*******************************/
Transport transport = Session.getDefaultInstance(props).getTransport("smtp");
int port = -1; // use the default port
transport.connect(smtpHost, port, username, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
return true
/******************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment