Android Open Source - my-wallpaper Mail Sender






From Project

Back to project page my-wallpaper.

License

The source code is released under:

MIT License

If you think the Android project my-wallpaper 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.koonen.utils;
/* w  ww .j  a va2  s.  com*/
import java.net.*;
import java.io.*;
import java.util.*;

public class MailSender {

  private Socket smtpHost;
  private BufferedReader In;
  private DataOutputStream Out;

  /**
   * <P>
   * Creates a new connection to the mail host on port 25
   * <P>
   * 
   * @param _Host
   *            The host name where the mail server resides.
   * 
   */
  public MailSender(String _Host) {
    try {
      smtpHost = new Socket(_Host, 25);
      Out = new DataOutputStream(smtpHost.getOutputStream());
      In = new BufferedReader(new InputStreamReader(smtpHost
          .getInputStream()));

      // - Read Welcome message from server
      String LineIn = In.readLine();
      if (LineIn.indexOf("220") == -1)
        throw new Exception("Bad Server");

      if (In.ready())
        LineIn = In.readLine();

      // - Introduce ourselves to the server
      Out.writeBytes("HELO n-ary.com\r\n");
      LineIn = In.readLine();
      if (LineIn.indexOf("250") == -1)
        throw new Exception("No response from Server");
    } catch (Exception E) {
      smtpHost = null;
    }
  }

  public boolean bOk() {
    if (smtpHost == null)
      return false;
    else
      return true;
  }

  /**
   * <P>
   * Sends a new email message to the specified addressee
   * <P>
   * 
   * @param _to
   *            The email address that will receive the email
   * @param _from
   *            The email address that will send the email
   * @param _subject
   *            The email subject
   * @param _body
   *            The email body
   * @return boolean Returns a <code>true</code> if the mail was sent
   *         successfully
   * 
   */
  public boolean send(String _to, String _from, String _subject, String _body) {
    // - Send One Email
    if (smtpHost == null)
      return false;

    try {
      String LineIn;

      // - Set the FROM
      Out.writeBytes("MAIL FROM:<" + _from + ">\r\n");
      LineIn = In.readLine();
      if (LineIn.indexOf("250") == -1)
        throw new Exception("Bad MAIL FROM:");

      // - Set the TO field
      Out.writeBytes("RCPT TO:<" + transform(_to) + ">\r\n");
      LineIn = In.readLine();
      if (LineIn.indexOf("250") == -1)
        throw new Exception("Bad RCPT TO:");

      // - Set the DATA field
      Out.writeBytes("DATA\r\n");
      LineIn = In.readLine();
      if (LineIn.indexOf("354") == -1)
        throw new Exception("Bad DATA");
      Out.writeBytes("From: " + _from + "\r\nSubject: " + _subject
          + "\r\n");
      Out.writeBytes("To: " + _to + "\r\n");

      Out.writeBytes(_body);
      Out.writeBytes("\r\n.\r\n");

      LineIn = In.readLine();
      if (LineIn.indexOf("250") == -1)
        throw new Exception("Bad End of Data");

    } catch (Exception E) {
      return false;
    }

    return true;
  }

  public boolean sendStart(String _to, String _from, String _subject) {
    // - Send One Email
    if (smtpHost == null)
      return false;

    String LineIn = "";
    try {

      // - Set the FROM
      Out.writeBytes("MAIL FROM:<" + _from + ">\r\n");
      LineIn = In.readLine();
      if (LineIn.indexOf("250") == -1)
        throw new Exception("Bad MAIL FROM:");

      // - Set the TO field
      Out.writeBytes("RCPT TO:<" + transform(_to) + ">\r\n");
      LineIn = In.readLine();
      if (LineIn.indexOf("250") == -1)
        throw new Exception("Bad RCPT TO:");

      // - Set the DATA field
      Out.writeBytes("DATA\r\n");

      LineIn = In.readLine();
      if (LineIn.indexOf("354") == -1)
        throw new Exception("Bad DATA");
      Out.writeBytes("From: " + _from + "\r\nSubject: " + _subject
          + "\r\n");
      Out.writeBytes("To: " + _to + "\r\n");

    } catch (Exception E) {
      return false;
    }

    return true;
  }

