Android Open Source - BlitzMail Mail Sender






From Project

Back to project page BlitzMail.

License

The source code is released under:

GNU General Public License

If you think the Android project BlitzMail 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 de.grobox.blitzmail;
//from   ww  w  .jav a2s .  co m
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.provider.JSSEProvider;

import android.util.Log;

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

public class MailSender extends javax.mail.Authenticator {
  private Properties props;
  private Session session;

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

  public MailSender(Properties props) {
    this.props = props;

    // get a new mail session, don't use the default one to ensure fresh sessions
    session = Session.getInstance(props, this);
  }

  protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(props.getProperty("mail.smtp.user", ""), props.getProperty("mail.smtp.pass", ""));
  }

  public synchronized void sendMail(String subject, String body,  String cc, String bcc) throws Exception {
    try {
      MimeMessage message = new MimeMessage(session);
      DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
      message.setFrom(); // uses mail.user property
      message.setSentDate(new Date());
      message.setSubject(subject);
      message.setDataHandler(handler);

      String recipients = props.getProperty("mail.smtp.recipients", "");

      if(recipients.indexOf(',') > 0) {
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
      } else {
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
      }

      if(cc != null) {
        if(cc.indexOf(',') > 0) {
          message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
        } else {
          message.setRecipient(Message.RecipientType.CC, new InternetAddress(cc));
        }
      }

      if(bcc != null) {
        if(bcc.indexOf(',') > 0) {
          message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
        } else {
          message.setRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
        }
      }

      Transport.send(message);
    }
    catch(Exception e) {
      Log.d("MailSender", "ERROR: " + e.getMessage(), e);
      throw e;
    }
  }

  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.provider.JSSEProvider.java
de.grobox.blitzmail.AsyncMailTask.java
de.grobox.blitzmail.Crypto.java
de.grobox.blitzmail.EncryptedEditTextPreference.java
de.grobox.blitzmail.MailSender.java
de.grobox.blitzmail.MailStorage.java
de.grobox.blitzmail.MainActivity.java
de.grobox.blitzmail.NoteActivity.java
de.grobox.blitzmail.NotificationHandlerActivity.java
de.grobox.blitzmail.PrivateConstants.sample_java
de.grobox.blitzmail.SendActivity.java