【发布时间】:2011-01-31 18:15:52
【问题描述】:
有人可以帮助我如何通过 socketConnection 以编程方式使用 gmail smtp 服务器。我的问题是如何编写 TSL/SSL 身份验证,因为我无法与服务器通信?有人在黑莓上用 java 做的吗?
谢谢
亚历克斯
【问题讨论】:
标签: java ssl blackberry smtp gmail
有人可以帮助我如何通过 socketConnection 以编程方式使用 gmail smtp 服务器。我的问题是如何编写 TSL/SSL 身份验证,因为我无法与服务器通信?有人在黑莓上用 java 做的吗?
谢谢
亚历克斯
【问题讨论】:
标签: java ssl blackberry smtp gmail
Java 中有一个用于发送邮件的库,名为JavaMail。我不知道它是否可以与黑莓一起使用,但如果是后者,就使用这个类:
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class MailUtils {
private MailUtils() {
}
public static void sendSSLMessage(String recipients[], String subject,
String message, String from) throws MessagingException {
boolean debug = true;
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your@email.at.gmail", "your password");
}
}
);
session.setDebug(debug);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
}
【讨论】:
黑莓的开源电子邮件客户端怎么样。使用gmail的smtp服务器没有问题,处理TSL/SSL认证也没有问题。
它恰好是 RIM 尚未发现的最流行的可用于黑莓的开源电子邮件客户端。
这是一个页面,您可以从中下载并试用,或获取所有源代码: http://www.logicprobe.org/proj/logicmail
【讨论】: