Android系统使用JAVA MAIL发送非gmail邮件(未验证)

转载 2013年12月02日 11:28:37

http://download.csdn.net/download/leoly003/4092287


package org.leoly;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
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.util.Log;

public class MailSender
{
	private final String Tag = "SMailSender";

	private String mSmtp_host;

	private String mFrom_userName;

	private String mFrom_passWord;

	private String mShow_name = "Jiahe";

	private MimeMessage mMimeMsg; // Òª·¢Ë͵ÄemailÐÅÏ¢

	private Session mSession;

	private Multipart mp; // Žæ·ÅÓÊŒþµÄtitle ÄÚÈݺ͞œŒþ

	private String mSubject;

	private String mContent;

	private String mToId = null;

	private String mCcId = null;

	private String mBccId = null;

	private String mFilePath = null;

	private final int NONE = 0;

	private final int CONNECTING = 1;

	private final int CONNECTED = 2;

	private final int SENDING = 3;

	private final int SENDED = 4;

	private int mState;

	public enum MailType
	{
		TYPE_GMAIL, TYPE_126, TYPE_163, TYPE_QQ;
	}

	private MailType mailType;

	// private Properties props;

	/**
	 * ¹¹Ô캯Êý
	 * @param hostName
	 */
	public MailSender()
	{
		// mSmtp_host = "smtp.gmail.com";
		mp = new MimeMultipart();
	}

	public MailSender(String hostName)
	{
		// mSmtp_host = hostName;
		mp = new MimeMultipart();
	}

	/**
	 * ÉèÖ÷¢ŒþÈ˵ÄÓû§ÃûºÍÃÜÂë Ä¿Ç°Ö»Ö§³Ögmail¡¢126¡¢163ÓÊÏä
	 * @param _userName
	 * @param _password
	 */
	public boolean setAuthor(String _userName, String _password)
	{
		mFrom_userName = _userName;
		mFrom_passWord = _password;
		return setHost(mFrom_userName); // ÉèÖÃÓÊÏäÀàÐÍ
	}

	/**
	 * ÉèÖÃÓÊŒþÖ÷Ìâ
	 * @param subject
	 */
	public void setSubject(String subject)
	{
		if (subject != null)
			mSubject = subject;
	}

	/**
	 * ÉèÖÃÓÊŒþÄÚÈÝ
	 * @param content
	 */
	public void setContent(String content)
	{
		if (content != null)
			mContent = content;
	}

	/**
	 * ÉèÖÃÊÕŒþÈË£¬ÊÕŒþÈËÖ®ŒäÓÃ";"žô¿ª
	 * @param to
	 * @return
	 */
	public void setTo(String toId)
	{
		mToId = toId;
	}

	/**
	 * ÉèÖÃÓÊŒþ³­ËÍÈË£¬³­ËÍÈËÖ®ŒäÓÃ";"žô¿ª
	 * @param copyto
	 * @return
	 */
	public void setCc(String ccId)
	{
		mCcId = ccId;
	}

	/**
	 * ÉèÖðµ³­ËÍÈË£¬°µ³­ËÍÈËÖ®ŒäÓÃ";"žô¿ª
	 * @param bcopyto
	 * @return
	 */
	public void setBcc(String bccId)
	{
		mBccId = bccId;
	}

	public void setShowName(String name)
	{
		mShow_name = name;
	}

	/**
	 * ÉèÖÞœŒþ·Ÿ¶£¬Â·Ÿ¶Ö®ŒäÓÃ";"žô¿ª
	 * @param path
	 */
	public void addFileAffix(String path)
	{
		mFilePath = path;
	}

	public int getMailState()
	{
		return mState;
	}

