当我将Bouncy Castle添加为安全提供程序时,电子邮件应用程序不起作用

时间:2013-03-25 22:38:07

标签: java cryptography bouncycastle

我目前正在与Bouncy Castle合作,我已经意识到每当我添加安全提供程序时,都不会发送电子邮件。我不知道为什么添加Bouncy Castle会阻止发送电子邮件。

import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SignatureException;

import java.security.cert.X509Certificate;
import java.util.Date;

import org.bouncycastle.asn1.x509.X509Name;
import org.bouncycastle.x509.X509V1CertificateGenerator;

@SuppressWarnings("deprecation")
public class RootCertificateAuthority {
    public KeyPair pair;
    String issuer = "CN=TRN";
    public X509Certificate rootcert;

    public  RootCertificateAuthority(){
        generateRSAKeyPair();
        generateV1Certificate();
    }

    public KeyPair generateRSAKeyPair() {
        KeyPairGenerator kpGen;
        try {
            kpGen = KeyPairGenerator.getInstance("RSA");
            kpGen.initialize(1024);
            return kpGen.generateKeyPair();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @SuppressWarnings("deprecation")
    public void generateV1Certificate()  {
        // Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        this.pair = generateRSAKeyPair();
        // Security.provider.7=org.bouncycastle.jce.provider.BouncyCastleProvider;
        X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();

        certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
        certGen.setIssuerDN(new X509Name( issuer));
        certGen.setNotBefore(new Date(System.currentTimeMillis()));
        certGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 100)));
        certGen.setSubjectDN(new X509Name( issuer));
        certGen.setPublicKey(pair.getPublic());
        certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");

        try {
            rootcert= certGen.generateX509Certificate(pair.getPrivate(),"BC");
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SignatureException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    };
}

//This class is used to send mail
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class PleaseEmail {
    RootCertificateAuthority CertificateAuthority;
    List<String> userslist;
    KeyStore keystore;

    public PleaseEmail(){
        userslist = new ArrayList<String>();
        CertificateAuthority = new RootCertificateAuthority();
        try {
            keystore= KeyStore.getInstance(KeyStore.getDefaultType());
            java.io.FileInputStream fis = null;
            fis = new java.io.FileInputStream("U:/mykeystore.jks");
            keystore.load(fis, "password".toCharArray());
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (CertificateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (KeyStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

    public void sendMail(){
        Properties props = new Properties( );
        props.put("mail.host","smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        Session mailConnection = 
            Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("XXX@GMAIL.COM","password");
            }
        });

        try {
            MimeMessage message = new MimeMessage(mailConnection);
            message.setFrom(new InternetAddress("opd@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("xxx@gmail.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler," +
                    "\n\n No spam to my email, please!");
            Transport.send(message);
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Security.addProvider(
            new org.bouncycastle.jce.provider.BouncyCastleProvider());
        PleaseEmail a = new PleaseEmail();
        a.sendMail();
    }
}

0 个答案:

没有答案