0
JavaMAIL API를 사용하여 디지털 서명/보안 메일을 보내려고합니다.보낸 사람의 자격 증명을 사용하지 않고 보안 된 메일을 보낼 수 있습니까?
이 메일에 대해 KEYSTORE 인증서를 생성하고 (bouncycastle을 통해로드)이 메일을 생성하여 사용자에게 보냈습니다. 하지만 나는 인증을 위해 보낸 사람 메일 ID의 사용자 이름과 암호를 제공해야합니다.
보낸 사람의 자격 증명없이 보안 메일을 보낼 수있는 방법이 있습니까?
인증을 false로 설정하여 시도했습니다. 운이 없다.
props.put("mail.smtp.auth", "true"); //enable authentication
내 코드 :::
public static void main(String[] args) {
final String fromEmail = "[email protected]"; //requires valid gmail id
final String toEmail = "[email protected]";
System.out.println("TLSEmail Start");
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "132.000.000.001"); //SMTP Host
props.put("mail.smtp.port", "587"); //TLS Port
props.put("mail.smtp.auth", "true"); //enable authentication
props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
props.put("mail.smtp.ssl.trust", "*");
props.put("mail.debug", "true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("MYUSERNAME","MYPASSWORD");
}
});
boolean isAlias = false;
//Session session = Session.getDefaultInstance(props);
System.out.println("SESSION CREATED..............");
try {
// Add BouncyCastle content handlers to command map
MailcapCommandMap mailcap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mailcap.addMailcap("multipart/signed;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.multipart_signed");
CommandMap.setDefaultCommandMap(mailcap);
Security.addProvider(new BouncyCastleProvider());
KeyStore keyStore = KeyStore.getInstance("JKS");
// Provide location of Java Keystore and password for access
keyStore.load(new FileInputStream("D:\\CERTIFICATES\\MAIL_CERT\\selfservice.cert"),
"keystore".toCharArray());
// Find the first legit alias in the keystore and use it
Enumeration<String> es = keyStore.aliases();
String alias = "";
while (es.hasMoreElements()) {
alias = (String) es.nextElement();
// Does alias refer to a private key? Assign true/false to isAlias & evaluate
if (isAlias = keyStore.isKeyEntry(alias)) {
break;
}
}
if (isAlias) {
KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, new KeyStore.PasswordProtection("keystore".toCharArray()));
PrivateKey myPrivateKey = pkEntry.getPrivateKey();
// Load certificate chain
Certificate[] chain = keyStore.getCertificateChain(alias);
// Create the SMIMESignedGenerator
SMIMECapabilityVector capabilities = new SMIMECapabilityVector();
capabilities.addCapability(SMIMECapability.dES_EDE3_CBC);
capabilities.addCapability(SMIMECapability.rC2_CBC, 128);
capabilities.addCapability(SMIMECapability.dES_CBC);
capabilities.addCapability(SMIMECapability.aES256_CBC);
ASN1EncodableVector attributes = new ASN1EncodableVector();
attributes.add(new SMIMEEncryptionKeyPreferenceAttribute(
new IssuerAndSerialNumber(
new X500Name(((X509Certificate) chain[0])
.getIssuerDN().getName()),
((X509Certificate) chain[0]).getSerialNumber())));
attributes.add(new SMIMECapabilitiesAttribute(capabilities));
SMIMESignedGenerator signer = new SMIMESignedGenerator();
signer.addSigner(
myPrivateKey,
(X509Certificate) chain[0],
"DSA".equals(myPrivateKey.getAlgorithm()) ? SMIMESignedGenerator.DIGEST_SHA1
: SMIMESignedGenerator.DIGEST_MD5,
new AttributeTable(attributes), null);
// Add the list of certs to the generator
List certList = new ArrayList();
certList.add(chain[0]);
CertStore certs = CertStore.getInstance("Collection",
new CollectionCertStoreParameters(certList), "BC");
signer.addCertificatesAndCRLs(certs);
// Construct the message body
MimeMessage body = new MimeMessage(session);
body.setFrom(new InternetAddress(fromEmail));
body.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
body.setContent("DEAR.....BODY....", "text/plain");
body.saveChanges();
// Sign the message
MimeMultipart mm = signer.generate(body, "BC");
MimeMessage signedMessage = new MimeMessage(session);
signedMessage.setFrom(new InternetAddress(fromEmail));
signedMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
signedMessage.setSubject("Testing Signed Subject");
// Set the content of the signed message
signedMessage.setContent(mm);
signedMessage.saveChanges();
// Send the message
Transport.send(signedMessage);
}
} catch (MessagingException e) {
throw new RuntimeException(e);
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | UnrecoverableEntryException |
InvalidAlgorithmParameterException | NoSuchProviderException | CertStoreException |SMIMEException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
어떤 오류가 있습니까? 전자 메일을 보내는 것은 분명히 사용자가 아닌 다른 자격 증명 (SMTP 인증 용)에 의존하지 않으며 암호화에는 수신자 공개 키 (hmm ** 공개 **)가 필요합니다. – Jan