代码之家  ›  专栏  ›  技术社区  ›  user1228123

使用Java Mail哪个更安全

  •  0
  • user1228123  · 技术社区  · 7 年前

    我已经编写了使用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 ***********************/
    

    这种方法使用会话。getTransport(“smtp”)和。连接以进行身份验证:

    /************************ 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 回复  |  直到 7 年前
        1
  •  1
  •   Bill Shannon    7 年前

    它们相当安全。一个更冗长。