ssljava代码,ssl java
java如何使用ssl连接qq邮箱
Gmail目前已经启用了POP3和SMTP服务,与其他邮箱不同的是Gmail提供的POP3和SMTP是使用安全套接字层SSL的,因此常规的JavaMail程序是无法收发邮件的,下面是使用JavaMail如何收取Gmail邮件以及发送邮件的代码:
公司主营业务:成都做网站、网站建设、外贸营销网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出莲花免费做网站回馈大家。
1. [代码]GmailFetch.java 跳至 [1] [2] [全屏预览]
01 package lius.javamail.ssl;
02
03 import java.io.UnsupportedEncodingException;
04 import java.security.*;
05 import java.util.Properties;
06 import javax.mail.*;
07 import javax.mail.internet.InternetAddress;
08 import javax.mail.internet.MimeUtility;
09
10 /**
11 * 用于收取Gmail邮件
12 * @author Winter Lau
13 */
14 public class GmailFetch {
15
16 public static void main(String argv[]) throws Exception {
17
18 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
19 final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
20
21 // Get a Properties object
22 Properties props = System.getProperties();
23 props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
24 props.setProperty("mail.pop3.socketFactory.fallback", "false");
25 props.setProperty("mail.pop3.port", "995");
26 props.setProperty("mail.pop3.socketFactory.port", "995");
27
28 //以下步骤跟一般的JavaMail操作相同
29 Session session = Session.getDefaultInstance(props,null);
30
31 //请将红色部分对应替换成你的邮箱帐号和密码
32 URLName urln = new URLName("pop3","pop.gmail.com",995,null,
33 "[邮箱帐号]", "[邮箱密码]");
34 Store store = session.getStore(urln);
35 Folder inbox = null;
36 try {
37 store.connect();
38 inbox = store.getFolder("INBOX");
39 inbox.open(Folder.READ_ONLY);
40 FetchProfile profile = new FetchProfile();
41 profile.add(FetchProfile.Item.ENVELOPE);
42 Message[] messages = inbox.getMessages();
43 inbox.fetch(messages, profile);
44 System.out.println("收件箱的邮件数:" + messages.length);
45 for (int i = 0; i messages.length; i++) {
46 //邮件发送者
47 String from = decodeText(messages[i].getFrom()[0].toString());
48 InternetAddress ia = new InternetAddress(from);
49 System.out.println("FROM:" + ia.getPersonal()+'('+ia.getAddress()+')');
50 //邮件标题
51 System.out.println("TITLE:" + messages[i].getSubject());
52 //邮件大小
53 System.out.println("SIZE:" + messages[i].getSize());
54 //邮件发送时间
55 System.out.println("DATE:" + messages[i].getSentDate());
56 }
57 } finally {
58 try {
59 inbox.close(false);
60 } catch (Exception e) {}
61 try {
62 store.close();
63 } catch (Exception e) {}
64 }
65 }
66
67 protected static String decodeText(String text)
68 throws UnsupportedEncodingException {
69 if (text == null)
70 return null;
71 if (text.startsWith("=?GB") || text.startsWith("=?gb"))
72 text = MimeUtility.decodeText(text);
73 else
74 text = new String(text.getBytes("ISO8859_1"));
75 return text;
76 }
77
78 }
2. [代码]GmailSender.java
01 package lius.javamail.ssl;
02
03 import java.security.Security;
04 import java.util.Date;
05 import java.util.Properties;
06
07 import javax.mail.Authenticator;
08 import javax.mail.Message;
09 import javax.mail.MessagingException;
10 import javax.mail.PasswordAuthentication;
11 import javax.mail.Session;
12 import javax.mail.Transport;
13 import javax.mail.internet.AddressException;
14 import javax.mail.internet.InternetAddress;
15 import javax.mail.internet.MimeMessage;
16
17 /**
18 * 使用Gmail发送邮件
19 * @author Winter Lau
20 */
21 public class GmailSender {
22
23 public static void main(String[] args) throws AddressException, MessagingException {
24 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
25 final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
26 // Get a Properties object
27 Properties props = System.getProperties();
28 props.setProperty("mail.smtp.host", "smtp.gmail.com");
29 props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
30 props.setProperty("mail.smtp.socketFactory.fallback", "false");
31 props.setProperty("mail.smtp.port", "465");
32 props.setProperty("mail.smtp.socketFactory.port", "465");
33 props.put("mail.smtp.auth", "true");
34 final String username = "[邮箱帐号]";
35 final String password = "[邮箱密码]";
36 Session session = Session.getDefaultInstance(props, new Authenticator(){
37 protected PasswordAuthentication getPasswordAuthentication() {
38 return new PasswordAuthentication(username, password);
39 }});
40
41 // -- Create a new message --
42 Message msg = new MimeMessage(session);
43
44 // -- Set the FROM and TO fields --
45 msg.setFrom(new InternetAddress(username + "@mo168.com"));
46 msg.setRecipients(Message.RecipientType.TO,
47 InternetAddress.parse("[收件人地址]",false));
48 msg.setSubject("Hello");
49 msg.setText("How are you");
50 msg.setSentDate(new Date());
51 Transport.send(msg);
52
53 System.out.println("Message sent.");
54 }
55 }
如何用Java代码来把SSL的证书自动导入到Jav
下面这个Java类可以帮助我们做这个事情。同时我们还可以把这个帮助方法开发一个可视化的程序,这样就更加方便:
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.security.KeyStore;import java.security.cert.CertificateFactory;import java.security.cert.X509Certificate;import java.util.List;import javax.naming.ldap.LdapName;import javax.naming.ldap.Rdn;import javax.security.auth.x500.X500Principal;public class KeyStoreHelper { public static void createTrustJKSKeyStore(final String originalTrustFolder, final String jksTrustStoreLocation, final String password) { File keyStoreFile = new File(jksTrustStoreLocation); if (!keyStoreFile.exists()) { try { KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); keystore.load(null, password.toCharArray()); File trustedFolder = new File(originalTrustFolder); File[] certs = trustedFolder.listFiles(); if (certs != null) { for (File cert : certs) { CertificateFactory factory = CertificateFactory.getInstance("X.509"); try { X509Certificate certificate = (X509Certificate) factory.generateCertificate(new FileInputStream(cert)); X500Principal principal = certificate.getSubjectX500Principal(); LdapName ldapDN = new LdapName(principal.getName()); ListRdn rdns = ldapDN.getRdns(); for (Rdn rdn : rdns) { String type = rdn.getType(); if (type.equals("CN")) { keystore.setCertificateEntry((String) rdn.getValue(),certificate);break; } } } catch (Exception ex) { continue; } } } FileOutputStream fos = new FileOutputStream(jksTrustStoreLocation); keystore.store(fos, password.toCharArray()); fos.close(); } catch (Exception exp) { } } } /** * @param args */ public static void main(String[] args) { KeyStoreHelper.createTrustJKSKeyStore("D:\\cacerts", "D:\\cacerts\\test.jks", "test123"); }}
JAVA怎样调用https类型的webservice
1.打开webService链接,右键属性—》证书—》详细信息—》复制到文件,保存cer格式的文件。
2. 复制下面的cmd命令,执行keytool命令,生成keystore文件,例如
c:\nciic.keystore
keytool -import -alias nciic -file c:\jswszx.cer -keystore c:\nciic.keystore
它会提示输入密码,随便输入,例如:123456,回车
4.他会提示是否信任这个认证,输入Y,回车,指定目录下就会生成nciic.keystore文件
它会提示输入密码,随便输入,例如:123456,回车
4.他会提示是否信任这个认证,输入Y,回车,指定目录下就会生成nciic.keystore文件
5.修改Java代码
在调用接口方法之前,添加如下代码:
System.setProperty("javax.NET.ssl.trustStore","c://nciic.keystore"); System.setProperty("java.protocol.handler.pkgs","com.sun.Net.ssl.internal.");
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
JAVA和C++的SSL通讯程序 问题1:C收到JAVA发来的程序,打印出来只有一部分。 问题2:C返回给JAVA的数据,
1、循环读取描述符,直到读取不到数据才算是读取完成;
2、正常情况下write只是把数据写到内核的缓冲区中,由内核调度写入到物理设备
可以使用O_SYNC同步I/O,这样write只有在数据被实际写入之后才会返回;
可以使用tcpdump查看是否已经把数据发送成功;
内核缓冲区的作用是提高网络发送效率,只有等缓冲区数据满了才会一起发送,查看是否是因为这个导致c程序只有在结束程序后,内核才把数据发送出去
Java 代码操作带SSL的FTP服务器
参考
client = new FTPSClient(implictSSL);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
kmf.init(KeyStore.getInstance("BKS"), "wshr.ut".toCharArray());
client.setTrustManager(new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() { return null; }
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { }
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { }
});
client.setKeyManager(kmf.getKeyManagers()[0]);
client.setNeedClientAuth(false);
client.setUseClientMode(false);
如何在Java SE使用SSL设定相互验证
这个要考虑并发,即使两个用户同时请求,请求的也是你底层的一段代码,在这个代码上面加锁,一个访问结束之前另一个不能访问。
Java实现ssl,需要将ssl证书导入jvm,才能正常工作,否则不行。
导入命令参考如下
keytool -import -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -storepass changeit -keypass 123456 -alias 45alias -file D:/test.cer
文章标题:ssljava代码,ssl java
文章地址:http://myzitong.com/article/hssddg.html