2013-03-24 240 views
0

我正在使用SMTP发送邮件。发送邮件效果很好。但是,我想使用按钮单击注销,以便我可以注销我的帐户。我如何注销帐户?有没有注销的方法?使用SMTP注销GMail帐户

下面是一些代码,我用:

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

} 
+1

http://stackoverflow.com/questions/2183906/programmatically-logout-from-gmail-via-oauth – 2013-03-24 15:42:07

+0

难道你不能只删除JavaMail对象或Session对象吗? – 2013-03-24 15:43:17

+0

如何删除会话呢? – 2013-03-24 16:30:34

回答

0

添加最后用session.close()块:您的最后一个catch语句后:

} finally { 
    session.close(); 
} 
0

首先,你做了一堆的common JavaMail mistakes;清理它们。

没有必要“注销”,因为您只需“登录”就可以发送消息。当Transport.send完成时,连接关闭,您注销。

+0

我可以为登录单独编写代码吗?成功登录后我想发送邮件。我应该只为登录而写什么? – 2013-03-25 05:21:26

+0

http://stackoverflow.com/questions/15601503/login-gmail-checking-correct-user-id-and-password-and-then-sending-mail-in-andr – 2013-03-25 05:28:56