  public boolean sendStart(String _to[], String _from, String _subject) {
    // - Send One Email
    if (smtpHost == null || _to.length == 0)
      return false;

    String LineIn = "";
    try {

      // - Set the FROM
      Out.writeBytes("MAIL FROM:<" + _from + ">\r\n");
      LineIn = In.readLine();
      if (LineIn.indexOf("250") == -1)
        throw new Exception("Bad MAIL FROM:");

      // - Set the TO field
      for (int x = 0; x < _to.length; x++) {
        Out.writeBytes("RCPT TO:<" + transform(_to[x]) + ">\r\n");
        LineIn = In.readLine();
        if (LineIn.indexOf("250") == -1)
          throw new Exception("Bad RCPT TO:");
      }

      // - Set the DATA field
      Out.writeBytes("DATA\r\n");

      LineIn = In.readLine();
      if (LineIn.indexOf("354") == -1)
        throw new Exception("Bad DATA");
      Out.writeBytes("From: " + _from + "\r\nSubject: " + _subject
          + "\r\n");
      Out.writeBytes("To: " + _to[0] + "\r\n");

    } catch (Exception E) {
      return false;
    }

    return true;
  }

  public boolean sendBodyLine(String _line) {
    try {
      Out.writeBytes(_line + "\n");
      return true;
    } catch (Exception E) {
    }
    return false;
  }

  public boolean sendEnd() {
    try {
      Out.writeBytes("\r\n.\r\n");
      String LineIn = In.readLine();
      if (LineIn.indexOf("250") == -1)
        throw new Exception("Bad End of Data");

      return true;
    } catch (Exception E) {
    }
    return false;
  }

  /**
   * <P>
   * Closes the connection to the mail server
   * <P>
   */
  public void close() {
    try {
      if (smtpHost != null)
        smtpHost.close();
    } catch (Exception E) {
    }
  }

  protected void finalize() throws Throwable {
    close();
  }

  public static String transform(String _to) {
    String newTo = "";

    StringTokenizer st = new StringTokenizer(_to, " ");
    while (st.hasMoreTokens()) {
      newTo = st.nextToken();
      if (newTo.indexOf("@") != -1) {
        return newTo.replace('<', ' ').replace('>', ' ').trim();
      }
    }

    return _to;
  }

  public boolean sendMail(String _to[], String _from, String _cc[],
      String _bcc[], String _subject, Vector<String> _vBody) {

    // - Send One Email
    if (smtpHost == null || _to.length == 0)
      return false;

    String LineIn = "";
    try {

      // - Set the FROM
      Out.writeBytes("MAIL FROM:<" + _from + ">\r\n");
      LineIn = In.readLine();
      if (LineIn.indexOf("250") == -1)
        throw new Exception("Bad MAIL FROM:");

      // - Set the TO field
      for (int x = 0; x < _to.length; x++) {
        Out.writeBytes("RCPT TO:<" + transform(_to[x]) + ">\r\n");
        LineIn = In.readLine();
      }

      // - Set the CC field
      for (int x = 0; x < _cc.length; x++) {
        Out.writeBytes("RCPT TO:<" + transform(_cc[x]) + ">\r\n");
        LineIn = In.readLine();
      }

      // - Set the BCC field
      for (int x = 0; x < _bcc.length; x++) {
        Out.writeBytes("RCPT TO:<" + transform(_bcc[x]) + ">\r\n");
        LineIn = In.readLine();
      }

      // - Set the DATA field
      Out.writeBytes("DATA\r\n");

      LineIn = In.readLine();
      if (LineIn.indexOf("354") == -1)
        throw new Exception("Bad DATA");
      Out.writeBytes("From: " + _from + "\r\nSubject: " + _subject
          + "\r\n");

      // - Send the to field
      Out.writeBytes("To: ");
      for (int x = 0; x < _to.length; x++) {
        Out.writeBytes(transform(_to[x]));
        if (x < _to.length - 1)
          Out.writeBytes(",");
      }
      Out.writeBytes("\r\n");

      // - Send the to field
      Out.writeBytes("Cc: ");
      for (int x = 0; x < _cc.length; x++) {
        Out.writeBytes(transform(_cc[x]));
        if (x < _cc.length - 1)
          Out.writeBytes(",");
      }
      Out.writeBytes("\r\n");

      java.util.Enumeration<?> EE = _vBody.elements();
      while (EE.hasMoreElements())
        Out.writeBytes((String) EE.nextElement() + "\n");

      Out.writeBytes("\r\n.\r\n");
      LineIn = In.readLine();
      if (LineIn.indexOf("250") == -1)
        throw new Exception("Bad End of Data");

      return true;

    } catch (Exception E) {
    }

    return false;
  }

}




