发送邮件后.收到的是乱码.大家帮我一下.怎么解决?下面是代码..在线等..

SEASKYLONG 2004-12-15 10:22:45
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%>
<%@ page import="java.util.*"%>
<HTML>
<head> <TITLE>JSP meets JavaMail, what a sweet combo.</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<BODY>
<%!
//byte[] subjectTemp= request.getParameter("Subject").getBytes("ISO8859_1");
public class PopupAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username, password;
username = "user";
password = "pass";
return new PasswordAuthentication(username, password);
}
}
%>
<%!
public String toChi(String input)
{
try
{
byte[] bytes = input.getBytes("ISO8859-1");
return new String(bytes);
}
catch(Exception ex)
{
}
return null;
}
%>

<%
try{
String name = request.getParameter("Name");
String subject = request.getParameter("Subject");
String Hi=request.getParameter("Body");
byte[] tmpbyte=Hi.getBytes("ISO8859_1");
Hi=new
String(tmpbyte);
// out.print(Hi);

//String body = (request.getParameter("Body").getBytes("ISO8859-1"));
Properties props = new Properties();
Session sendMailSession; Store store;
Transport transport;
props.put("mail.smtp.host","yourhost");
props.put("mail.smtp.auth","true");
Authenticator auth = new PopupAuthenticator();
sendMailSession = Session.getInstance(props,auth);
sendMailSession.setDebug(true);
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress("frommail"));
newMessage.setRecipient(
Message.RecipientType.TO, new InternetAddress("tomail"));
newMessage.setSubject(subject);
newMessage.setSentDate(new Date());
//String body = ("hello are you你好啊.?");
newMessage.setText(Hi);
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
%>
<P>Your mail has been sent.</P>
<%
} catch(MessagingException m) {
out.println(m.toString());
}
%>
</BODY>
</HTML>
...全文
229 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
SEASKYLONG 2004-12-16
  • 打赏
  • 举报
回复
主要是改变收到时的乱码.是要怎么解决文字.是在tomcat4.1下的
CALM 2004-12-15
  • 打赏
  • 举报
回复
ISO8859-1改称gb2312吧
qljsd 2004-12-15
  • 打赏
  • 举报
回复
package com.netease.util;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MailSender {

static Log log = LogFactory.getLog(MailSender.class);

private Properties props; //系统属性
private String hostName; //邮件服务器名
private Session session; //邮件会话对象

private String userName; //smtp认证用戶名
private String password; //smtp认证密码
private boolean isNeedAuth; //是否进行smtp身份认证标志

private MimeMessage mimeMsg; //MIME邮件消息对象

private Multipart mp; //Multipart对象,邮件內容,标题,附件等內容均添加到其中后再生成MimeMessage物件

public MailSender() {
hostName = "hostName"; //指定默认的邮件服务器
userName = "userName";
password = "password";
isNeedAuth = true;
init();
}

public MailSender(
String hostName,
String userName,
String password,
boolean isNeedAuth) {
this.hostName = hostName;
this.userName = userName;
this.password = password;
this.isNeedAuth = isNeedAuth;
init();
}

/**
* @return boolean
*/
private void init() {
// 初始化系统属性
props = System.getProperties();
props.put("mail.smtp.host", hostName);
if (isNeedAuth)
props.put("mail.smtp.auth", "true");
else
props.put("mail.smtp.auth", "false");
// 初始化邮件会话
session = Session.getDefaultInstance(props, null);
// 初始化MIME邮件消息
mimeMsg = new MimeMessage(session);
// 初始化邮件Multipart对象
mp = new MimeMultipart();
}

/**
* 设置发信人
* @param name String
* @param pass String
*/
public void setFrom(String from) throws MailException {
if (from == null || from.length() <= 0)
throw new MailException("Form is empty.");

log.debug("from: " + from);
try {
mimeMsg.setFrom(new InternetAddress(from));
} catch (MessagingException e) {
throw new MailException("设置发信人:" + from + " 发生错误!" + e);
}
}

/**
* 设置接收人
* @param name String
* @param pass String
*/
public void setTo(String to) throws MailException {
if (to == null || to.length() <= 0)
throw new MailException("To is empty.");

log.debug("to: " + to);
try {
mimeMsg.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(to));
} catch (MessagingException e) {
throw new MailException("设置接收人: " + to + " 发生错误!", e);
}
}

/**
* 设置抄送人
* @param name String
* @param pass String
*/
public void setCopyTo(String copyTo) throws MailException {
if (copyTo == null || copyTo.length() <= 0)
return;

log.debug("copyTo: " + copyTo);
try {
mimeMsg.setRecipients(
Message.RecipientType.CC,
InternetAddress.parse(copyTo));
} catch (MessagingException e) {
throw new MailException("设置抄送人: " + copyTo + " 发生错误!", e);
}
}

/**
* 设置邮件主题
* @param mailSubject String
* @return boolean
*/
public void setSubject(String mailSubject, String charset)
throws MailException {
log.debug("mailSubject: " + mailSubject + "charset: " + charset);
try {
mimeMsg.setSubject(mailSubject, charset);
} catch (MessagingException e) {
throw new MailException("设置邮件主题发生错误!", e);
}
}

/**
* 设置邮件正文
* @param mailBody String
*/
public void setBody(String mailBody, String type) throws MailException {
log.debug("mailBody: " + mailBody + "type: " + type);
try {
BodyPart bp = new MimeBodyPart();
bp.setContent(mailBody, type);
mp.addBodyPart(bp);
} catch (MessagingException e) {
throw new MailException("设置邮件正文时发生错误!" + e);
}
}

/**
* 添加邮件附件
* @param name String
* @param pass String
*/
public void addFileAffix(String filename) throws MailException {
if (filename == null || filename.length() <= 0)
return;

try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setFileName(fileds.getName());
bp.setDataHandler(new DataHandler(fileds));
mp.addBodyPart(bp);
} catch (MessagingException e) {
throw new MailException("添加邮件附件:" + filename + "发生错误!" + e);
}
}

/**
* @param name String
* @param pass String
*/
public void send() throws MailException {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();

Transport transport = session.getTransport("smtp");
transport.connect(
(String) props.get("mail.smtp.host"),
userName,
password);
transport.sendMessage(
mimeMsg,
mimeMsg.getRecipients(Message.RecipientType.TO));

log.debug("邮件发送成功!");
transport.close();
} catch (MessagingException e) {
throw new MailException("邮件发送失败!", e);
}
}

/**
* Just do it as this
*/
public static void main(String[] args) {

String mailbody = "Test message!";

MailSender themail = new MailSender();

try {
ms.setBody("body", "text/html;charset=GBK");
themail.setBody(mailbody, "text/html;charset=GBK");
themail.setTo("xxx@xxx.com");
themail.setFrom("yyy@yyy.com");
themail.addFileAffix("c:\xxx.txt");
themail.send();
} catch (MailException e) {
e.printStackTrace();
}
}
}
sanyou98 2004-12-15
  • 打赏
  • 举报
回复
up
xiaoyaozgc 2004-12-15
  • 打赏
  • 举报
回复
编码方式改一下,改成GB2312,
前段时间我刚做了一个,
只可惜没带在身边,否则就可以给你了,
SEASKYLONG 2004-12-15
  • 打赏
  • 举报
回复
不行...老大.你们有没有做过类似的东西.就是在线发送邮件的.然后可以发送中文的就行了..如果可以给我提供一个吗?先谢谢了..E-mail:seaskylong@163.com

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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