	private boolean setSendToMsg(String to)
	{
		if (to.equals("") || to == null)
		{
			return false;
		}
		try
		{
			String sendto[];
			sendto = to.split(";");
			for (int i = 0; i < sendto.length; i++)
			{
				mMimeMsg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(sendto[i]));
			}
			return true;
		}
		catch (Exception e)
		{
			return false;
		}
	}

	private boolean setCopyToMsg(String copyto)
	{
		if (copyto.equals("") || copyto == null)
		{
			return false;
		}
		try
		{
			String copy[];
			copy = copyto.split(";");
			for (int i = 0; i < copy.length; i++)
			{
				mMimeMsg.addRecipients(Message.RecipientType.CC, InternetAddress.parse(copy[i]));
			}
			return true;
		}
		catch (Exception e)
		{
			return false;
		}
	}

	private boolean setBCopyToMsg(String bcopyto)
	{
		if (bcopyto.equals("") || bcopyto == null)
		{
			return false;
		}

		try
		{
			String bcopy[];
			bcopy = bcopyto.split(";");
			for (int i = 0; i < bcopy.length; i++)
			{
				mMimeMsg.addRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcopy[i]));
			}
			return true;
		}
		catch (Exception e)
		{
			return true;
		}
	}

	private boolean setContentMp(String conMp)
	{
		try
		{
			BodyPart bp = new MimeBodyPart();
			bp.setContent("<meta http-equiv=Context-Type context=text/html;charset=gb2312>" + conMp,
					"text/html;charset=GB2312");
			mp.addBodyPart(bp);
			return true;
		}
		catch (Exception e)
		{
			System.out.println("Set context Faild! " + e);
			return false;
		}
	}

	private boolean addFile(String filePath)
	{
		// System.out.println("add affix..");
		if (filePath.equals("") || filePath == null)
		{
			return false;
		}
		String file[];
		file = filePath.split(";");
		// System.out.println("you have " + file.length + " affix!");
		try
		{
			for (int i = 0; i < file.length; i++)
			{
				BodyPart bp = new MimeBodyPart();
				FileDataSource fileds = new FileDataSource(file[i]);
				bp.setDataHandler(new DataHandler(fileds));
				bp.setFileName(fileds.getName());
				mp.addBodyPart(bp);
			}
			return true;
		}
		catch (Exception e)
		{
			Log.d(Tag, "add affix: " + filePath + "--faild!" + e);
			System.err.println("add affix: " + filePath + "--faild!" + e);
			return false;
		}
	}

	private boolean setHost(String mailAddress)
	{
		if (mailAddress.contains("@gmail."))
		{
			mSmtp_host = "smtp.gmail.com";
			mailType = MailType.TYPE_GMAIL;
			return true;
		}
		if (mailAddress.contains("@126."))
		{
			mSmtp_host = "pop.126.com";
			mailType = MailType.TYPE_126;
			return true;
		}
		if (mailAddress.contains("@163."))
		{
			mSmtp_host = "pop.163.com";
			mailType = MailType.TYPE_163;
			return true;
		}
		else
			return false;
		/*
		 * if(mailAddress.contains("@qq.")){ mSmtp_host =
		 * "smtp.qq.com"; mailType = MailType.TYPE_QQ; }
		 */
	}

	public void sendMail()
	{
		try
		{
			mState = NONE;
			Properties props = System.getProperties();
			props = System.getProperties();
			// Èç¹ûΪgmailÓÊÏ䣬ÐèÒªÉèÖÃÏÂÁœÏî
			if (mailType == MailType.TYPE_GMAIL)
			{
				props.put("mail.smtp.starttls.enable", "true");
				props.put("mail.smtp.port", "587");
			}

			props.put("mail.smtp.host", mSmtp_host);
			props.put("mail.smtp.user", mFrom_userName);
			props.put("mail.smtp.password", mFrom_passWord);
			props.put("mail.smtp.auth", "true");

			mSession = Session.getDefaultInstance(props, null);
			mSession.setDebug(false);

			mMimeMsg = new MimeMessage(mSession);
			mMimeMsg.setFrom(new InternetAddress(mFrom_userName));

			// mMimeMsg.setContent(mContent,
			// "text/html;charset=utf-8");

			if (mToId != null)
				setSendToMsg(mToId);
			if (mCcId != null)
				setCopyToMsg(mCcId);
			if (mBccId != null)
				setBCopyToMsg(mBccId);

			if (mSubject != null)
			{
				mMimeMsg.setSubject(mSubject);
			}
			if (mShow_name != null)
			{
				mMimeMsg.setFrom(new InternetAddress(mShow_name + "<" + mFrom_userName + ">"));
			}
			if (mContent != null)
			{
				setContentMp(mContent);
				mMimeMsg.setContent(mp);
			}
			if (mFilePath != null)
			{
				addFile(mFilePath);
			}

			System.out.println(mMimeMsg.getAllRecipients().toString());
			Transport transport = mSession.getTransport("smtp");
			System.out.println("connecting...");
			mState = CONNECTING;
			transport.connect(mSmtp_host, mFrom_userName, mFrom_passWord);
			mState = CONNECTED;
			Log.i(Tag, "Connected to " + mFrom_userName + " succuss");
			System.out.println("sending...");
			mState = SENDING;
			transport.sendMessage(mMimeMsg, mMimeMsg.getAllRecipients());
			mState = SENDED;
			transport.close();
			Log.i(Tag, "Send a mail to " + mToId);
			System.out.println("send an email to " + mToId + " success");
		}
		catch (Exception e)
		{
			System.out.println("failure! ");
			Log.d(Tag, " failure! ", e);
			e.printStackTrace();
		}

	}

	public void receiveMail() throws Exception
	{
		Properties props = new Properties();
		Session session = Session.getInstance(props);
		Store store = session.getStore("pop3");
		store.connect(mSmtp_host, mFrom_userName, mFrom_passWord);
		Folder folder = store.getFolder("INBOX");
		folder.open(Folder.READ_ONLY);
		Message[] msgs = folder.getMessages();
		for (Message msg : msgs)
		{
			System.out.println(msg.getSubject());
		}

		store.close();
	}
}


相关文章推荐

Android利用Java Mail实现自动发送邮件

仿照服务器端,Android自动发送邮件

使用Java Mail Authenticator子类进行用户认证来发送电子邮件示例

/** * CrazyItTest * 使用JavaMail的Authenticator类进行用户认证发送带附件的电子邮件示例 */ package com.labci.javamail...

Java mail使用QQ邮箱发送邮件配置方案及异常处理

java通过SMTP发送QQ邮件的完全步骤 首先,我们需要打开QQ邮箱的SMTP服务,因为QQ邮箱对于一般的用户都是默认关闭SMTP服务的。 打开QQ邮箱,点击设置  点击帐户  找...

使用Java Mail发送邮件

package cn.cgw.util.mail;      import java.util.Properties;      import javax.activation.DataHa...

JAVA 使用JavaMail发送邮件(mail.jar和activtion.jar)

使用JavaMail发送邮件需要用到mail.jar和activtion.jar两个包。 该类实现了较完整的邮件发送功能,包括以HTML格式发送,添加附件和抄送人。

Java mail使用QQ邮箱发送邮件配置方案

Java mail使用QQ邮箱发送邮件配置方案java mail配置了QQ邮箱作为服务器发送邮件,结果导致出现503异常:Authentication failed; nested exception...

使用java mail发送带附件的邮件

参考了部分网上资料。   /** * * 方法名称: sendMail|描述: 发送带附件的邮件 * @param smtpServer * @param username * @p...

【笔记】Java Mail发送邮件(使用QQ邮箱)

Java mail 使用QQ邮箱发送邮件

【JAVA】使用javax.mail发送邮件

本文主要解决以下问题: 1、如何用java实现邮件发送功能? 2、如何发送给多个人、抄送给多个人? 3、如何发送携带附件的邮件? 开始说正文。 首先,发送邮件肯定要有个发件人,所有要...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)