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.
Here is my Class currently.
Code:
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class GMailSender {
private String mailhost = "smtp.gmail.com";
private Session session;
static {
Security.addProvider(new org.mortbay.ijetty.console.JSSEProvider());
}
public GMailSender(Context context) throws Exception {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", this.mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
boolean debug = false;
Authenticator auth = new SMTPAuthenticator(context);
this.session = Session.getDefaultInstance(props, auth);
this.session.setDebug(debug);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setText(body);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}catch(Exception e){
}
}
/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator
{
private String user;
private String password;
public SMTPAuthenticator(Context context){
this.user = getEmail(context);
this.password = getPassword(context);
Log.w("GMAILSENDER ","username : " + this.user + " password: " + this.password);
}
//not working no context to be had through ijetty
private String getEmail(Context context) {
AccountManager accountManager = AccountManager.get(context);
Account account = getAccount(accountManager);
if (account == null) {
return null;
} else {
return account.name;
}
}
private String getPassword(Context context) {
AccountManager accountManager = AccountManager.get(context);
Account account = getAccount(accountManager);
if (account == null) {
return null;
} else {
return accountManager.getPassword(account);
}
}
// not working
private Account getAccount(AccountManager accountManager) {
Account[] accounts = accountManager.getAccountsByType("com.google");
Account account;
if (accounts.length > 0) {
account = accounts[0];
} else {
account = null;
}
return account;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(this.user, this.password);
}
}
}