android socket乱码,[求助]socket发邮件,中文乱码问题

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

如下代码,当标题或者正文有中文时,中文会乱码,显示成方块里有个问号。底下的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:"+lineFeet);

if(isStartWith(f,"250")){

}else{

return "Can't set from";

}

Log.i("status","From set completed");

String toStr = sendCommand("RCPT 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:"+lineFeet);

sb.append("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);

}

}

-----寻狗启示:本人养了条土狗,叫 驴绿绿绿绿绿 ,昨日突然失踪,望看到的吧友告知一声。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值