如下代码,当标题或者正文有中文时,中文会乱码,显示成方块里有个问号。底下的charset改成GB2312或者utf-8也没用。。
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
//import android.util.Base64;
import android.util.*;
public class SocketMail {
String mailServer;
String from;
String to;
String content;
String subject;
String date;
String login;
String pwd;
String lineFeet = "\r\n";
int port;
Socket client;
BufferedReader in;
DataOutputStream os;
SocketMail(String mailServer,int port,String login,String pwd,String from,String to,String subject,String content,String date){
this.mailServer=mailServer;
this.port=port;
this.login=login;
this.pwd=pwd;
this.from=from;
this.to=to;
this.subject=subject;
this.content=content;
this.date=date;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getMailServer() {
return mailServer;
}
public void setMailServer(String mailServer) {
this.mailServer = mailServer;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
private boolean init(){
boolean boo = true;
if(mailServer==null || "".equals(mailServer)){
return false;
}
try {
client = new Socket(mailServer,port);//mailServer,port);
in = new BufferedReader(new InputStreamReader(client.getInputStream())); os = new DataOutputStream(client.getOutputStream());
String isConnect = response();
if(isConnect.startsWith("220")){
}else{
boo = false;
}
} catch (Exception e) {
Log.e("send failed","Can't connect:"+e.toString());
return false;
}
return boo;
}
// 发送smtp指令
private String sendCommand(String msg){
String result = null;
try {
os.writeBytes(msg);
os.flush();
result = response();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//读取服务器端响应信息
private String response(){
String result = null;
try {
result = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 关闭
*/
private void close(){
try {
os.close();
in.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 发送邮件
* @return
*/
public String sendMail(){
//初始化
if(client==null){
if(init()){
Log.i("status","init completed.");
}else{
Log.e("send failed","can't init");
return "can't init";
}
}
//判断from,to
if(from==null || from.isEmpty() || to == null || to.isEmpty()){
return "from or to field is null";
}
//进行握手
String result = sendCommand("HELO "+mailServer+lineFeet);
if(isStartWith(result,"250")){
}else{
Log.e("send failed","Shake failed:"+result);
return "Shake failed:"+result;
}
Log.i("status","Shake completed");
//验证发信人信息
String auth = sendCommand("AUTH LOGIN"+lineFeet);
if(isStartWith(auth,"334")){
}else{
return "Can't request Login";
}
String user = sendCommand(new String(Base64.encodeToString(login.getBytes(),Base64.DEFAULT))); if(isStartWith(user,"334")){
}else{
return "Invalid username";
}
String pass = sendCommand(new String(Base64.encodeToString(pwd.getBytes(),Base64.DEFAULT))); if(isStartWith(pass,"235")){
}else{
Log.e("send failed","Can't login:"+pass);
return "Wrong username or password";
}
Log.i("status","Login completed");
//发送指令
String f = sendCommand("Mail From:<"+from+">"+lineFeet);
if(isStartWith(f,"250")){
}else{
return "Can't set from";
}
Log.i("status","From set completed");
String toStr = sendCommand("RCPT TO:<"+to+">"+lineFeet);
if(isStartWith(toStr,"250")){
}else{
return "Can't set to";
}
Log.i("status","To set completed");
String data = sendCommand("DATA"+lineFeet);
if(isStartWith(data,"354")){
}else{
return "Can't set start";
}
Log.i("status","Start building mail");
StringBuilder sb = new StringBuilder();
sb.append("From:<"+from+">"+lineFeet);
sb.append("To:<"+to+">"+lineFeet);
sb.append("Subject:"+subject+lineFeet);
sb.append("Date:"+date+lineFeet);
sb.append("Content-Type:text/plain;charset=\"ASCII\""+lineFeet); sb.append(lineFeet);
sb.append(content);
sb.append(lineFeet+"."+lineFeet);
String conStr = sendCommand(sb.toString());
if(isStartWith(conStr,"250")){
}else{
Log.e("send failed","Can't send mail:"+conStr);
return "Send failed:"+conStr;
}
Log.i("","0110");
//quit
String quit = sendCommand("QUIT"+lineFeet);
if(isStartWith(quit,"221")){
close();
Log.i("DONE","Mail send");
return "done";
}
else{
return "Can't quit";
}
}
private boolean isStartWith(String res,String with){
return res.startsWith(with);
}
}
-----寻狗启示:本人养了条土狗,叫 驴绿绿绿绿绿 ,昨日突然失踪,望看到的吧友告知一声。
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
//import android.util.Base64;
import android.util.*;
public class SocketMail {
String mailServer;
String from;
String to;
String content;
String subject;
String date;
String login;
String pwd;
String lineFeet = "\r\n";
int port;
Socket client;
BufferedReader in;
DataOutputStream os;
SocketMail(String mailServer,int port,String login,String pwd,String from,String to,String subject,String content,String date){
this.mailServer=mailServer;
this.port=port;
this.login=login;
this.pwd=pwd;
this.from=from;
this.to=to;
this.subject=subject;
this.content=content;
this.date=date;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getMailServer() {
return mailServer;
}
public void setMailServer(String mailServer) {
this.mailServer = mailServer;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
private boolean init(){
boolean boo = true;
if(mailServer==null || "".equals(mailServer)){
return false;
}
try {
client = new Socket(mailServer,port);//mailServer,port);
in = new BufferedReader(new InputStreamReader(client.getInputStream())); os = new DataOutputStream(client.getOutputStream());
String isConnect = response();
if(isConnect.startsWith("220")){
}else{
boo = false;
}
} catch (Exception e) {
Log.e("send failed","Can't connect:"+e.toString());
return false;
}
return boo;
}
// 发送smtp指令
private String sendCommand(String msg){
String result = null;
try {
os.writeBytes(msg);
os.flush();
result = response();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//读取服务器端响应信息
private String response(){
String result = null;
try {
result = in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 关闭
*/
private void close(){
try {
os.close();
in.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 发送邮件
* @return
*/
public String sendMail(){
//初始化
if(client==null){
if(init()){
Log.i("status","init completed.");
}else{
Log.e("send failed","can't init");
return "can't init";
}
}
//判断from,to
if(from==null || from.isEmpty() || to == null || to.isEmpty()){
return "from or to field is null";
}
//进行握手
String result = sendCommand("HELO "+mailServer+lineFeet);
if(isStartWith(result,"250")){
}else{
Log.e("send failed","Shake failed:"+result);
return "Shake failed:"+result;
}
Log.i("status","Shake completed");
//验证发信人信息
String auth = sendCommand("AUTH LOGIN"+lineFeet);
if(isStartWith(auth,"334")){
}else{
return "Can't request Login";
}
String user = sendCommand(new String(Base64.encodeToString(login.getBytes(),Base64.DEFAULT))); if(isStartWith(user,"334")){
}else{
return "Invalid username";
}
String pass = sendCommand(new String(Base64.encodeToString(pwd.getBytes(),Base64.DEFAULT))); if(isStartWith(pass,"235")){
}else{
Log.e("send failed","Can't login:"+pass);
return "Wrong username or password";
}
Log.i("status","Login completed");
//发送指令
String f = sendCommand("Mail From:<"+from+">"+lineFeet);
if(isStartWith(f,"250")){
}else{
return "Can't set from";
}
Log.i("status","From set completed");
String toStr = sendCommand("RCPT TO:<"+to+">"+lineFeet);
if(isStartWith(toStr,"250")){
}else{
return "Can't set to";
}
Log.i("status","To set completed");
String data = sendCommand("DATA"+lineFeet);
if(isStartWith(data,"354")){
}else{
return "Can't set start";
}
Log.i("status","Start building mail");
StringBuilder sb = new StringBuilder();
sb.append("From:<"+from+">"+lineFeet);
sb.append("To:<"+to+">"+lineFeet);
sb.append("Subject:"+subject+lineFeet);
sb.append("Date:"+date+lineFeet);
sb.append("Content-Type:text/plain;charset=\"ASCII\""+lineFeet); sb.append(lineFeet);
sb.append(content);
sb.append(lineFeet+"."+lineFeet);
String conStr = sendCommand(sb.toString());
if(isStartWith(conStr,"250")){
}else{
Log.e("send failed","Can't send mail:"+conStr);
return "Send failed:"+conStr;
}
Log.i("","0110");
//quit
String quit = sendCommand("QUIT"+lineFeet);
if(isStartWith(quit,"221")){
close();
Log.i("DONE","Mail send");
return "done";
}
else{
return "Can't quit";
}
}
private boolean isStartWith(String res,String with){
return res.startsWith(with);
}
}
-----寻狗启示:本人养了条土狗,叫 驴绿绿绿绿绿 ,昨日突然失踪,望看到的吧友告知一声。