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 rece
- 1
- 2
- 3
- 4
前往页