【问题标题】:Gmail smtp, Blackberry, BB, socketConnection, send email via gmail smtp programmaticallyGmail smtp、Blackberry、BB、socketConnection,以编程方式通过 gmail smtp 发送电子邮件
【发布时间】:2011-01-31 18:15:52
【问题描述】:

有人可以帮助我如何通过 socketConnection 以编程方式使用 gmail smtp 服务器。我的问题是如何编写 TSL/SSL 身份验证,因为我无法与服务器通信?有人在黑莓上用 java 做的吗?

谢谢

亚历克斯

【问题讨论】:

    标签: java ssl blackberry smtp gmail


    【解决方案1】:

    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);
        }
    }
    

    【讨论】:

    • 这在 BlackBerry 上不起作用 - 这些 javax.mail.* 类在 J2ME 中不可用。
    • 好的,那么抱歉。那你想让我删除我的答案吗?
    • Martc Novakowski 说得对,我尝试了所有我想到的东西......我在网上看到了一个解决方案,但我不确定它会起作用,而且有点贵..跨度>
    【解决方案2】:

    黑莓的开源电子邮件客户端怎么样。使用gmail的smtp服务器没有问题,处理TSL/SSL认证也没有问题。

    它恰好是 RIM 尚未发现的最流行的可用于黑莓的开源电子邮件客户端。

    这是一个页面,您可以从中下载并试用,或获取所有源代码: http://www.logicprobe.org/proj/logicmail

    【讨论】:

    • 嗨 Vernom,感谢您的回复,但我必须通过 smpt 在我的应用程序中以编程方式发送电子邮件(该应用程序不是电子邮件客户端)
    • 这就是开源的全部意义所在。 LogicMail 做了你想做的所有事情,你可以查看它的源代码。您基本上有两个问题需要解决:打开 SSL 网络连接和使用 SMTP 协议。
    • 许可证呢?我可以在非开源项目中使用它吗?我阅读了有关 BSD 许可证的信息,但我不清楚我是否可以在闭源项目中使用或不使用
    猜你喜欢
    • 1970-01-01
    • 2017-05-12
    • 2020-02-21
    • 1970-01-01
    • 2015-10-29
    • 2014-01-04
    • 2016-01-16
    • 2015-10-09
    • 2011-08-11
    相关资源
    最近更新 更多