2013-02-27 138 views
9

我想使用用戶已設置的任何默認郵件提供程序在我的應用程序中發送電子郵件。無需任何用戶交互,就可以發送電子郵件。如果我可以避免顯示任何UI,那將是最好的解決方案。唯一不能接受的是硬編碼或請求用戶輸入他們的電子郵件憑證。用戶必須已經使用他們的憑據設置了他們的帳戶,例如Gmail。我所有的應用程序都使用已安裝的現有電子郵件提供程序。在沒有用戶交互的情況下在Android中發送電子郵件

我在這裏找到的大多數帖子都使用Intent,但從我所瞭解的情況來看,這將顯示電子郵件用戶界面,並要求用戶按下「發送」按鈕。

+3

我已經搜索了這個很長一段時間。雖然這樣做似乎很合乎邏輯,但實際上沒有辦法。事實上,你所能做的就是發送一封電子郵件,意圖讓用戶自己發送電子郵件,或者輸入自己的憑證,即使你不想。 – Alpay 2013-02-27 08:17:53

+0

爲了保護用戶,您可能會認爲Google會提供一個簡單的API,如果用戶授予應用權限,則讓該應用使用帳戶設置發送電子郵件,而無需訪問用戶的用戶名和密碼。 – AndroidDev 2013-02-27 08:20:38

+0

@ChintanRathod在允許的情況下,應該可以在沒有用戶交互的情況下發送電子郵件。這樣思考並不荒謬。然而,有一種方法可以做到這一點,但我不知道。我無法實現這一點,並分享我的經驗。 – Alpay 2013-02-27 08:44:43

回答

8

經過一番探索後,我認爲有一個解決方案。 Google現在增加了對使用OAuth 2.0進行Gmail身份驗證的支持,該功能可避免訪問用戶的用戶名和密碼。由於我的應用程序需要用戶擁有Gmail帳戶,因此這可能是解決方案。當然這對任何其他不支持OAuth的電子郵件提供商都無效,但由於Google關心保護用戶名/密碼,這種方法似乎是正確的。需要研究如何從我的應用程序中使用OAuth,但理論上它應該是可能的。一旦用戶向Google服務授予權限,該應用程序將收到一個用於該權限保留期限的令牌。這仍然是尚未解決的問題是,Gmail的是否支持發送電子郵件使用OAuth:

Google Brings OAuth 2.0 Support To Gmail And Google Talk To Make Third-Party Apps More Secure

OAuth2 Support for Gmail

Google's sample code for Gmail support and OAuth2

Android app demonstrating how to send e-mail using OAuth

YouTube video showing how a mobile app uses 2 step verification in an app

Android docs on using OAuth 2.0

Code sample to send e-mail using OAuth2

+1

「這個問題仍然沒有解決,Gmail是否支持使用OAuth發送電子郵件」爲什麼?查看此處:https://developers.google.com/google-apps/gmail/xoauth2_protocol#smtp_protocol_exchange – user1050755 2013-04-06 13:01:34

0

試試這個代碼...

public class SendAttachment{ 
         public static void main(String [] args){ 
       //to address 
         String to="[email protected]";//change accordingly 
         //from address 
         final String user="[email protected]";//change accordingly 
         final String password="password";//change accordingly 
         MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); 
         mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); 
         mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); 
         mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
         mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
         mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); 
         CommandMap.setDefaultCommandMap(mc); 
         //1) get the session object 
         Properties properties = System.getProperties(); 
         properties.put("mail.smtp.port", "465"); 
         properties.put("mail.smtp.host", "smtp.gmail.com"); 
         properties.put("mail.smtp.socketFactory.port", "465"); 
         properties.put("mail.smtp.socketFactory.class", 
           "javax.net.ssl.SSLSocketFactory"); 
         properties.put("mail.smtp.auth", "true"); 
         properties.put("mail.smtp.port", "465"); 

         Session session = Session.getDefaultInstance(properties, 
         new javax.mail.Authenticator() { 
         protected PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(user,password); 
         } 
         }); 

         //2) compose message 
         try{ 
         MimeMessage message = new MimeMessage(session); 
         message.setFrom(new InternetAddress(user)); 
         message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); 
         message.setSubject("Hii"); 
         //3) create MimeBodyPart object and set your message content  
         BodyPart messageBodyPart1 = new MimeBodyPart(); 
         messageBodyPart1.setText("How is This"); 
         //4) create new MimeBodyPart object and set DataHandler object to this object  
         MimeBodyPart messageBodyPart2 = new MimeBodyPart(); 
        //Location of file to be attached 
         String filename = Environment.getExternalStorageDirectory().getPath()+"/R2832.zip";//change accordingly 
         DataSource source = new FileDataSource(filename); 
         messageBodyPart2.setDataHandler(new DataHandler(source)); 
         messageBodyPart2.setFileName("Hello"); 
         //5) create Multipart object and add MimeBodyPart objects to this object  
         Multipart multipart = new MimeMultipart(); 
         multipart.addBodyPart(messageBodyPart1); 
         multipart.addBodyPart(messageBodyPart2); 
         //6) set the multiplart object to the message object 
         message.setContent(multipart); 
         //7) send message 
         Transport.send(message); 
         System.out.println("MESSAGE SENT...."); 
         }catch (MessagingException ex) {ex.printStackTrace();} 
         } 
        } 
+0

添加jar文件activation.jar,additionnal.jar,javax.mail.jar – Rashid 2014-05-14 04:55:51

相關問題