2014-04-01 4 views
0

Oauth2를 사용하여 연결하는 사용자 계정을 통해 전자 메일을 보내려고합니다. 이 잘 작동하고 나에게 토큰을 제공oauth2를 사용하여 사용자 전자 메일에 연결

String SCOPES = "https://mail.google.com/"; 
token = GoogleAuthUtil.getToken(m_context,SystemUtills.getUsername(m_context)+"@gmail.com","oauth2:" + SCOPES); 

:

이 제품은 액세스 토큰을 얻기를위한 내 코드입니다.

지금 나는이 같은 IMAP 및 SMTP 연결을 시도하고있다 : 내 호출 클래스에서

public class OAuth2Authenticator extends AsyncTask<String, Void, String> { 
private static final Logger logger =Logger.getLogger(OAuth2Authenticator.class.getName()); 
private static String user_email; 
public static final class OAuth2Provider extends Provider { 
private static final long serialVersionUID = 1L; 

public OAuth2Provider() { 
    super("Google OAuth2 Provider", 1.0, 
     "Provides the XOAUTH2 SASL Mechanism"); 
    put("SaslClientFactory.XOAUTH2", 
     "com.google.code.samples.oauth2.OAuth2SaslClientFactory"); 

} 
} 

public static void initialize() { 
Security.addProvider(new OAuth2Provider()); 
} 

public static IMAPStore connectToImap(String host, 
            int port, 
            String userEmail, 
            String oauthToken, 
            boolean debug) throws Exception { 
Properties props = new Properties(); 
props.put("mail.imaps.sasl.enable", "true"); 
props.put("mail.imaps.sasl.mechanisms", "XOAUTH2"); 
props.put("mail.imaps.sasl.mechanisms.oauth2.oauthToken", oauthToken); 
Session session = Session.getInstance(props); 
session.setDebug(debug); 

final URLName unusedUrlName = null; 
IMAPSSLStore store = new IMAPSSLStore(session, unusedUrlName); 
final String emptyPassword = ""; 
store.connect(host, port, userEmail, emptyPassword); 
return store; 
} 


public static SMTPTransport connectToSmtp(String host, 
             int port, 
             String userEmail, 
             String oauthToken, 
             boolean debug) throws Exception { 
Properties props = new Properties(); 
props.put("mail.smtp.starttls.enable", "true"); 
props.put("mail.smtp.starttls.required", "true"); 
props.put("mail.smtp.sasl.enable", "true"); 
props.put("mail.smtp.sasl.mechanisms", "XOAUTH2"); 
props.put("mail.imaps.sasl.mechanisms.oauth2.oauthToken", oauthToken); 
Session session = Session.getInstance(props); 
session.setDebug(debug); 

final URLName unusedUrlName = null; 
SMTPTransport transport = new SMTPTransport(session, unusedUrlName); 
// If the password is non-null, SMTP tries to do AUTH LOGIN. 
final String emptyPassword = ""; 
transport.connect(host, port, userEmail, emptyPassword); 

return transport; 
} 

@Override 
protected String doInBackground(String... arg0) 
{ 
    try 
    { 
     IMAPStore imapStore = connectToImap("imap.gmail.com", 993, AppSettings.user_email,AppSettings.getAccessToken(), true); 
     SMTPTransport smtpTransport = connectToSmtp("smtp.gmail.com",587,AppSettings.user_email,AppSettings.getAccessToken(),true); 
     return "SUC"; 
    }catch (Exception e) { 
     e.printStackTrace(); 
     return "FLD"; 
    } 
} 

내가 사용하여 연결을 호출하고 다음 SMTP의

new AsyncGetAccessToken().execute(); 

