Created
July 30, 2012 04:01
-
-
Save siosio/3204305 to your computer and use it in GitHub Desktop.
OracleSMIMEを使った電子署名つきメール送信
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mail | |
import oracle.security.crypto.core.AlgID | |
import oracle.security.crypto.smime.SmimeCapabilities | |
import oracle.security.crypto.smime.SmimeMultipartSigned | |
import java.security.KeyStore | |
import javax.activation.CommandMap | |
import javax.activation.DataHandler | |
import javax.activation.FileDataSource | |
import javax.activation.MailcapCommandMap | |
import javax.mail.Message | |
import javax.mail.Session | |
import javax.mail.Transport | |
import javax.mail.internet.InternetAddress | |
import javax.mail.internet.MimeBodyPart | |
import javax.mail.internet.MimeMessage | |
import javax.mail.internet.MimeMultipart | |
/** | |
* KeyStoreからprivateキーや証明書を取得するクラス。 | |
*/ | |
class KeyStoreWrapper { | |
private KeyStore keyStore; | |
private String alias; | |
def initialize() { | |
KeyStore keyStore = KeyStore.getInstance("PKCS12") | |
FileInputStream stream = new FileInputStream("comodo.p12") | |
keyStore.load(stream, "password".chars) | |
stream.close(); | |
alias = keyStore.aliases().find {keyStore.isKeyEntry(it)} | |
this.keyStore = keyStore | |
} | |
def getPrivateKey() { | |
keyStore.getKey(alias, "password".chars) | |
} | |
def getCertificate() { | |
keyStore.getCertificate(alias) | |
} | |
} | |
def createSessionProperties() { | |
Properties properties = new Properties(); | |
properties.put("mail.smtp.host", "****"); | |
properties.put("mail.host", "****"); | |
properties.put("mail.smtp.port", "25"); | |
properties | |
} | |
// コマンドマッピング | |
def map = new MailcapCommandMap() | |
map.addMailcap("multipart/signed;; x-java-content-handler=oracle.security.crypto.smime.SmimeDataContentHandler"); | |
CommandMap.setDefaultCommandMap(map) | |
// メール本文の生成 | |
def mailText = new MimeBodyPart() | |
mailText.setText('''メール本文 | |
あいうえお | |
かきくけこ | |
''', "iso-2022-jp") | |
// 添付ファイルの生成 | |
// 添付ファイルには、このGroovy Scriptを指定しとく | |
def attachedFile = new MimeBodyPart() | |
attachedFile.setDataHandler(new DataHandler(new FileDataSource("OracleSMIME.groovy"))) | |
attachedFile.setFileName('OracleSMIME.groovy') | |
// マルチパートオブジェクトの生成 | |
def multipart = new MimeMultipart() | |
multipart.addBodyPart(mailText) | |
multipart.addBodyPart(attachedFile) | |
// マルチパートオブジェクトを持つBodyPartの生成 | |
def mail = new MimeBodyPart() | |
mail.setContent(multipart) | |
def keyStore = new KeyStoreWrapper() | |
keyStore.initialize() | |
// OracleSMIMEを使って、電子署名生成&証明書の生成 | |
SmimeMultipartSigned sig = new SmimeMultipartSigned(mail, AlgID.sha1); | |
def capabilities = new SmimeCapabilities() | |
capabilities.addCapability(AlgID.sha512WithRSAEncryption) | |
sig.addCertificate(keyStore.getCertificate()) | |
sig.addSignature(keyStore.getPrivateKey(), keyStore.getCertificate(), capabilities) | |
// 送信するメッセージの作成 | |
Session session = Session.getDefaultInstance(createSessionProperties()) | |
session.setDebug(true) | |
MimeMessage message = new MimeMessage(session) | |
message.setSubject("タイトル", 'iso-2022-jp') | |
message.setRecipients(Message.RecipientType.TO, "***@***.com") | |
message.setFrom(new InternetAddress("****@****.com")) | |
message.setContent(sig, sig.generateContentType()) | |
message.saveChanges() | |
Transport.send(message) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment