Today we are going to demonstrate how to send encrypted emails using java.
Data encryption is necessary as in the real life is full of security threats from hackers, The data must be protected by using some form of encryption, but transferring the data from one place to another place also needs encryption so the people or the users on the same line can’t read your data.
Today in this article I am going to demonstrate you how to send encrypted emails using the java code.
How to send encrypted email in Java using the Chilkat email component.
Java Code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import com.chilkatsoft.CkMailMan; import com.chilkatsoft.CkEmail; public class EncryptedEmail { static { try { System.loadLibrary("chilkat"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load.\n" + e); System.exit(1); } } // Sending an encrypted email using the recipient's digital certificate. public static void main(String argv[]) { CkMailMan mailman = new CkMailMan(); mailman.UnlockComponent("coding security"); // Set your SMTP server's hostname mailman.put_SmtpHost("smtp.comcast.net"); // If your SMTP server requires a login, set username/password //mailman.put_SmtpUsername("myUsername"); //mailman.put_SmtpPassword("myPassword"); // Create a simple email CkEmail email = new CkEmail(); email.put_Subject("Sending mail from Java"); email.put_Body("This email was sent from a Java program"); email.put_From("Codingsec Support <support@codingsec.com>"); // Add a single recipient email.AddTo("Admin","admin@codingsec.net"); // There are a number of ways to send digitally encrypted email using Chilkat. // This example demonstrates the easiest (other examples will explore // some of the other possibilities). // To send an encrypted email, you will first need the digital certificate // for the recipient. The email address in the certificate should match // the recipient's email address. You do not need the private key to send // encrypted email: only the public-key is required. The recipient however, // must have his/her certificate installed with private key in order to decrypt. // // If you have the digital certificate in a .cer file, double-click on it from // Windows Explorer and install the certificate in the default location. // Chilkat should now be able to locate it. You only need to set the SendEncrypted // property = true to send encrypted email. // // That's it. When SendEmail is called, it will locate the certificate matching // the recipient's email address, encrypt the email (including all attachments) and // send it. email.put_SendEncrypted(true); // Note: When sending encrypted mail, only one recipient is allowed. If you wish // to send the same email to multiple recipients, create a program loop to call // SendEmail for each recipient. boolean success = mailman.SendEmail(email); if (!success) { mailman.SaveLastError("lastError.txt"); } } } |
The Code Explained
- Download chilkatsoft api from the link once you have downloaded copy the class files in to the mypackage folder.
- We have to import the chilkat library hence.
1 2 3 4 |
import com.chilkatsoft.CkMailMan; import com.chilkatsoft.CkEmail; |
Now I have writted an class Encrypted Email which is used to load their library if the load fails we have generate the exception that the import has failed hence I have used a catch block. I have used the static block just to make the block accessible globally
1 2 3 4 5 6 7 8 9 10 |
static { try { System.loadLibrary("chilkat"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load.\n" + e); System.exit(1); } } |
Now we have to create an object for the ChilKat class in order to use it’s methods
1 2 3 |
CkMailMan mailman = new CkMailMan(); |
An SMTP(Send Message Transfer Protocol) is required to send a message using the mail
1 2 3 |
mailman.put_SmtpHost("smtp.comcast.net"); |
I forgot you must have a working mail server in order to send mails. If they ask for username and password provide in these fields. I have commented them in the code assuming that you won’t require them.I have just added a single recipient you can use multiple recipient using a loop
1 2 3 4 |
mailman.put_SmtpUsername("myUsername"); mailman.put_SmtpPassword("myPassword"); |
Let’s create a simple email now using the methods and objects
1 2 3 4 5 6 7 8 |
CkEmail email = new CkEmail(); email.put_Subject("Sending mail from Java"); email.put_Body("This email was sent from a Java program"); email.put_From("Codingsec Support <support@codingsec.com>"); // Add a single recipient email.AddTo("Admin","admin@codingsec.net"); |
We have came to the main part of the article you just need to add this part in order to encrypt the email
1 2 3 |
email.put_SendEncrypted(true); |
I have finally created a boolean method to test weather the email is delivered or not
1 2 3 4 5 6 7 |
boolean success = mailman.SendEmail(email); if (!success) { mailman.SaveLastError("lastError.txt"); } |
That’s pretty much wraps it up
The above code can be turned into function to automate the mail in your server by making the main method as the interface in your java.
Take your time to comment on this article and one more previous article about the Doc to PDF conversion was just help the programmers how they can approach with file conversion and how to handle the docx files.
Yes, you can send mail from gmail, yahoo or outlook but for the programmers in order to automate their mail server they kinda need this code.