Java Source Code List

com.koonen.photostream.ActivityConstants.java
com.koonen.photostream.BootReceiver.java
com.koonen.photostream.CameraPreviewActivity.java
com.koonen.photostream.CategoryActivity.java
com.koonen.photostream.CategoryAdapter.java
com.koonen.photostream.CategoryEditActivity.java
com.koonen.photostream.CropWallpaperTask.java
com.koonen.photostream.Eula.java
com.koonen.photostream.FastBitmapDrawable.java
com.koonen.photostream.FileBrowserActivity.java
com.koonen.photostream.GridLayout.java
com.koonen.photostream.ImageUtilities.java
com.koonen.photostream.PhotostreamActivity.java
com.koonen.photostream.RotationService.java
com.koonen.photostream.ServiceConnector.java
com.koonen.photostream.SetWallpaperTask.java
com.koonen.photostream.UserTask.java
com.koonen.photostream.ViewPhotoActivity.java
com.koonen.photostream.WallPaperExecutor.java
com.koonen.photostream.api.FilePhoto.java
com.koonen.photostream.api.IPhotoService.java
com.koonen.photostream.api.Location.java
com.koonen.photostream.api.PhotoList.java
com.koonen.photostream.api.PhotoSize.java
com.koonen.photostream.api.Photo.java
com.koonen.photostream.api.ResponseHandler.java
com.koonen.photostream.api.ResponseParser.java
com.koonen.photostream.api.ServiceContext.java
com.koonen.photostream.api.ServiceException.java
com.koonen.photostream.api.ServiceManager.java
com.koonen.photostream.api.ServiceNetworkException.java
com.koonen.photostream.api.SourceType.java
com.koonen.photostream.api.Type.java
com.koonen.photostream.api.UserInfo.java
com.koonen.photostream.api.UserNotFoundException.java
com.koonen.photostream.api.User.java
com.koonen.photostream.api.flickr.Auth.java
com.koonen.photostream.api.flickr.FlickrConstants.java
com.koonen.photostream.api.flickr.FlickrService.java
com.koonen.photostream.api.flickr.Perms.java
com.koonen.photostream.dao.CategoryDAO.java
com.koonen.photostream.dao.CategoryList.java
com.koonen.photostream.dao.Category.java
com.koonen.photostream.dao.ImageDAO.java
com.koonen.photostream.dao.PhotoDAO.java
com.koonen.photostream.dao.PhotoUrlListProvider.java
com.koonen.photostream.dao.PhotoUrlList.java
com.koonen.photostream.dao.PhotoUrl.java
com.koonen.photostream.effects.EffectsApplier.java
com.koonen.photostream.effects.EffectsFactory.java
com.koonen.photostream.effects.Rotate3dAnimation.java
com.koonen.photostream.effects.TypeEffect.java
com.koonen.photostream.settings.BackgroundSource.java
com.koonen.photostream.settings.Network.java
com.koonen.photostream.settings.UserPreferences.java
com.koonen.photostream.settings.UserSettingsActivity.java
com.koonen.photostream.settings.WallpaperSettingMode.java
com.koonen.utils.ConfigurationReader.java
com.koonen.utils.DialogUtils.java
com.koonen.utils.Enumeration.java
com.koonen.utils.GroupUtils.java
com.koonen.utils.MailSender.java
com.koonen.utils.StatisticUtils.java
com.koonen.utils.StreamUtils.java