Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_47647075
emmagee
提交
acbacf41
E
emmagee
项目概览
weixin_47647075
/
emmagee
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
E
emmagee
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
acbacf41
编写于
4月 08, 2013
作者:
A
andrewleo2013
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
send performance report by email
上级
c1322231
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
482 addition
and
49 deletion
+482
-49
src/com/netease/qa/emmagee/activity/MainPageActivity.java
src/com/netease/qa/emmagee/activity/MainPageActivity.java
+17
-12
src/com/netease/qa/emmagee/activity/SettingsActivity.java
src/com/netease/qa/emmagee/activity/SettingsActivity.java
+109
-25
src/com/netease/qa/emmagee/service/EmmageeService.java
src/com/netease/qa/emmagee/service/EmmageeService.java
+40
-10
src/com/netease/qa/emmagee/utils/CpuInfo.java
src/com/netease/qa/emmagee/utils/CpuInfo.java
+2
-2
src/com/netease/qa/emmagee/utils/CustomizedAuthenticator.java
...com/netease/qa/emmagee/utils/CustomizedAuthenticator.java
+24
-0
src/com/netease/qa/emmagee/utils/EncryptData.java
src/com/netease/qa/emmagee/utils/EncryptData.java
+177
-0
src/com/netease/qa/emmagee/utils/MailSender.java
src/com/netease/qa/emmagee/utils/MailSender.java
+113
-0
未找到文件。
src/com/netease/qa/emmagee/activity/MainPageActivity.java
浏览文件 @
acbacf41
...
...
@@ -16,12 +16,11 @@
*/
package
com.netease.qa.emmagee.activity
;
import
java.io.BufferedWriter
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.OutputStreamWriter
;
import
java.util.List
;
import
java.util.Properties
;
import
android.app.Activity
;
import
android.app.AlertDialog
;
...
...
@@ -39,8 +38,6 @@ import android.view.MenuItem;
import
android.view.View
;
import
android.view.View.OnClickListener
;
import
android.view.ViewGroup
;
import
android.view.Window
;
import
android.view.WindowManager
;
import
android.widget.BaseAdapter
;
import
android.widget.Button
;
import
android.widget.CompoundButton
;
...
...
@@ -82,9 +79,6 @@ public class MainPageActivity extends Activity {
public
void
onCreate
(
Bundle
savedInstanceState
)
{
Log
.
i
(
LOG_TAG
,
"MainActivity::onCreate"
);
super
.
onCreate
(
savedInstanceState
);
requestWindowFeature
(
Window
.
FEATURE_NO_TITLE
);
getWindow
().
setFlags
(
WindowManager
.
LayoutParams
.
FLAG_FULLSCREEN
,
WindowManager
.
LayoutParams
.
FLAG_FULLSCREEN
);
setContentView
(
R
.
layout
.
mainpage
);
createNewFile
();
processInfo
=
new
ProcessInfo
();
...
...
@@ -132,6 +126,9 @@ public class MainPageActivity extends Activity {
});
}
/**
* customized BroadcastReceiver
*/
public
class
UpdateReceiver
extends
BroadcastReceiver
{
@Override
...
...
@@ -143,6 +140,7 @@ public class MainPageActivity extends Activity {
}
}
@Override
protected
void
onStart
()
{
Log
.
d
(
LOG_TAG
,
"onStart"
);
receiver
=
new
UpdateReceiver
();
...
...
@@ -168,15 +166,22 @@ public class MainPageActivity extends Activity {
private
void
createNewFile
()
{
Log
.
i
(
LOG_TAG
,
"create new file to save setting data"
);
settingTempFile
=
getBaseContext
().
getFilesDir
().
getPath
()
+
"\\Emmagee_Settings.txt"
;
+
"\\EmmageeSettings.properties"
;
Log
.
i
(
LOG_TAG
,
"settingFile = "
+
settingTempFile
);
File
settingFile
=
new
File
(
settingTempFile
);
if
(!
settingFile
.
exists
())
{
try
{
settingFile
.
createNewFile
();
BufferedWriter
bw
=
new
BufferedWriter
(
new
OutputStreamWriter
(
new
FileOutputStream
(
settingFile
)));
bw
.
write
(
"5"
+
"\r\n"
+
"true"
);
bw
.
close
();
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"interval"
,
"5"
);
properties
.
setProperty
(
"isfloat"
,
"true"
);
properties
.
setProperty
(
"sender"
,
""
);
properties
.
setProperty
(
"password"
,
""
);
properties
.
setProperty
(
"recipients"
,
""
);
properties
.
setProperty
(
"smtp"
,
""
);
FileOutputStream
fos
=
new
FileOutputStream
(
settingTempFile
);
properties
.
store
(
fos
,
"Setting Data"
);
fos
.
close
();
}
catch
(
IOException
e
)
{
Log
.
d
(
LOG_TAG
,
"create new file exception :"
+
e
.
getMessage
());
}
...
...
src/com/netease/qa/emmagee/activity/SettingsActivity.java
浏览文件 @
acbacf41
...
...
@@ -16,12 +16,13 @@
*/
package
com.netease.qa.emmagee.activity
;
import
java.io.
BufferedWriter
;
import
java.io.
FileInputStream
;
import
java.io.FileNotFoundException
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.OutputStreamWriter
;
import
java.io.RandomAccessFile
;
import
java.util.Properties
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
android.app.Activity
;
import
android.content.Intent
;
...
...
@@ -35,10 +36,11 @@ import android.widget.EditText;
import
android.widget.Toast
;
import
com.netease.qa.emmagee.R
;
import
com.netease.qa.emmagee.utils.EncryptData
;
/**
* Setting Page of Emmagee
*
*
*/
public
class
SettingsActivity
extends
Activity
{
...
...
@@ -47,8 +49,15 @@ public class SettingsActivity extends Activity {
private
CheckBox
chkFloat
;
private
EditText
edtTime
;
private
String
time
;
private
EditText
edtRecipients
;
private
EditText
edtSender
;
private
EditText
edtPassword
;
private
EditText
edtSmtp
;
private
String
time
,
sender
;
private
String
prePassword
,
curPassword
;
private
String
settingTempFile
;
private
String
recipients
,
smtp
;
private
String
[]
receivers
;
@Override
public
void
onCreate
(
Bundle
savedInstanceState
)
{
...
...
@@ -56,28 +65,32 @@ public class SettingsActivity extends Activity {
super
.
onCreate
(
savedInstanceState
);
setContentView
(
R
.
layout
.
settings
);
final
EncryptData
des
=
new
EncryptData
(
"emmagee"
);
Intent
intent
=
this
.
getIntent
();
settingTempFile
=
intent
.
getStringExtra
(
"settingTempFile"
);
chkFloat
=
(
CheckBox
)
findViewById
(
R
.
id
.
floating
);
edtTime
=
(
EditText
)
findViewById
(
R
.
id
.
time
);
edtSender
=
(
EditText
)
findViewById
(
R
.
id
.
sender
);
edtPassword
=
(
EditText
)
findViewById
(
R
.
id
.
password
);
edtRecipients
=
(
EditText
)
findViewById
(
R
.
id
.
recipients
);
edtSmtp
=
(
EditText
)
findViewById
(
R
.
id
.
smtp
);
Button
btnSave
=
(
Button
)
findViewById
(
R
.
id
.
save
);
boolean
floatingTag
=
true
;
RandomAccessFile
raf
;
try
{
raf
=
new
RandomAccessFile
(
settingTempFile
,
"r"
);
String
f
=
raf
.
readLine
();
if
(
f
==
null
||
(
""
.
equals
(
f
)))
{
time
=
"5"
;
}
else
{
time
=
f
;
}
String
tag
=
raf
.
readLine
();
if
(
"false"
.
equals
(
tag
))
{
floatingTag
=
false
;
}
raf
.
close
();
Properties
properties
=
new
Properties
();
properties
.
load
(
new
FileInputStream
(
settingTempFile
));
String
interval
=
properties
.
getProperty
(
"interval"
).
trim
();
String
isfloat
=
properties
.
getProperty
(
"isfloat"
).
trim
();
sender
=
properties
.
getProperty
(
"sender"
).
trim
();
prePassword
=
properties
.
getProperty
(
"password"
).
trim
();
recipients
=
properties
.
getProperty
(
"recipients"
).
trim
();
time
=
""
.
equals
(
interval
)
?
"5"
:
interval
;
floatingTag
=
"false"
.
equals
(
isfloat
)
?
false
:
true
;
recipients
=
properties
.
getProperty
(
"recipients"
);
smtp
=
properties
.
getProperty
(
"smtp"
);
}
catch
(
FileNotFoundException
e
)
{
Log
.
e
(
LOG_TAG
,
"FileNotFoundException: "
+
e
.
getMessage
());
e
.
printStackTrace
();
...
...
@@ -87,11 +100,40 @@ public class SettingsActivity extends Activity {
}
edtTime
.
setText
(
time
);
chkFloat
.
setChecked
(
floatingTag
);
edtRecipients
.
setText
(
recipients
);
edtSender
.
setText
(
sender
);
edtPassword
.
setText
(
prePassword
);
edtSmtp
.
setText
(
smtp
);
// edtTime.setInputType(InputType.TYPE_CLASS_NUMBER);
btnSave
.
setOnClickListener
(
new
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
time
=
edtTime
.
getText
().
toString
().
trim
();
sender
=
edtSender
.
getText
().
toString
().
trim
();
if
(!
""
.
equals
(
sender
)
&&
!
checkMailFormat
(
sender
))
{
Toast
.
makeText
(
SettingsActivity
.
this
,
"发件人邮箱格式不正确"
,
Toast
.
LENGTH_LONG
).
show
();
return
;
}
recipients
=
edtRecipients
.
getText
().
toString
().
trim
();
receivers
=
recipients
.
split
(
"\\s+"
);
for
(
int
i
=
0
;
i
<
receivers
.
length
;
i
++)
{
if
(!
""
.
equals
(
receivers
[
i
])
&&
!
checkMailFormat
(
receivers
[
i
]))
{
Toast
.
makeText
(
SettingsActivity
.
this
,
"收件人邮箱"
+
receivers
[
i
]
+
"格式不正确"
,
Toast
.
LENGTH_LONG
).
show
();
return
;
}
}
curPassword
=
edtPassword
.
getText
().
toString
().
trim
();
smtp
=
edtSmtp
.
getText
().
toString
().
trim
();
if
(
checkMailConfig
(
sender
,
recipients
,
smtp
,
curPassword
)
==
-
1
)
{
Toast
.
makeText
(
SettingsActivity
.
this
,
"邮箱配置不完整,请完善所有信息"
,
Toast
.
LENGTH_LONG
).
show
();
return
;
}
if
(!
isNumeric
(
time
))
{
Toast
.
makeText
(
SettingsActivity
.
this
,
"输入数据无效,请重新输入"
,
Toast
.
LENGTH_LONG
).
show
();
...
...
@@ -105,12 +147,31 @@ public class SettingsActivity extends Activity {
Toast
.
LENGTH_LONG
).
show
();
}
else
{
try
{
BufferedWriter
bw
=
new
BufferedWriter
(
new
OutputStreamWriter
(
new
FileOutputStream
(
settingTempFile
)));
time
=
Integer
.
toString
(
Integer
.
parseInt
(
time
));
bw
.
write
(
time
+
"\r\n"
+
chkFloat
.
isChecked
());
bw
.
close
();
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
"interval"
,
time
);
properties
.
setProperty
(
"isfloat"
,
chkFloat
.
isChecked
()
?
"true"
:
"false"
);
properties
.
setProperty
(
"sender"
,
sender
);
Log
.
d
(
LOG_TAG
,
"sender="
+
sender
);
try
{
// FIXME 注释
properties
.
setProperty
(
"password"
,
curPassword
.
equals
(
prePassword
)
?
curPassword
:
(
""
.
equals
(
curPassword
)
?
""
:
des
.
encrypt
(
curPassword
)));
Log
.
d
(
LOG_TAG
,
"curPassword="
+
curPassword
);
Log
.
d
(
LOG_TAG
,
"encrtpt="
+
des
.
encrypt
(
curPassword
));
}
catch
(
Exception
e
)
{
properties
.
setProperty
(
"password"
,
""
);
}
properties
.
setProperty
(
"recipients"
,
recipients
);
properties
.
setProperty
(
"smtp"
,
smtp
);
FileOutputStream
fos
=
new
FileOutputStream
(
settingTempFile
);
properties
.
store
(
fos
,
"Setting Data"
);
fos
.
close
();
Toast
.
makeText
(
SettingsActivity
.
this
,
"保存成功"
,
Toast
.
LENGTH_LONG
).
show
();
Intent
intent
=
new
Intent
();
...
...
@@ -136,9 +197,32 @@ public class SettingsActivity extends Activity {
super
.
onDestroy
();
}
private
int
checkMailConfig
(
String
sender
,
String
recipients
,
String
smtp
,
String
curPassword
)
{
if
(!
""
.
equals
(
curPassword
)
&&
!
""
.
equals
(
sender
)
&&
!
""
.
equals
(
recipients
)
&&
!
""
.
equals
(
smtp
))
{
return
1
;
}
else
if
(
""
.
equals
(
curPassword
)
&&
""
.
equals
(
sender
)
&&
""
.
equals
(
recipients
)
&&
""
.
equals
(
smtp
))
{
return
0
;
}
else
return
-
1
;
}
/**
* 检查邮件格式正确性
*/
private
boolean
checkMailFormat
(
String
mail
)
{
String
strPattern
=
"^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*"
+
"[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$"
;
Pattern
p
=
Pattern
.
compile
(
strPattern
);
Matcher
m
=
p
.
matcher
(
mail
);
return
m
.
matches
();
}
/**
* is input a number.
*
*
* @param inputStr
* input string
* @return true is numeric
...
...
src/com/netease/qa/emmagee/service/EmmageeService.java
浏览文件 @
acbacf41
...
...
@@ -18,14 +18,15 @@ package com.netease.qa.emmagee.service;
import
java.io.BufferedWriter
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.OutputStreamWriter
;
import
java.io.RandomAccessFile
;
import
java.text.DecimalFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Calendar
;
import
java.util.Properties
;
import
android.app.Activity
;
import
android.app.Service
;
...
...
@@ -49,8 +50,9 @@ import android.widget.ImageView;
import
android.widget.TextView
;
import
android.widget.Toast
;
//import com.netease.qa.emmagee.activity.MainPageActivity;
import
com.netease.qa.emmagee.utils.CpuInfo
;
import
com.netease.qa.emmagee.utils.EncryptData
;
import
com.netease.qa.emmagee.utils.MailSender
;
import
com.netease.qa.emmagee.utils.MemoryInfo
;
import
com.netease.qa.emmagee.utils.MyApplication
;
import
com.netease.qa.emmagee.R
;
...
...
@@ -89,6 +91,9 @@ public class EmmageeService extends Service {
private
String
processName
,
packageName
,
settingTempFile
;
private
int
pid
,
uid
;
private
boolean
isServiceStop
=
false
;
private
String
sender
,
password
,
recipients
,
smtp
;
private
String
[]
receivers
;
private
EncryptData
des
;
public
static
BufferedWriter
bw
;
public
static
FileOutputStream
out
;
...
...
@@ -106,6 +111,7 @@ public class EmmageeService extends Service {
fomart
=
new
DecimalFormat
();
fomart
.
setMaximumFractionDigits
(
2
);
fomart
.
setMinimumFractionDigits
(
0
);
des
=
new
EncryptData
(
"emmagee"
);
}
@Override
...
...
@@ -158,11 +164,19 @@ public class EmmageeService extends Service {
*/
private
void
readSettingInfo
(
Intent
intent
)
{
try
{
RandomAccessFile
raf
=
new
RandomAccessFile
(
new
File
(
settingTempFile
),
"r"
);
time
=
raf
.
readLine
();
isFloating
=
(
"true"
.
equals
(
raf
.
readLine
()))
?
true
:
false
;
raf
.
close
();
Properties
properties
=
new
Properties
();
properties
.
load
(
new
FileInputStream
(
settingTempFile
));
String
interval
=
properties
.
getProperty
(
"interval"
).
trim
();
isFloating
=
"true"
.
equals
(
properties
.
getProperty
(
"isfloat"
).
trim
())
?
true
:
false
;
sender
=
properties
.
getProperty
(
"sender"
).
trim
();
password
=
properties
.
getProperty
(
"password"
).
trim
();
recipients
=
properties
.
getProperty
(
"recipients"
).
trim
();
time
=
""
.
equals
(
interval
)
?
"5"
:
interval
;
recipients
=
properties
.
getProperty
(
"recipients"
);
receivers
=
recipients
.
split
(
"\\s+"
);
smtp
=
properties
.
getProperty
(
"smtp"
);
}
catch
(
IOException
e
)
{
time
=
"5"
;
isFloating
=
true
;
...
...
@@ -315,7 +329,7 @@ public class EmmageeService extends Service {
}
else
{
Intent
intent
=
new
Intent
();
intent
.
putExtra
(
"isServiceStop"
,
true
);
intent
.
setAction
(
"com.netease.action.emmageeService"
);
// action与接收器相同
intent
.
setAction
(
"com.netease.action.emmageeService"
);
sendBroadcast
(
intent
);
stopSelf
();
}
...
...
@@ -408,8 +422,24 @@ public class EmmageeService extends Service {
handler
.
removeCallbacks
(
task
);
closeOpenedStream
();
isStop
=
true
;
Toast
.
makeText
(
this
,
"测试结果文件:"
+
EmmageeService
.
resultFilePath
,
Toast
.
LENGTH_LONG
).
show
();
boolean
isSendSuccessfully
=
false
;
try
{
isSendSuccessfully
=
MailSender
.
sendTextMail
(
sender
,
des
.
decrypt
(
password
),
smtp
,
"Emmagee Performance Test Report"
,
"see attachment"
,
resultFilePath
,
receivers
);
}
catch
(
Exception
e
)
{
isSendSuccessfully
=
false
;
}
if
(
isSendSuccessfully
)
{
Toast
.
makeText
(
this
,
"测试结果报表已发送至邮箱:"
+
recipients
,
Toast
.
LENGTH_LONG
).
show
();
}
else
{
Toast
.
makeText
(
this
,
"测试结果未成功发送至邮箱,结果保存在:"
+
EmmageeService
.
resultFilePath
,
Toast
.
LENGTH_LONG
).
show
();
}
super
.
onDestroy
();
}
...
...
src/com/netease/qa/emmagee/utils/CpuInfo.java
浏览文件 @
acbacf41
...
...
@@ -169,8 +169,8 @@ public class CpuInfo {
traffic
=
(
lastestTraffic
-
initialTraffic
+
1023
)
/
1024
;
processCpuRatio
=
fomart
.
format
(
100
*
((
double
)
(
processCpu
-
processCpu2
)
/
((
double
)
(
totalCpu
-
totalCpu2
))));
totalCpuRatio
=
fomart
.
format
(
100
*
((
double
)
((
totalCpu
-
idleCpu
)
-
(
totalCpu2
-
idleCpu2
))
/
(
double
)
(
totalCpu
-
totalCpu2
)));
totalCpuRatio
=
fomart
.
format
(
100
*
((
double
)
((
totalCpu
-
idleCpu
)
-
(
totalCpu2
-
idleCpu2
))
/
(
double
)
(
totalCpu
-
totalCpu2
)));
long
pidMemory
=
mi
.
getPidMemorySize
(
pid
,
context
);
String
pMemory
=
fomart
.
format
((
double
)
pidMemory
/
1024
);
long
freeMemory
=
mi
.
getFreeMemorySize
(
context
);
...
...
src/com/netease/qa/emmagee/utils/CustomizedAuthenticator.java
0 → 100644
浏览文件 @
acbacf41
package
com.netease.qa.emmagee.utils
;
import
javax.mail.*
;
/**
* Customized Authenticator
*
*/
public
class
CustomizedAuthenticator
extends
Authenticator
{
String
userName
=
null
;
String
password
=
null
;
public
CustomizedAuthenticator
()
{
}
public
CustomizedAuthenticator
(
String
username
,
String
password
)
{
this
.
userName
=
username
;
this
.
password
=
password
;
}
protected
PasswordAuthentication
getPasswordAuthentication
()
{
return
new
PasswordAuthentication
(
userName
,
password
);
}
}
src/com/netease/qa/emmagee/utils/EncryptData.java
0 → 100644
浏览文件 @
acbacf41
package
com.netease.qa.emmagee.utils
;
import
java.security.*
;
import
javax.crypto.BadPaddingException
;
import
javax.crypto.Cipher
;
import
javax.crypto.IllegalBlockSizeException
;
/**
* 提供加密算法,可以对输入的字符串进行加密、解密操作
*/
public
class
EncryptData
{
private
static
String
strDefaultKey
=
"emmagee"
;
private
Cipher
encryptCipher
=
null
;
private
Cipher
decryptCipher
=
null
;
/**
* 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
* hexStr2ByteArr(String strIn) 互为可逆的转换过程
*
* @param arrB
* 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
*/
public
static
String
byteArr2HexStr
(
byte
[]
arrB
)
throws
Exception
{
int
iLen
=
arrB
.
length
;
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer
sb
=
new
StringBuffer
(
iLen
*
2
);
for
(
int
i
=
0
;
i
<
iLen
;
i
++)
{
int
intTmp
=
arrB
[
i
];
// 把负数转换为正数
while
(
intTmp
<
0
)
{
intTmp
=
intTmp
+
256
;
}
// 小于0F的数需要在前面补0
if
(
intTmp
<
16
)
{
sb
.
append
(
"0"
);
}
sb
.
append
(
Integer
.
toString
(
intTmp
,
16
));
}
return
sb
.
toString
();
}
/**
* 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
* 互为可逆的转换过程
*
* @param strIn
* 需要转换的字符串
* @return 转换后的byte数组
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
* @author
*/
public
static
byte
[]
hexStr2ByteArr
(
String
strIn
)
throws
Exception
{
byte
[]
arrB
=
strIn
.
getBytes
();
int
iLen
=
arrB
.
length
;
// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte
[]
arrOut
=
new
byte
[
iLen
/
2
];
for
(
int
i
=
0
;
i
<
iLen
;
i
=
i
+
2
)
{
String
strTmp
=
new
String
(
arrB
,
i
,
2
);
arrOut
[
i
/
2
]
=
(
byte
)
Integer
.
parseInt
(
strTmp
,
16
);
}
return
arrOut
;
}
/**
* 默认构造方法,使用默认密钥
*
* @throws Exception
*/
public
EncryptData
()
throws
Exception
{
this
(
strDefaultKey
);
}
/**
* 指定密钥构造方法
*
* @param strKey
* 指定的密钥
* @throws Exception
*/
public
EncryptData
(
String
strKey
)
{
try
{
Key
key
=
getKey
(
strKey
.
getBytes
());
encryptCipher
=
Cipher
.
getInstance
(
"DES"
);
encryptCipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
key
);
decryptCipher
=
Cipher
.
getInstance
(
"DES"
);
decryptCipher
.
init
(
Cipher
.
DECRYPT_MODE
,
key
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
/**
* 加密字节数组
*
* @param arrB
* 需加密的字节数组
* @return 加密后的字节数组
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws Exception
*/
public
byte
[]
encrypt
(
byte
[]
arrB
)
throws
Exception
{
return
encryptCipher
.
doFinal
(
arrB
);
}
/**
* 加密字符串
*
* @param strIn
* 需加密的字符串
* @return 加密后的字符串
* @throws Exception
*/
public
String
encrypt
(
String
strIn
)
throws
Exception
{
if
(
strIn
==
null
)
{
strIn
=
""
;
}
return
byteArr2HexStr
(
encrypt
(
strIn
.
getBytes
(
"utf-8"
)));
}
/**
* 解密字节数组
*
* @param arrB
* 需解密的字节数组
* @return 解密后的字节数组
* @throws Exception
*/
public
byte
[]
decrypt
(
byte
[]
arrB
)
throws
Exception
{
return
decryptCipher
.
doFinal
(
arrB
);
}
/**
* 解密字符串
*
* @param strIn
* 需解密的字符串
* @return 解密后的字符串
* @throws Exception
*/
public
String
decrypt
(
String
strIn
)
throws
Exception
{
return
new
String
(
decrypt
(
hexStr2ByteArr
(
strIn
)));
}
/**
* 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
*
* @param arrBTmp
* 构成该字符串的字节数组
* @return 生成的密钥
* @throws java.lang.Exception
*/
private
Key
getKey
(
byte
[]
arrBTmp
)
throws
Exception
{
// 创建一个空的8位字节数组(默认值为0)
byte
[]
arrB
=
new
byte
[
8
];
// 将原始字节数组转换为8位
for
(
int
i
=
0
;
i
<
arrBTmp
.
length
&&
i
<
arrB
.
length
;
i
++)
{
arrB
[
i
]
=
arrBTmp
[
i
];
}
// 生成密钥
Key
key
=
new
javax
.
crypto
.
spec
.
SecretKeySpec
(
arrB
,
"DES"
);
return
key
;
}
}
src/com/netease/qa/emmagee/utils/MailSender.java
0 → 100644
浏览文件 @
acbacf41
package
com.netease.qa.emmagee.utils
;
import
java.util.Date
;
import
java.util.Properties
;
import
javax.activation.CommandMap
;
import
javax.activation.DataHandler
;
import
javax.activation.DataSource
;
import
javax.activation.FileDataSource
;
import
javax.activation.MailcapCommandMap
;
import
javax.mail.Address
;
import
javax.mail.BodyPart
;
import
javax.mail.Message
;
import
javax.mail.MessagingException
;
import
javax.mail.Multipart
;
import
javax.mail.Session
;
import
javax.mail.Transport
;
import
javax.mail.internet.InternetAddress
;
import
javax.mail.internet.MimeBodyPart
;
import
javax.mail.internet.MimeMessage
;
import
javax.mail.internet.MimeMultipart
;
/**
* 发送邮件给多个接收者、抄送邮件
*/
public
class
MailSender
{
private
static
final
String
LOG_TAG
=
MailSender
.
class
.
getSimpleName
();
private
static
final
int
PORT
=
25
;
/**
* 以文本格式发送邮件
*
* 待发送的邮件的信息
*/
public
static
boolean
sendTextMail
(
String
sender
,
String
encryptPassword
,
String
smtp
,
String
subject
,
String
content
,
String
file
,
String
[]
maillists
)
{
if
(
maillists
==
null
||
maillists
.
length
==
0
||
(
maillists
[
0
].
trim
().
equals
(
""
)))
{
return
false
;
}
else
{
// Get system properties
Properties
props
=
new
Properties
();
// Setup mail server
props
.
put
(
"mail.smtp.host"
,
smtp
);
props
.
put
(
"mail.smtp.port"
,
PORT
);
// Get session
props
.
put
(
"mail.smtp.auth"
,
"true"
);
// 如果需要密码验证,把这里的false改成true
// 判断是否需要身份认证
CustomizedAuthenticator
authenticator
=
null
;
if
(
true
)
{
// 如果需要身份认证,则创建一个密码验证器
authenticator
=
new
CustomizedAuthenticator
(
sender
,
encryptPassword
);
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session
sendMailSession
=
Session
.
getInstance
(
props
,
authenticator
);
try
{
// 根据session创建一个邮件消息
Message
mailMessage
=
new
MimeMessage
(
sendMailSession
);
// 创建邮件发送者地址
Address
from
=
new
InternetAddress
(
sender
);
// 设置邮件消息的发送者
mailMessage
.
setFrom
(
from
);
// 创建邮件的接收者地址,并设置到邮件消息中
Address
[]
tos
=
null
;
tos
=
new
InternetAddress
[
maillists
.
length
];
for
(
int
i
=
0
;
i
<
maillists
.
length
;
i
++)
{
tos
[
i
]
=
new
InternetAddress
(
maillists
[
i
]);
}
// Message.RecipientType.TO属性表示接收者的类型为TO
mailMessage
.
setRecipients
(
Message
.
RecipientType
.
TO
,
tos
);
// 设置邮件消息的主题
mailMessage
.
setSubject
(
subject
);
// 设置邮件消息发送的时间
mailMessage
.
setSentDate
(
new
Date
());
BodyPart
bodyPart
=
new
MimeBodyPart
();
bodyPart
.
setText
(
content
);
MimeBodyPart
attachPart
=
new
MimeBodyPart
();
DataSource
source
=
new
FileDataSource
(
file
);
attachPart
.
setDataHandler
(
new
DataHandler
(
source
));
attachPart
.
setFileName
(
file
);
Multipart
multipart
=
new
MimeMultipart
();
multipart
.
addBodyPart
(
bodyPart
);
multipart
.
addBodyPart
(
attachPart
);
mailMessage
.
setContent
(
multipart
);
MailcapCommandMap
mc
=
(
MailcapCommandMap
)
CommandMap
.
getDefaultCommandMap
();
mc
.
addMailcap
(
"text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"
);
mc
.
addMailcap
(
"text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"
);
mc
.
addMailcap
(
"text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"
);
mc
.
addMailcap
(
"multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"
);
mc
.
addMailcap
(
"message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"
);
CommandMap
.
setDefaultCommandMap
(
mc
);
Transport
.
send
(
mailMessage
);
return
true
;
}
catch
(
MessagingException
ex
)
{
ex
.
printStackTrace
();
}
}
return
false
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录