Android Open Source - Android-Universal-Notifier G Mail Sender






From Project

Back to project page Android-Universal-Notifier.

License

The source code is released under:

Apache License

If you think the Android project Android-Universal-Notifier listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mairos.universalnotifier.network;
/*  w ww  .j  a  va2 s.c  o m*/
import javax.activation.DataHandler;   
import javax.activation.DataSource;   
import javax.activation.FileDataSource;
import javax.mail.Message;   
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 com.mairos.universalnotifier.model.Const;
import com.mairos.universalnotifier.model.Logger;

import android.content.Context;
import android.util.Log;

import java.io.ByteArrayInputStream;   
import java.io.File;
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;   
import java.security.Security;   
import java.util.ArrayList;
import java.util.Properties;   

public class GMailSender extends javax.mail.Authenticator {   
    private String mailhost = "smtp.gmail.com";
    private String port = "465";
    private String user;   
    private String password;
    private Session session;   

    static {   
        Security.addProvider(new JSSEProvider());
    }  

    public GMailSender(String user, String password) {
        this.user = user;
        this.password = password;   

        Properties props = new Properties();   
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.host", mailhost);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", port);
        
      props.put("mail.smtp.socketFactory.port", port);
      props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        
      props.put("mail.smtp.socketFactory.fallback", "false");
      props.setProperty("mail.smtp.quitwait", "false");
        
      // not to use Session.getDefaultInstance - problems with a few different connections
        session = Session.getInstance(props, this);
    }
    
    public GMailSender(String user, String password, String server, String port) {   
        this.user = user;   
        this.password = password;
        this.mailhost = server;
        this.port = port;

        Properties props = new Properties();   
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.host", mailhost);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.starttls.enable", "true");
        
        session = Session.getInstance(props, this); 
    }  

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password);
    }   

    public synchronized boolean sendMail(String subject, String body, String sender, String recipients, ArrayList<File> attachments, Context f_context) throws Exception {   
        try {
          MimeMessage message = new MimeMessage(session);    
          message.setSender(new InternetAddress(sender));   
          message.setSubject(subject);   
          
          Multipart mp = new MimeMultipart();
          
          MimeBodyPart mbp1 = new MimeBodyPart();
          mbp1.setText(body);
          mp.addBodyPart(mbp1);
          
          if (attachments != null){
            MimeBodyPart mbp2 = null;
            FileDataSource fds = null;
            for (int i = 0; i < attachments.size(); i++) {
              if (attachments.get(i) != null){
            mbp2 = new MimeBodyPart();
            fds = new FileDataSource(attachments.get(i));
            mbp2.setDataHandler(new DataHandler(fds));
            mbp2.setFileName(fds.getName());
            mp.addBodyPart(mbp2);
              }
        }
          }

          message.setContent(mp);
          
          if (recipients.indexOf(',') > 0)   
              message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
          else  
              message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
          Transport.send(message);
          
          return true;
      } catch(Exception e){
        Log.d(Const.LOG_TAG, "send mail = " + e.toString());
        Logger.addToLog("sendMailError : " + e.toString(), f_context);
        return false;
      }
    }   

    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");   
        }   
    }   
}




Java Source Code List

com.mairos.universalnotifier.UI.LogFragmentTab.java
com.mairos.universalnotifier.UI.MainActivity.java
com.mairos.universalnotifier.UI.SettingsActivity.java
com.mairos.universalnotifier.UI.TabListener.java
com.mairos.universalnotifier.UI.TaskInfoActivity.java
com.mairos.universalnotifier.UI.TasksFragmentTab.java
com.mairos.universalnotifier.model.AlarmManagerBroadcastReceiver.java
com.mairos.universalnotifier.model.AttachmentData.java
com.mairos.universalnotifier.model.Const.java
com.mairos.universalnotifier.model.JSInterpreter.java
com.mairos.universalnotifier.model.LauncherBroadcastReceiver.java
com.mairos.universalnotifier.model.Logger.java
com.mairos.universalnotifier.model.NotificationService.java
com.mairos.universalnotifier.model.NotificationTask.java
com.mairos.universalnotifier.model.TasksQueue.java
com.mairos.universalnotifier.model.TasksXMLLoader.java
com.mairos.universalnotifier.network.GMailSender.java
com.mairos.universalnotifier.network.JSSEProvider.java
com.mairos.universalnotifier.network.NetworkOperations.java
com.mairos.universalnotifier.network.SMSSender.java