如何在 Android 中使用 IMAP over SSL 连接到邮件服务器?
我需要与 MailServer
(自定义邮件服务器)建立连接
。每当我尝试连接时,它都会抛出 javax.net.SSLException,不受信任的服务器证书 异常。
我不知道如何为此创建证书。并且也不知道如何通过该证书来与邮件服务器建立安全连接。
我的代码是:
Properties props;// = new Properties();
Session session;
props=new Properties();
props.put("mail.imap.socketFactory.port", "993");
props.put("mail.imap.socketFactory.class",
"javx.net.ssl.SSLSocketFactory");
session=Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect(hostName,portNumber, emailId,password);
//the above statement throws the Exception
Folder folder = store.getFolder("INBOX");
我想知道如何为 Android 应用程序创建自签名证书。
I need to establish the Connection
with MailServer
(Custom Mail server). Whenever I tried to connect it throws the javax.net.SSLException, Not trusted server certificate
exception.
I don,t know how to create the certificate for this. And also don't know to pass that certificate to make the secure connection with mail server.
My Code is:
Properties props;// = new Properties();
Session session;
props=new Properties();
props.put("mail.imap.socketFactory.port", "993");
props.put("mail.imap.socketFactory.class",
"javx.net.ssl.SSLSocketFactory");
session=Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect(hostName,portNumber, emailId,password);
//the above statement throws the Exception
Folder folder = store.getFolder("INBOX");
I'd like to know how to create a self-signed certificate for an Android application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建自签名证书并不能解决您的问题,事实上您正在连接的服务器正在使用自签名证书,从而导致了您所看到的错误。
您要么需要购买受信任的证书并将其安装在邮件服务器上(这可能超出您的控制范围),要么需要更改 javamail 的行为以接受未经认可的机构签名的证书。
看看我对 android javamail api imap over ssl 这可能会帮助您实现第二个选项。
Creating a self-signed certificate won't solve your problem, it is the fact that the server you're connecting to is using a self-signed certificate that's causing the error that you're seeing.
You either need to purchase a trusted certificate and install it on the mail server (which may be outside of your control) or you need to change the behaviour of javamail to accept certificates which are not signed by a recognised authority.
Have a look at my answer to android javamail api imap over ssl which may help you to implement the second option.
未知证书的问题是 Java 中的一个已知问题。如果本地密钥库中没有正确的证书,您将无法直接连接到 HTTPS 服务器。
话虽这么说,我在我的一个应用程序中有一个 Apache HTTP 客户端的覆盖子句(与 Android 中使用的相同),您可以从那里开始并在其基础上构建以使其在 Android 上运行,
并且 HttpsSecurityOverride 类如下:
祝你好运!
The problem with unknown certificates is a known problem in Java. You can't just connect to an HTTPS server without having a correct certificate in your local keystore.
That being said, I have in one of my application an override clause for Apache HTTP Client (same thing being used in Android), you can maybe start from there and build on it to get it running on Android
and the HttpsSecurityOverride class is as follows:
Good luck!