@Grab(group="javax.mail", module="mail", version="latest.integration") import java.security.* import java.util.* import javax.mail.* import javax.mail.internet.* import javax.activation.* 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, "recipient@gmail.com") msg.setSubject("画像付きHTMLメールサンプル","iso-2022-jp") multipart = new MimeMultipart() // 本文のパートを作成 bp1 = new MimeBodyPart() def writer = new StringWriter() def html = new MarkupBuilder(writer) html.doubleQuotes = true html.html { body { p { mkp.yield("画像付きHTMLメールサンプル") br() img(src:"cid:img1", style:"display:block;") br() mkp.yield("sanfrancisco") } } } bp1.setContent(writer.toString(),"text/html; charset=iso-2022-jp") multipart.addBodyPart(bp1) // 添付ファイルのパートを作成 bp2 = new MimeBodyPart() fds = new FileDataSource("SF.JPG") bp2.setFileName(MimeUtility.encodeWord(fds.getName())) bp2.setDataHandler(new DataHandler(fds)) bp2.setHeader("Content-ID", "<img1>") multipart.addBodyPart(bp2) msg.setContent(multipart) Transport.send(msg)動作環境
groovy 1.8.2, JDK6 update27
0 件のコメント:
コメントを投稿