1. Download our Official Android App: Forums for Android!

Authenticating gmail with JavaMail and AccountManager help.

Discussion in 'Android Apps & Games' started by tanema, Sep 22, 2011.

  1. tanema

    tanema Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Sep 22, 2011

    Sep 22, 2011
    1
    0
    5
    so I am hoping to send out emails without user interaction using the default gmail account. So far I can do it with the user still entering the password but I would rather them not have to. Right now I can get the gmail password but it looks like a hash which I have tried sending but does not seem to work. I was thinking if I could maybe send a gmail auth token somehow, that would work but I cannot figure out how to do that either. Any help is appreciated.

    Here is my Class currently.

    Code (Text):
    1.  
    2. import java.security.Security;
    3. import java.util.Date;
    4. import java.util.Properties;
    5.  
    6. import javax.mail.Authenticator;
    7. import javax.mail.Message;
    8. import javax.mail.MessagingException;
    9. import javax.mail.Multipart;
    10. import javax.mail.PasswordAuthentication;
    11. import javax.mail.Session;
    12. import javax.mail.Transport;
    13. import javax.mail.internet.InternetAddress;
    14. import javax.mail.internet.MimeBodyPart;
    15. import javax.mail.internet.MimeMessage;
    16. import javax.mail.internet.MimeMultipart;
    17.  
    18. import android.accounts.Account;
    19. import android.accounts.AccountManager;
    20. import android.content.Context;
    21. import android.content.Intent;
    22. import android.util.Log;
    23.  
    24. public class GMailSender {  
    25.     private String mailhost = "smtp.gmail.com";  
    26.     private Session session;
    27.    
    28.     static {  
    29.         Security.addProvider(new org.mortbay.ijetty.console.JSSEProvider());  
    30.     }
    31.  
    32.     public GMailSender(Context context) throws Exception {
    33.         Properties props = new Properties();  
    34.         props.setProperty("mail.transport.protocol", "smtp");  
    35.         props.setProperty("mail.host", this.mailhost);  
    36.         props.put("mail.smtp.auth", "true");  
    37.         props.put("mail.smtp.port", "465");  
    38.         props.put("mail.smtp.socketFactory.port", "465");  
    39.         props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");  
    40.         props.put("mail.smtp.socketFactory.fallback", "false");  
    41.         props.setProperty("mail.smtp.quitwait", "false");  
    42.        
    43.         boolean debug = false;
    44.  
    45.         Authenticator auth = new SMTPAuthenticator(context);
    46.         this.session = Session.getDefaultInstance(props, auth);
    47.         this.session.setDebug(debug);  
    48.     }  
    49.    
    50.     public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {  
    51.         try{
    52.             MimeMessage message = new MimeMessage(session);    
    53.             message.setSender(new InternetAddress(sender));  
    54.             message.setSubject(subject);  
    55.             message.setText(body);
    56.             if (recipients.indexOf(',') > 0)  
    57.                 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));  
    58.             else  
    59.                 message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));  
    60.            
    61.             Transport.send(message);  
    62.         }catch(Exception e){
    63.  
    64.         }
    65.     }  
    66.    
    67.     /**
    68.     * SimpleAuthenticator is used to do simple authentication
    69.     * when the SMTP server requires it.
    70.     */
    71.     private class SMTPAuthenticator extends javax.mail.Authenticator
    72.     {
    73.         private String user;
    74.         private String password;
    75.        
    76.         public SMTPAuthenticator(Context context){
    77.             this.user = getEmail(context);  
    78.             this.password = getPassword(context);
    79.             Log.w("GMAILSENDER ","username : " + this.user + " password: " + this.password);
    80.         }
    81.        
    82.         //not working no context to be had through ijetty
    83.         private String getEmail(Context context) {
    84.           AccountManager accountManager = AccountManager.get(context);
    85.           Account account = getAccount(accountManager);
    86.           if (account == null) {
    87.             return null;
    88.           } else {
    89.             return account.name;
    90.           }
    91.         }
    92.        
    93.         private String getPassword(Context context) {
    94.             AccountManager accountManager = AccountManager.get(context);
    95.             Account account = getAccount(accountManager);
    96.             if (account == null) {
    97.                 return null;
    98.             } else {
    99.                 return accountManager.getPassword(account);
    100.             }
    101.         }
    102.        
    103.         // not working
    104.         private Account getAccount(AccountManager accountManager) {
    105.             Account[] accounts = accountManager.getAccountsByType("com.google");
    106.             Account account;
    107.             if (accounts.length > 0) {
    108.                 account = accounts[0];      
    109.             } else {
    110.                 account = null;
    111.             }
    112.             return account;
    113.         }
    114.        
    115.         public PasswordAuthentication getPasswordAuthentication()
    116.         {
    117.             return new PasswordAuthentication(this.user, this.password);
    118.         }
    119.     }
    120. }  
     

    Advertisement

Share This Page

Loading...