Sample send Email code
Posted by chapconnor on 2008-12-13 23:13
public static boolean sendEmail(String textMessage, String htmlMessage, ArrayList<String> to, String from, ArrayList<String> cc, String subject) throws MessagingException {
try {
// set up the InetAddress arrays
InternetAddress[] toArray = new InternetAddress[to.size()];
InternetAddress[] ccArray = new InternetAddress[cc.size()];
int x = 0, y = 0;
for (String item : to)
toArray[x++] = new InternetAddress(item);
for (String item : cc)
ccArray[y++] = new InternetAddress(item);
// Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", Config.getProperty("smtp-host"));
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
// create a message
Message msg = new MimeMessage(session);
MimeMultipart mp = new MimeMultipart();
BodyPart tp = new MimeBodyPart();
tp.setText(textMessage);
mp.addBodyPart(tp);
tp = new MimeBodyPart();
tp.setContent(htmlMessage, "text/html");
mp.addBodyPart(tp);
mp.setSubType("alternative");
msg.addRecipients(Message.RecipientType.TO, toArray);
msg.addRecipients(Message.RecipientType.CC, ccArray);
msg.setFrom(new InternetAddress(from));
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(mp);
Transport.send(msg);
} catch (MessagingException me) {
throw new MessagingException(me.toString());
}
return true;
}
try {
// set up the InetAddress arrays
InternetAddress[] toArray = new InternetAddress[to.size()];
InternetAddress[] ccArray = new InternetAddress[cc.size()];
int x = 0, y = 0;
for (String item : to)
toArray[x++] = new InternetAddress(item);
for (String item : cc)
ccArray[y++] = new InternetAddress(item);
// Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", Config.getProperty("smtp-host"));
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
// create a message
Message msg = new MimeMessage(session);
MimeMultipart mp = new MimeMultipart();
BodyPart tp = new MimeBodyPart();
tp.setText(textMessage);
mp.addBodyPart(tp);
tp = new MimeBodyPart();
tp.setContent(htmlMessage, "text/html");
mp.addBodyPart(tp);
mp.setSubType("alternative");
msg.addRecipients(Message.RecipientType.TO, toArray);
msg.addRecipients(Message.RecipientType.CC, ccArray);
msg.setFrom(new InternetAddress(from));
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(mp);
Transport.send(msg);
} catch (MessagingException me) {
throw new MessagingException(me.toString());
}
return true;
}
Home / Developer API / Tour / Get a Project - Solutions for Bug & Issue Tracking, Collaboration Tools, Subversion Hosting, Git Hosting
Ahs webdev is powered by Assembla.
0 Comments