使用Java Mail更安全

时间:2018-04-14 10:05:26

标签: android security authentication smtp javamail

我使用JavaMail发送电子邮件的代码,我很想知道下面两个选项中的哪一个更安全,更好的做法。这两组代码目前都可以使用Gmail,Outlook或Yahoo通过SMTP发送。代码来自Android,但更通用的是Java。

String password = "**********";
String username = "????@gmail.com";//"????@outlook.com";//"????@yahoo.com";
String smtp_host_setting = "smtp.gmail.com";//"smtp-mail.outlook.com";//"smtp.mail.yahoo.com";
String smtp_port_setting = "587";//"587";//"587";
String recipient_email_address = "recipient@recipient_server.com";
String email_subject = "Email Subject";
String email_msg = "Some text for the message\r\nThanks!";
Session session = null;

此方法使用getPasswordAuthentication():

/************************ OPTION 1 *****************************/
private int send_email_temp(){
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", smtp_host_setting); // YAHOO needs it to be mail.smtp.host, GMAIL and OUTLOOK were OK with mail.host (and with this)
    //props.put("mail.debug", "true");
    props.put("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.port", smtp_port_setting);

    session = Session.getInstance(
            props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    ActuallySendAsync_temp asy = new ActuallySendAsync_temp(true);
    asy.execute();

    return 0;
}


class ActuallySendAsync_temp extends AsyncTask<String, String, Void> {

    public ActuallySendAsync_temp(boolean boo) {
        // something to do before sending email
    }

    @Override
    protected Void doInBackground(String... params) {
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(recipient_email_address));
            message.setSubject(email_subject);
            message.setText(email_msg);

            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        } finally {

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        // something to do after sending email
    }
}
/************************ OPTION 1 - End ***********************/

此方法使用session.getTransport(&#34; smtp&#34;)和.connect进行身份验证:

/************************ OPTION 2 *****************************/
private int send_email_temp(){
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", smtp_host_setting); // YAHOO needs it to be mail.smtp.host, GMAIL and OUTLOOK were OK with mail.host (and with this)
    //props.put("mail.debug", "true");
    props.put("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.port", smtp_port_setting);

    session = Session.getInstance(props);

 ActuallySendAsync_temp asy = new ActuallySendAsync_temp(true);
    asy.execute();

    return 0;
}


class ActuallySendAsync_temp extends AsyncTask<String, String, Void> {

    public ActuallySendAsync_temp(boolean boo) {
        // something to do before sending email
    }

    @Override
    protected Void doInBackground(String... params) {
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(recipient_email_address));
            message.setSubject(email_subject);
            message.setText(email_msg);

            Transport transport = session.getTransport("smtp");
            transport.connect(smtp_host_setting, username, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (MessagingException e) {
            e.printStackTrace();
        } finally {

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        // something to do after sending email
    }
}
/************************ OPTION 2 - End ***********************/

这两种方法是否相同,或者一种选择被认为比另一种更安全?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

他们相当安全。一个是更加冗长。