@Grab(group="javax.mail", module="mail", version="latest.integration") import java.security.* import java.util.* import javax.mail.* import javax.mail.internet.* import groovy.xml.* smtpHost = "smtp.gmail.com" smtpPort = "465" Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()) Properties props = new Properties() props.put("mail.smtp.host", smtpHost) props.put("mail.smtp.port", smtpPort) props.put("mail.smtp.auth", "true") props.put("mail.smtp.socketFactory.port", smtpPort); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory") props.put("mail.smtp.socketFactory.fallback", "false") Session session = Session.getDefaultInstance(props, [ getPasswordAuthentication: { return new PasswordAuthentication("youraccount@gmail.com", 'yourpassword') } ] as Authenticator ) Message msg = new MimeMessage(session) msg.setFrom(new InternetAddress( "youraccount@gmail.com","youraccount@gmail.com","iso-2022-jp")) msg.setRecipients(Message.RecipientType.TO, "recipients@gmail.com") msg.setSubject("HTMLメールサンプル","iso-2022-jp") import groovy.xml.* def writer = new StringWriter() def html = new MarkupBuilder(writer) html.doubleQuotes = true html.html { body { p { mkp.yield("HTMLメールサンプル") br() a(href:"http://groovyarekore.blogspot.com", "groovyあれこれ") } } } msg.setContent(writer.toString(), "text/html; charset=iso-2022-jp") Transport.send(msg)
動作環境
groovy 1.8.0, JDK6 Update22
0 件のコメント:
コメントを投稿