    OAuth2Authenticator.initialize(); 
    try{ 

     new OAuth2Authenticator().execute(); 
    }catch (Exception e) { 
     e.printStackTrace(); 
    } 

을 (디버그 협상 :

04-01 07:57:08.770: I/System.out(31190): DEBUG: setDebug: JavaMail version 1.4.1 
04-01 07:57:37.890: I/System.out(31190): DEBUG SMTP: useEhlo true, useAuth false 
04-01 07:57:37.900: I/System.out(31190): DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false 
04-01 07:57:38.150: I/System.out(31190): 220 mx.google.com ESMTP q41sm37770244eez.7 - gsmtp 
04-01 07:57:38.150: I/System.out(31190): DEBUG SMTP: connected to host "smtp.gmail.com", port: 587 
04-01 07:57:38.155: I/System.out(31190): EHLO localhost 
04-01 07:57:38.310: I/System.out(31190): 250-mx.google.com at your service, [46.120.129.170] 
04-01 07:57:38.310: I/System.out(31190): 250-SIZE 35882577 
04-01 07:57:38.320: I/System.out(31190): 250-8BITMIME 
04-01 07:57:38.320: I/System.out(31190): 250-STARTTLS 
04-01 07:57:38.320: I/System.out(31190): 250-ENHANCEDSTATUSCODES 
04-01 07:57:38.320: I/System.out(31190): 250 CHUNKING 
04-01 07:57:38.330: I/System.out(31190): DEBUG SMTP: Found extension "SIZE", arg "35882577" 
04-01 07:57:38.330: I/System.out(31190): DEBUG SMTP: Found extension "8BITMIME", arg "" 
04-01 07:57:38.330: I/System.out(31190): DEBUG SMTP: Found extension "STARTTLS", arg "" 
04-01 07:57:38.335: I/System.out(31190): DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" 
04-01 07:57:38.335: I/System.out(31190): DEBUG SMTP: Found extension "CHUNKING", arg "" 
04-01 07:57:38.345: I/System.out(31190): STARTTLS 
04-01 07:57:38.440: I/System.out(31190): 220 2.0.0 Ready to start TLS 
04-01 07:57:39.115: I/System.out(31190): EHLO localhost 
04-01 07:57:39.220: I/System.out(31190): 250-mx.google.com at your service, [46.120.129.170] 
04-01 07:57:39.220: I/System.out(31190): 250-SIZE 35882577 
04-01 07:57:39.220: I/System.out(31190): 250-8BITMIME 
04-01 07:57:39.225: I/System.out(31190): 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN 
04-01 07:57:39.225: I/System.out(31190): 250-ENHANCEDSTATUSCODES 
04-01 07:57:39.225: I/System.out(31190): 250 CHUNKING 
04-01 07:57:39.235: I/System.out(31190): DEBUG SMTP: Found extension "SIZE", arg "35882577" 
04-01 07:57:39.235: I/System.out(31190): DEBUG SMTP: Found extension "8BITMIME", arg "" 
04-01 07:57:39.240: I/System.out(31190): DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN" 
04-01 07:57:39.240: I/System.out(31190): DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" 
04-01 07:57:39.245: I/System.out(31190): DEBUG SMTP: Found extension "CHUNKING", arg "" 
04-01 07:57:39.245: I/System.out(31190): DEBUG SMTP: Attempt to authenticate 
04-01 07:57:39.250: I/System.out(31190): AUTH LOGIN 
04-01 07:57:39.350: I/System.out(31190): 334 VXNlcm5hbWU6 
04-01 07:57:39.355: I/System.out(31190): bWljaGFlbC5hc2FyYWZAZ21haWwuY29t 
04-01 07:57:39.460: I/System.out(31190): 334 UGFzc3dvcmQ6 
04-01 07:57:39.470: I/System.out(31190): 
04-01 07:57:39.990: I/System.out(31190): 535-5.7.8 Username and Password not accepted. Learn more at 
04-01 07:57:39.990: I/System.out(31190): 535 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 q41sm37770244eez.7 - gsmtp 

예외 :

04-01 08:00:57.860: W/System.err(31190): javax.mail.AuthenticationFailedException 
04-01 08:00:57.880: W/System.err(31190):  at javax.mail.Service.connect(Service.java:319) 
04-01 08:00:57.885: W/System.err(31190):  at com.my_android_assistant.OAUTH.OAuth2Authenticator.connectToSmtp(OAuth2Authenticator.java:128) 

답변

1

서버가 LOGIN을 광고하기 때문에 OAuth2 공급자를 사용하지 않기 때문에 JavaMail이 먼저이를 선택합니다. 강제로 SASL을 사용하고 XOAUTH2를 선택하려면 LOGIN 및 PLAIN 메커니즘을 비활성화해야합니다. "mail.imaps.auth.login.disable"및 "mail.imaps.auth.plain.disable"을 "true"로 설정하십시오.

또한 OAuth2 support built into JavaMail 1.5.2을 사용해보세요. SNAPSHOT release을 사용할 수 있습니다.

+0

소리가 잘 들립니다 (나중에 사용해보십시오). 이전 버전의 코드에서 자바 메일을 사용하여 사용자 패스 (자격 증명을 묻는 askingg)를 사용하여 메일을 보냈습니다. 만약 내가 understant 올바른 암호를 OAUTH 토큰 insteat와 java.mail 1.5.2 사용할 수 있습니다 그냥 내 이전 코드를 사용하여 말하고 있습니까? –

관련 문제