java - Is it possible to send a secured mail with out the credentials of the sender? -
i trying send digital signed/secrured mail using javamail api.
for created keystore certificate , used (loaded through bouncycastle) while generating mail , sending user. have provide user name , password of sender mail id authenticate.
is there way send secured mail out credentials of sender?
i tried making authentication false. no luck.
props.put("mail.smtp.auth", "true"); //enable authentication
my code :::
public static void main(string[] args) { final string fromemail = "satishkumar.reddy@xyz.com"; //requires valid gmail id final string toemail = "satishxxxxxreddy@gmail.com"; 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 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 , password access keystore.load(new fileinputstream("d:\\certificates\\mail_cert\\selfservice.cert"), "keystore".tochararray()); // find first legit alias in keystore , use enumeration<string> es = keystore.aliases(); string alias = ""; while (es.hasmoreelements()) { alias = (string) es.nextelement(); // alias refer private key? assign true/false 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 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 list of certs generator list certlist = new arraylist(); certlist.add(chain[0]); certstore certs = certstore.getinstance("collection", new collectioncertstoreparameters(certlist), "bc"); signer.addcertificatesandcrls(certs); // construct 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 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 content of signed message signedmessage.setcontent(mm); signedmessage.savechanges(); // send 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(); } }
the creation of signed or encrypted email message independent of ability login mail server , send email message. no public email servers going let send email message without logging in first. if have user's private key create signed email message, you're going need user's password login mail server.
Comments
Post a Comment