JavaMail问题,望解决,在线等,急!!!!!!!!

zhangleibbq 2003-11-27 11:08:19
我的发邮件的代码:
package classes;

/**用JavaMail发送邮件的类*/
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
import java.util.Date;
import java.lang.*;
import java.io.*;
import java.net.*;
import javax.activation.DataHandler;


public class MailSender
{
private MailSender()
{
}
/**发送邮件的方法*/
public void sendMail(String toadd,String subject,String text)
{
try{
InternetAddress fromaddress = new InternetAddress("zlbbq47054370@sina.com");
String fromsmtp = "smtp.sina.com.cn";
InternetAddress toaddress = new InternetAddress(toadd);
SmtpAuthenticator st = new SmtpAuthenticator();
Properties pt = new Properties();
pt.put("mail.transport.protocol", "smtp");
pt.put("mail.smtp.auth","true");
pt.put("mail.smtp.host",fromsmtp);
Session session = Session.getInstance(pt,st);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(fromaddress);
msg.setRecipient(Message.RecipientType.TO,toaddress);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setContent(text,"text/html;charset=gb2312");
msg.setText(text);
msg.saveChanges();
Transport.send(msg);
}catch(Exception e){System.out.println("Error: "+e.getMessage());return;}
System.out.println("Send mail successfully.");
}
/**到服务器进行验证的类*/
class SmtpAuthenticator extends javax.mail.Authenticator
{
//SMTP服务器身份验证
protected javax.mail.PasswordAuthentication getPasswordAuthentication()
{
return new javax.mail.PasswordAuthentication("zlbbq47054370","5626154");
}
}
public static void main(String args[])
{
MailSender ms = new MailSender();
ms.sendMail("zl_software@126.com","Test","http://www.sina.com");
}
}





编译通过,但是发邮件的时候会出现异常:javax.activation.UnsupportedDataTypeException: no object DCH for MIME type text/html,请指教哪步错了.
...全文
78 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
zhangleibbq 2003-11-27
to:leshui(大象无形)(有物混成,先天地生)
不可能没有包的,如果没有包的话,首先,编译就不能通过.这是什么情况,会不会是smtp服务器的问题?
回复
littlecong 2003-11-27
发html类型的邮件,请参考javaMail自带的一个例子,具体我记不清了
回复
leshui 2003-11-27
应该没问题的
是不是有的包没有
要两个包 javamail和jaf
回复
zhangleibbq 2003-11-27
leshui(大象无形)(有物混成,先天地生),有问题,代码还是一样的代码,我发不出去,
Exception 还是一样的Exception,多了一点:boundary="----=_Part_0_31XXXXXXXXXXXXXXX",
多谢指出是哪个地方的原因.
回复
leshui 2003-11-27
http://www.csdn.net/develop/Read_Article.asp?Id=14929
回复
tyfun 2003-11-27
下面是我很早已前写的
编译通过
运行正常


/*
*Author:tyfun
*DateTime:2002.01.09
*Package:com.westarsoft.function
*/

package com.westarsoft.function;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendMail {
private String SMTPServer = new String();
private String from = new String();
private String subject = new String();
private String content = new String();
private String user = new String();
private String password = new String();
private String fileAttachment = new String();
public String getSMTPServer() {
return SMTPServer;
}
public void setSMTPServer(String SMTPServer) {
this.SMTPServer = SMTPServer;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFileAttachment() {
return fileAttachment;
}
public void setFileAttachment(String fileAttachment) {
this.fileAttachment = fileAttachment;
}

public void sendMailTo(String to,String cc,String bcc) {
try {
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;

sendMailSession = Session.getInstance(props, null);
props.put("mail.smtp.host", SMTPServer);
MimeMessage newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(from));
newMessage.setSubject(subject);
newMessage.setSentDate(new Date());
/*
StringTokenizer tokenTO = new StringTokenizer(to, ",");
InternetAddress[] addrArrTO = new InternetAddress[tokenTO.countTokens()];
int i = 0;
while(tokenTO.hasMoreTokens()) {
addrArrTO[i] = new InternetAddress(tokenTO.nextToken().toString());
i++;
}
*/
transport = sendMailSession.getTransport("smtp");
transport.connect(SMTPServer, user, password);
//newMessage.setRecipients(Message.RecipientType.TO, addrArrTO);
newMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
newMessage.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
newMessage.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));

MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(content);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);
newMessage.setContent(multipart);

transport.send(newMessage);
}
catch(Exception e) {
System.out.println(e);
}
}
}





<%
if(request.getMethod().equals("POST")) {
SendMail mail = new SendMail();
mail.setSMTPServer("200.1.1.157");
mail.setUser("lint");
mail.setPassword("xxxxx");
mail.setFrom(request.getParameter("from"));
mail.setSubject(request.getParameter("subject"));
mail.setContent(request.getParameter("content"));
mail.setFileAttachment(request.getParameter("filename"));
mail.sendMailTo(request.getParameter("to"),request.getParameter("cc"),request.getParameter("bcc"));
}
%>

回复
zhangleibbq 2003-11-27
我找到错误原因了,是DCH(DataContentHandler)的原因,Transport在发送邮件的时候,先要将其转换成MIME类型,它将调用javax.mail.activation.CommandMap,具体代码如下:
javax.mail.activation.CommandMap cmp = javax.mail.activation.CommandMap.getDefaultCommandMap();
DataConentHandler DCH = cmp.createDataContentHandler(contenttype);//contenttype="text/palin","text/html","multipart/*"...
if(DCH==null)
{
throw new UnSupprotedDataTypeException("no object DCH for MIME type"+contenttype);
}

CommandMap会在META-INF目录下找一个叫mailcap的文件,我的系统没有找到,所以DCH对角为空,呵呵呵.还是谢谢各位.
回复
leshui 2003-11-27
有可能是
换一个试试
163的就可以
回复
相关推荐
发帖
Java EE
创建于2007-09-28

6.6w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2003-11-27 11:08
社区公告
暂无公告