Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用Java邮件哪个更安全_Android_Security_Authentication_Smtp_Jakarta Mail - Fatal编程技术网

Android 使用Java邮件哪个更安全

Android 使用Java邮件哪个更安全,android,security,authentication,smtp,jakarta-mail,Android,Security,Authentication,Smtp,Jakarta Mail,我已经编写了使用JavaMail发送电子邮件的代码,我很想了解下面两个选项中哪一个更安全、更实用。这两组代码目前都可以使用Gmail、Outlook或Yahoo通过SMTP发送。代码来自Android,但更通用的是Java String password = "**********"; String username = "????@gmail.com";//"????@outlook.com";//"????@yahoo.com"; String smtp_host_setting = "sm

我已经编写了使用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():

/*******************选项1*****************************/
私人int发送\电子邮件\临时(){
Properties props=新属性();
props.put(“mail.smtp.auth”,“true”);
props.put(“mail.smtp.host”,smtp_host_设置);//YAHOO需要它是mail.smtp.host,GMAIL和OUTLOOK可以使用mail.host(和这个)
//props.put(“mail.debug”,“true”);
put(“mail.smtp.ssl.enable”,“true”);
props.put(“mail.smtp.starttls.enable”、“true”);
props.put(“mail.smtp.port”,smtp\u端口设置);
session=session.getInstance(
道具,
新的javax.mail.Authenticator(){
受保护的密码身份验证getPasswordAuthentication(){
返回新密码身份验证(用户名、密码);
}
});
ActuallySendAsync_temp asy=新的ActuallySendAsync_temp(真);
asy.execute();
返回0;
}
类实际数据同步\u临时扩展异步任务{
公共实际数据同步温度(布尔布尔布尔值){
//发送电子邮件前要做的事情
}
@凌驾
受保护的Void doInBackground(字符串…参数){
试一试{
Message Message=新的mimessage(会话);
message.setFrom(新的Internet地址(用户名));
message.setRecipients(message.RecipientType.TO,
InternetAddress.parse(收件人\电子邮件\地址));
message.setSubject(电子邮件主题);
message.setText(email_msg);
传输。发送(消息);
}捕获(消息异常e){
e、 printStackTrace();
}最后{
}
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
//发送电子邮件后要做的事情
}
}
/************************备选案文1-结束***********************/
此方法使用session.getTransport(“smtp”)和.connect进行身份验证:

/************************ 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 ***********************/
/************************ 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 ***********************/
/*******************选项2*****************************/
私人int发送\电子邮件\临时(){
Properties props=新属性();
props.put(“mail.smtp.auth”,“true”);
props.put(“mail.smtp.host”,smtp_host_设置);//YAHOO需要它是mail.smtp.host,GMAIL和OUTLOOK可以使用mail.host(和这个)
//props.put(“mail.debug”,“true”);
put(“mail.smtp.ssl.enable”,“true”);
props.put(“mail.smtp.starttls.enable”、“true”);
props.put(“mail.smtp.port”,smtp\u端口设置);
session=session.getInstance(道具);
ActuallySendAsync_temp asy=新的ActuallySendAsync_temp(真);
asy.execute();
返回0;
}
类实际数据同步\u临时扩展异步任务{
公共实际数据同步温度(布尔布尔布尔值){
//发送电子邮件前要做的事情
}
@凌驾
受保护的Void doInBackground(字符串…参数){
试一试{
Message Message=新的mimessage(会话);
message.setFrom(新的Internet地址(用户名));
message.setRecipients(message.RecipientType.TO,
InternetAddress.parse(收件人\电子邮件\地址));
message.setSubject(电子邮件主题);
message.setText(email_msg);
传输=session.getTransport(“smtp”);
传输连接(smtp主机设置、用户名、密码);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
}捕获(消息异常e){
e、 printStackTrace();
}最后{
}
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
//发送电子邮件后要做的事情
}
}
/************************备选案文2-结束***********************/
这两种方法是等效的,还是一种方法比另一种方法更安全


谢谢你的帮助。

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

非常感谢比尔的回答。你能推荐一种更安全的方式使用JavaMail吗?一种更安全的方式做什么,确切地说?在应用程序中管理密码总是很棘手的,不管它们是用来向邮件服务器或其他什么东西进行身份验证。您可能希望深入研究,这避免了一些问题,但要复杂得多。我同意在应用程序中管理密码的挑战,但我很高兴我能处理这方面的问题。我想检查我上面使用的方法是否是通过密码直接使用JavaMail身份验证的最安全的方法。你对我在这方面采取的方法满意吗?例如,有一种方法让垃圾收集器从RAM中删除密码,这可能是一种草率的方法吗?JavaMail将密码作为字符串而不是字符数组进行管理,因此您将遇到GC问题。对不起,没有其他选择了。