2009-09-09

Groovy SendMail source code

Groovy SendMail code

import javax.mail.*
import javax.mail.internet.*

StringBuffer mailContent = new StringBuffer();

Properties props = new Properties();
props.put("mail.smtp.host", "mail.xxx.com");
Session mailSession = Session.getInstance(props, null);
MimeMessage msg = new MimeMessage(mailSession);

//set sender
msg.setFrom(new InternetAddress("somebody@xxx.com")); //must @xxx.com
mailSubject = 'test send mail from groovy AP'
msg.setSubject(mailSubject);

String toaddr = "007@xxx.com" ;
String cc = "009@xxx.com" ;
String bcc = "011@xxx.com, 013@xxx.com" ;
InternetAddress[] TheAddresses = InternetAddress.parse(toaddr); //(sender);
InternetAddress[] TheAddresses2 = InternetAddress.parse(cc);
InternetAddress[] TheAddresses3 = InternetAddress.parse(bcc); //v001
msg.setRecipients(Message.RecipientType.TO,TheAddresses);
msg.setRecipients(Message.RecipientType.CC,TheAddresses2);
msg.setRecipients(Message.RecipientType.BCC, TheAddresses3);

// ----- Email Content --start--
MimeBodyPart mbp = new MimeBodyPart();

mailContent.append(" Test 您好:\r\n");

mbp.setContent(mailContent.toString(), "text/plain;charset=big5");
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp);
// ----- Email Content --end--

//TODO mail Content
msg.setContent(mp);

//sending email
Transport.send(msg);

//record in log

沒有留言: