Вы находитесь на странице: 1из 2

1. Import the given .

cer file into your server with Keytool command

keytool -import -noprompt -trustcacerts -alias relay.nic.in -file PATH.cer


-keystore "jssecacert" file path(file available in where java installed )

Enter keystore password:


Re-enter new password:
Certificate was added to keystore

2. List certificate file

keytool -list -keystore ..\lib\security\jsscacert -alias relay.nic.in

3. Now test with the given JAVA code:

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailTest {

private static final String SMTP_HOST_NAME = "relay.nic.in";


private static final String SMTP_AUTH_USER = "user-address@nic.in"; //
user's email address
private static final String SMTP_AUTH_PWD = "password; //
Password

public static void main(String[] args) throws Exception {

new EmailTest().test("Recipients@nic.in"); // Recipient email address

public void test(String email) throws Exception {


Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.debug", "true");

Authenticator auth = new SMTPAuthenticator();


Session mailSession = Session.getDefaultInstance(props, auth);
Transport transport = mailSession.getTransport();
String msg = "Test Mail";
MimeMessage message = new MimeMessage(mailSession);
message.setContent(msg, "text/html; charset=utf-8");
message.setSubject("Test Mail");
message.setFrom(new InternetAddress("from@nic.in")); // from address
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(email));

transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}

private class SMTPAuthenticator extends javax.mail.Authenticator {

public PasswordAuthentication getPasswordAuthentication() {


String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}

Вам также может понравиться