@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.* 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("添付ファイルサンプル","iso-2022-jp") multipart = new MimeMultipart() // 本文のパートを作成 bp1 = new MimeBodyPart() bp1.setText("添付ファイルのサンプル","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)) multipart.addBodyPart(bp2) msg.setContent(multipart) Transport.send(msg)
動作環境
groovy 1.8.2, JDK6 Update22
0 件のコメント:
コメントを投稿