I am developing an application in which I want to send mail to the number of receptions using my own API. I reached to mail them, but for this I had a hard encoding of my email id and password. now I want to use the users built into the Primary account to send mail from his own Android phone. I found something like This . but I do not get how to use this, and this does not happen in the if statement. is there any other way to achieve this. please solve my problem.
Thank you in advance
AutoMailActivity.java
public class AutoMailActivity extends Activity { public void onCreate(Bundle savedInstanceState) { setContentView(R.layout.main); AppLogger.LogError("Reached to Step1"); final Button send = (Button) this.findViewById(R.id.send); final EditText address; final EditText subject; final EditText emailtext; address = (EditText) findViewById(R.id.emailaddress); subject = (EditText) findViewById(R.id.emailsubject); emailtext = (EditText) findViewById(R.id.emailtext); send.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub final String email1 = emailtext.getText().toString(); final String sub = subject.getText().toString(); final String adrs = address.getText().toString(); try { AppLogger.LogError("Reached to Step2"); GMailSender sender = new GMailSender(" example@gmail.com ","mypassword"); AppLogger.LogError("Reached to Step3"); sender.sendMail(sub,email1," example@gmail.com ", adrs); AppLogger.LogError("Reached to Step4"); } catch (Exception e) { AppLogger.LogError("Reached to Step5"); Log.e("SendMail", e.getMessage(), e); } } });
Now GmailSender.java
public class GMailSender extends javax.mail.Authenticator { private String mailhost = "smtp.gmail.com"; private String user; private String password; private Session session; static { AppLogger.LogError("Reached to Step1.1"); Security.addProvider(new com.provider.JSSEProvider()); } public GMailSender(String user, String password) { AppLogger.LogError("Reached to Step1.2"); this.user = user; this.password = password; Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); AppLogger.LogError("Reached to Step1.3"); session = Session.getDefaultInstance(props, this); AppLogger.LogError("Reached to Step1.4"); } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception { try{ AppLogger.LogError("Reached to Step1.5"); MimeMessage message = new MimeMessage(session); AppLogger.LogError("Reached to Step1.6"); DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain")); message.setSender(new InternetAddress(sender)); message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipients)); AppLogger.LogError("Reached to Step1.7"); message.setSubject(subject); message.setDataHandler(handler); AppLogger.LogError("Reached to Step1.8"); if (recipients.indexOf(',') > 0) { AppLogger.LogError("Reached to Step1.9"); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients)); Transport.send(message); AppLogger.LogError("Reached to Step2.1"); } else { AppLogger.LogError("Reached to Step2.2"); message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients)); Transport.send(message); AppLogger.LogError("Reached to Step2.3"); } AppLogger.LogError("Reached to Step2.4"); }catch(Exception e) { AppLogger.LogError(e.getMessage()); } } public class ByteArrayDataSource implements DataSource { private byte[] data; private String type; public ByteArrayDataSource(byte[] data, String type) { super(); this.data = data; this.type = type; } public ByteArrayDataSource(byte[] data) { super(); this.data = data; } public void setType(String type) { this.type = type; } public String getContentType() { if (type == null) return "application/octet-stream"; else return type; } public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(data); } public String getName() { return "ByteArrayDataSource"; } public OutputStream getOutputStream() throws IOException { throw new IOException("Not Supported"); } } } } }
user674427
source share