1

我想發送一個意圖由用戶選擇的電子郵件應用程序處理。意圖有一個簽名作爲附件。我讀過Gmail應用程序無法處理自定義MIME類型,因此我點擊K-9 Mail來處理意圖。Android的意圖與p7s數字簽名來簽署電子郵件

Intent emailIntent = new Intent(Intent.ACTION_SEND); 
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email test"); 
emailIntent.putExtra(Intent.EXTRA_TEXT, confirmEmailBody); 
emailIntent.putExtra(Intent.EXTRA_STREAM, uriSigned); 
emailIntent.setType("application/pkcs7-signature"); 
try { 
    startActivity(Intent.createChooser(emailIntent, "Sending email...")); 
} catch (android.content.ActivityNotFoundException ex) { 
    Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show(); 
} 

我已經從P7S文件(本SD卡上),從字符串生成confirmEmailBody uriSigned。上面的意圖設置附件屬性是這樣的:

Content-Transfer-Encoding: base64 
Content-Type: application/pkcs7-signature; 
name="smime.p7s" 
Content-Disposition: attachment; 
filename="smime.p7s"; 
size=1886 

但是,附件不能發現的簽名,因爲一般的電子郵件內容類型(位於一起發送方信息,時間等)的multipart /混合時它應該是多部分/簽名的。 如何在意圖中設置標題,使其最終成爲Content-Type:multipart/signed;在電子郵件中?

當它顯示爲簽名後,我會看看它是否會根據電子郵件的正文進行驗證。

P.S.當然,我可以使用javamail輕鬆發送正確簽名的電子郵件,但我想避免向用戶詢問電話的gmail密碼。

回答

1

因爲我沒有設法按照自己的想法找到一種方法,所以我認爲我會發布替代方法來解決問題,仍然避免要求輸入密碼。我使用Google Play服務獲取Auth2令牌。

String token = GoogleAuthUtil.getToken(context, userEmail, "oauth2:https://mail.google.com/"); 
SMTPTransport smtpTransport = connectToSmtp("smtp.gmail.com", 587, userEmail, token, true); 

SMTPMessage smtpMessage = new SMTPMessage(smtpSession); 
smtpMessage.setFrom(new InternetAddress(userEmail)); 
smtpMessage.addRecipient(Message.RecipientType.TO, toAddress); 
smtpMessage.setSubject(subject); 
smtpMessage.setContent(multipart, multipart.getContentType()); 
smtpMessage.saveChanges(); 

smtpTransport.sendMessage(smtpMessage, smtpMessage.getAllRecipients()); 
smtpTransport.close(); 

當然,您可以選擇適合您需要的oauth2範圍。要設置SMTPTransport接受令牌(併爲代碼創建smtpSession以上):

private 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); 
     smtpSession = Session.getInstance(props); 
     smtpSession.setDebug(debug); 

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

     byte[] responseTemp = String.format("user=%s\1auth=Bearer %s\1\1", userEmail, oauthToken).getBytes(); 
     byte[] response = BASE64EncoderStream.encode(responseTemp); 
     transport.issueCommand("AUTH XOAUTH2 " + new String(response), 235); 

     return transport; 
} 

欲瞭解更多信息,請參閱以下鏈接:Authorizing with Google on AndroidOAuth2 info