关于邮件乱码的问题!

lgljz 2003-06-03 08:41:04
我现在遇到一个问题:我用linux (red hat 8.0)做WEB服务器,我有一个功能,就是把表单提交的信息以邮件的形式发送出去,我实现的思路是这样的:从表单接受来的参数,我把这些参数都防到一个字符串里,然后对字符串进行转换:String newstr=new String(oldstr.getBytes("iso8859_1"),"gb2312"),然后把这个字符串设置为邮件正文:然后发送出去,可是收邮件的时候,却是乱码,下面是我的原代码,请高手帮忙!
我非常着急,问题解决后,一定高分增送!!!!!!!!!!!!!!!!!!
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page language="java"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.mail.*"%>
<%@ page import ="javax.mail.internet.*"%>
<%@ page import ="javax.activation.*"%>
<%@ page import ="javax.swing.*"%>
<%@ page import ="java.awt.*"%>
<%@ page import= "java.io.*"%>
<%@ page import="java.io.FileWriter"%>
<%@ page import="java.lang.String"%>


<%

//接受表单传过来的参数
String company=request.getParameter("ddname") ;
String address=request.getParameter("addr") ;
String people=request.getParameter("people") ;
String phone=request.getParameter("phone") ;
String taiaddress=request.getParameter("taizhan") ;
String shebei=request.getParameter("shebei");
String tianxian=request.getParameter("gaodu") ;
String pinlv=request.getParameter("ganrao") ;
String time=request.getParameter("time") ;
// String changqiang=new String(request.getParameter("changqian").getBytes("ISO8859_1"),"gb2312" ) ;
String texing=request.getParameter("texing");
String chendu=request.getParameter("chengdu") ;
String content=request.getParameter("neirong") ;
if(content.length() >600)
{session.setAttribute("tscontent",content) ;
%>

<jsp:forward page="puanduan.jsp"/>
<%
}
/////////////////////////////////////////////////////////////////////////////////////
//把表单提交后的信息保存在一个字符串中
String getcontent= " 干扰投诉内容\r\n"+
" 单位名称:" +company+"\r\n"+
" 办公地址:" +address+"\r\n"+
" 联系人:"+people+"\r\n"+
" 联系电话:"+phone+"\r\n"+
" 台站地址:"+taiaddress+"\r\n"+
" 设备名称及型号:"+shebei+"\r\n"+
" 天线高度和极化方式:"+tianxian+"\r\n"+
" 受干扰频率:"+pinlv+"\r\n"+
" 受干扰日期、时段:"+time+"\r\n"+
" 干扰特性:"+texing+"\r\n"+
" 干扰影响程度:"+chendu+"\r\n"+
" 申诉内容:"+content;

/////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////
//定义发件人、收件人、主题等
//String mes="您的信息未能提交,请重新提交";
String havemes="";
String tishi="";
String zhuti="干扰投诉邮件";
//标题-->
String message_subject=new String(zhuti.getBytes("gb2312"),"iso8859_1") ;
//定义邮件正文变量
String mailcontent=getcontent;
out.println(mailcontent);
//正文
String message_body="\r\n"+new String(getcontent.getBytes("gb2312"),"iso8859_1") ;
//JavaMail Session对象
Session psession;
//JavaMail Message对象
Message mesg;
Properties props=new Properties();
//设置邮件方式和接受邮件的主机(或域名)
props.put("mail.smtp.host","lic.gov.cn");
//身份认证
props.put("mail.smtp.auth", "false");
psession=Session.getDefaultInstance(props,null);
//是否调试错误
psession.setDebug(false);
//开始处理邮件
try{
//创建一个邮件
mesg=new MimeMessage(psession);
//FROM地址
mesg.setFrom(new InternetAddress("tousuren@163.com"));
//TO地址;;;; //这里自己添加收件人地址
InternetAddress toAddress=new InternetAddress("ligl@lic.gov.cn");
mesg.addRecipient(Message.RecipientType.TO,toAddress);
//设置标题
mesg.setSubject(message_subject );
//设置正文
mesg.setText(message_body );
//设置传送方式:smtp方式发送
Transport transport = psession.getTransport("smtp");
//这里设置默认邮件的用户名和密码
transport.connect("lic.gov.cn", "","");

////////////////
//最后发送邮件
transport.send(mesg);
havemes="secuss";
}catch(Exception e)
{
havemes="failure";
System.out.println(e);}
if(havemes=="secuss")
{tishi="您已经成功提交";}
else if(havemes=="failure")
{tishi="您的信息未能成功提交,请稍后再试";}

%>
<html><head><title>发送邮件</title>
<jsp:include page="../inc/headline.inc" flush="true"/>
<link rel=stylesheet href="..\css\input.css">
</head>
<body leftmargin="10" topmargin="0" marginwidth="0" marginheight="0">
<table width=760 height=400 align="center">
<tr>
<td height="83" valign="top" colspan="5" align="center">
<jsp:include page="head.jsp" flush="true"/>
</td>
</tr>
<tr height="300" width=760>
<td align="center">
<table>
<tr height=200><td align="center"><font size=4 ><%=tishi%></font></td></tr>
<tr height=20><td align="center"><a href="../index.jsp"><img src="img\rrr.gif" border=0></a></td></tr>
</table>
</td>
</tr>
<tr><td><jsp:include page="..\inc\foot.inc" flush="true"/></td></tr>

</table>

</body>
</html>
...全文
53 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawnming 2003-06-05
  • 打赏
  • 举报
回复
如果是写在bean中进行调用的话,写成mesg.setContent(message_body ,"text/plain;charset=GBK");我尝试用mesg.setContent(message_body ,"text/plain;charset=gb2312");但好像行不通
accp 2003-06-05
  • 打赏
  • 举报
回复
你的message_body用什么编码,就把charset指定为你用的编码~
accp 2003-06-05
  • 打赏
  • 举报
回复
//设置正文
mesg.setText(message_body );
把上面这句改成
mesg.setContent(message_body ,"text/plain;charset=gb2312");
dawnming 2003-06-05
  • 打赏
  • 举报
回复
up
kenshin_z 2003-06-05
  • 打赏
  • 举报
回复
up!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