0

SMTP を使用してメールを送信しています。メールの送信はうまくいきます。ただし、アカウントからログアウトできるように、ボタンのクリックでログアウトを使用したいと考えています。アカウントからログアウトするにはどうすればよいですか? ログアウトする方法はありますか?

ここに私が使用するいくつかのコードがあります:

   public void Mail(String user, String pass) {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(FROM_MAILID,PASSWORD);
        }
        });
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(FROM_MAILID));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(TO_MAILID));
        message.setSubject("Testing Subject");     
        Multipart multipart = new MimeMultipart();     
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/wallpaper.jpg"));
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName("image.png");
        messageBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
        messageBodyPart.setHeader("Content-ID","<vogue>");
        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart);
        Transport.send(message);

        Log.d("sent","mail sent...");
    }catch(AuthenticationFailedException afe){
        Log.d("wrong","wrong passwrd....");
    }catch(AddressException ae){


    }catch (MessagingException e) {
        throw new RuntimeException(e);
    }

}
4

2 に答える 2

0

最後の catch ステートメントの後に session.close(): で finally ブロックを追加します。

} finally {
    session.close();
}
于 2013-03-24T19:17:38.333 に答える
0

まず、 JavaMailでよくある間違いをたくさん犯しました。それらをクリーンアップします。

メッセージの送信にかかる時間だけ「ログイン」しているだけなので、「ログアウト」する必要はありません。Transport.send が完了すると、接続が閉じられ、ログアウトされます。

于 2013-03-25T03:48:46.133 に答える