Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Devaka Cooray
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Jeanne Boyarsky
Tim Cooke
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Tim Moores
Mikalai Zaikin
Carey Brown
Bartenders:
Forum:
Java in General
java code to digitally sign and verify an email
anandi soni
Greenhorn
Posts: 1
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
please help me if anyone knows how to send digitally signed emails.
I have tried by using opensslagian by using keytool also but.its not working.
the program I'm doing is:
package algorithmImpl; import java.io.File; import java.io.IOException; import java.security.GeneralSecurityException; import java.security.Key; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; 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; import net.suberic.crypto.EncryptionKeyManager; import net.suberic.crypto.EncryptionManager; import net.suberic.crypto.EncryptionUtils; public class digitalsigningemail { public static void main(String[] args) { digitalsigningemail sign = new digitalsigningemail(); try { sign.test(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //main public void test()throws MessagingException { Properties props = new Properties(); Session session = Session.getInstance(props); MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress("abc@gmail.com")); msg.setRecipient(Message.RecipientType.TO, new InternetAddress("def@gmail.com")); msg.setSubject("Testovaci podepsany mail"); // create and fill the first message part MimeBodyPart mbp1 = new MimeBodyPart(); mbp1.setText("Text testovaciho podepsaneho mailu"); msg.setContent("Testovaci podepsany mail", "text/plain"); // create the second message part MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message FileDataSource fds = new FileDataSource(new File("D:/workspace/Email client/rawMsg.txt")); mbp2.setDataHandler(new DataHandler(fds)); mbp2.setFileName(fds.getName()); // create the Multipart and add its parts to it Multipart mp = new MimeMultipart(); mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); // add the Multipart to the message msg.setContent(mp); // set the Date: header msg.setSentDate(new Date()); msg.saveChanges(); try{ msg = signMessage(msg, session); }catch(IOException e){e.printStackTrace();} catch(GeneralSecurityException ge){ge.printStackTrace();} Transport transport = session.getTransport("smtp"); transport.connect("smtp.server", null, null); transport.sendMessage(msg, msg.getAllRecipients()); } protected MimeMessage signMessage(MimeMessage mimeMessage, Session session)throws MessagingException,IOException,GeneralSecurityException { // Getting of the S/MIME EncryptionUtilities. EncryptionUtils encUtils = EncryptionManager.getEncryptionUtils(EncryptionManager.SMIME); // Loading of the S/MIME keystore from the file (stored as resource). char[] keystorePass = getCertificatePassword().toCharArray(); System.out.println(getCertificatePassword()); EncryptionKeyManager encKeyManager = encUtils.createKeyManager(); encKeyManager.loadPrivateKeystore(getClass().getResourceAsStream("C:/newopenssl/mtech1.p12"), keystorePass); // Getting of the S/MIME private key for signing. Key privateKey = encKeyManager.getPrivateKey(getPrivateKeyAlias(), keystorePass); // Signing the message. return encUtils.signMessage(session, mimeMessage, privateKey); } private String getPrivateKeyAlias() { return "mtech"; } private String getCertificatePassword() { return "mtech1"; } }
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Problem attaching a File
problem with java mail attachment
JAVAMAIL
File attachments using Java Mail API
Sending mail through Exchange Server
More...