当前位置: 技术问答>java相关
使用javaMail编写发送邮件的程序,报错如下:
来源: 互联网 发布时间:2015-09-03
本文导语: 编写一个Mail类,代码如下: public class Mail { //定义发件人、收件人、主题等 String to="jiao0608@0451.com"; String from="dulj@css.com.cn"; String host="211.157.248.3"; String filename=""; String subject="javamail test"; //用于保存发送附...
编写一个Mail类,代码如下:
public class Mail {
//定义发件人、收件人、主题等
String to="jiao0608@0451.com";
String from="dulj@css.com.cn";
String host="211.157.248.3";
String filename="";
String subject="javamail test";
//用于保存发送附件的文件名的集合
Vector file = new Vector();
//做一个可以传发件人等参数的构造
public Mail (String to,String from,String smtpServer,String subject)
{
//初始化发件人、收件人、主题等
this.to=to;
this.from=from;
this.host=smtpServer;
this.subject=subject;
}
//该方法用于收集附件名
public void attachfile(String fname)
{
file.addElement(fname);
}
//开始发送信件的方法
public boolean startSend()
{
//创建Properties对象
Properties props = System.getProperties();
//创建信件服务器
props.put("mail.smtp.host", host);
//得到默认的对话对象
Session session=Session.getDefaultInstance(props, null);
try
{
//创建一个消息,并初始化该消息的各项元素
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
//后面的BodyPart将加入到此处创建的Multipart中
Multipart mp = new MimeMultipart();
//利用枚举器方便的遍历集合
Enumeration efile=file.elements();
//检查序列中是否还有更多的对象
while(efile.hasMoreElements())
{
MimeBodyPart mbp=new MimeBodyPart();
//选择出每一个附件名
filename=efile.nextElement().toString();
System.out.println(filename);
//得到数据源
FileDataSource fds=new FileDataSource(filename);
//得到附件本身并至入BodyPart
mbp.setDataHandler(new DataHandler(fds));
//得到文件名同样至入BodyPart
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
System.out.println(mp);
}
//移走集合中的所有元素
file.removeAllElements();
//Multipart加入到信件
msg.setContent(mp);
//设置信件头的发送日期
msg.setSentDate(new Date());
//发送信件
Transport.send(msg);
} catch (MessagingException mex)
{
mex.printStackTrace();
Exception ex = null;
if ((ex=mex.getNextException())!=null)
{
ex.printStackTrace();
}
return false;
}
return true;
}
public static void send(String smtpServer, String to, String from, String subject, String body)
{
try{
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.RecipientType.CC
// ,InternetAddress.parse(cc, false));
// -- Set the subject and body text --
msg.setSubject(subject);
msg.setText(body);
// -- Set some other header information --
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
// -- Send the message --
Transport.send(msg);
System.out.println("Message sent OK.");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
编写一个test类对Mail类进行测试:
public class test
{
public static void main(String[] args)
{
String to="jiao0608@0451.com";
String from="jiao0608@0451.com";
String smtpServer="mail.0451.com";
String filename="";
String subject="javamail test";
String body = "wo shi zhu!";
Mail.send(smtpServer, to, from,subject, body);
/*Mail mail = new Mail(to,from,smtpServer,subject);
mail.attachfile("Mail.java");
boolean b = mail.startSend () ;
System.out.println(b);
*/
System.exit(0);
}
}
若调用Mail.send方法,则报错如下:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME ty
pe text/plain; charset=us-ascii
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at com.css.mail.Mail.send(Mail.java:120)
at test.main(test.java:21)
将Mail.send方法屏蔽,调用startSend方法,则报错如下:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME ty
pe multipart/mixed; boundary="----=_Part_0_2951274.1031999878766"
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at com.css.mail.Mail.startSend(Mail.java:83)
at test.main(test.java:24)
javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME ty
pe multipart/mixed; boundary="----=_Part_0_2951274.1031999878766"
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:356)
at javax.mail.Transport.send0(Transport.java:163)
at javax.mail.Transport.send(Transport.java:80)
at com.css.mail.Mail.startSend(Mail.java:83)
at test.main(test.java:24)
我检查了一下,不知道程序中到底哪里出了问题,还请大虾指教!
public class Mail {
//定义发件人、收件人、主题等
String to="jiao0608@0451.com";
String from="dulj@css.com.cn";
String host="211.157.248.3";
String filename="";
String subject="javamail test";
//用于保存发送附件的文件名的集合
Vector file = new Vector();
//做一个可以传发件人等参数的构造
public Mail (String to,String from,String smtpServer,String subject)
{
//初始化发件人、收件人、主题等
this.to=to;
this.from=from;
this.host=smtpServer;
this.subject=subject;
}
//该方法用于收集附件名
public void attachfile(String fname)
{
file.addElement(fname);
}
//开始发送信件的方法
public boolean startSend()
{
//创建Properties对象
Properties props = System.getProperties();
//创建信件服务器
props.put("mail.smtp.host", host);
//得到默认的对话对象
Session session=Session.getDefaultInstance(props, null);
try
{
//创建一个消息,并初始化该消息的各项元素
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
//后面的BodyPart将加入到此处创建的Multipart中
Multipart mp = new MimeMultipart();
//利用枚举器方便的遍历集合
Enumeration efile=file.elements();
//检查序列中是否还有更多的对象
while(efile.hasMoreElements())
{
MimeBodyPart mbp=new MimeBodyPart();
//选择出每一个附件名
filename=efile.nextElement().toString();
System.out.println(filename);
//得到数据源
FileDataSource fds=new FileDataSource(filename);
//得到附件本身并至入BodyPart
mbp.setDataHandler(new DataHandler(fds));
//得到文件名同样至入BodyPart
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
System.out.println(mp);
}
//移走集合中的所有元素
file.removeAllElements();
//Multipart加入到信件
msg.setContent(mp);
//设置信件头的发送日期
msg.setSentDate(new Date());
//发送信件
Transport.send(msg);
} catch (MessagingException mex)
{
mex.printStackTrace();
Exception ex = null;
if ((ex=mex.getNextException())!=null)
{
ex.printStackTrace();
}
return false;
}
return true;
}
public static void send(String smtpServer, String to, String from, String subject, String body)
{
try{
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put("mail.smtp.host", smtpServer);
Session session = Session.getDefaultInstance(props, null);
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
// -- We could include CC recipients too --
// if (cc != null)
// msg.setRecipients(Message.RecipientType.CC
// ,InternetAddress.parse(cc, false));
// -- Set the subject and body text --
msg.setSubject(subject);
msg.setText(body);
// -- Set some other header information --
msg.setHeader("X-Mailer", "LOTONtechEmail");
msg.setSentDate(new Date());
// -- Send the message --
Transport.send(msg);
System.out.println("Message sent OK.");
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
编写一个test类对Mail类进行测试:
public class test
{
public static void main(String[] args)
{
String to="jiao0608@0451.com";
String from="jiao0608@0451.com";
String smtpServer="mail.0451.com";
String filename="";
String subject="javamail test";
String body = "wo shi zhu!";
Mail.send(smtpServer, to, from,subject, body);
/*Mail mail = new Mail(to,from,smtpServer,subject);
mail.attachfile("Mail.java");
boolean b = mail.startSend () ;
System.out.println(b);
*/
System.exit(0);
}
}
若调用Mail.send方法,则报错如下:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME ty
pe text/plain; charset=us-ascii
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at com.css.mail.Mail.send(Mail.java:120)
at test.main(test.java:21)
将Mail.send方法屏蔽,调用startSend方法,则报错如下:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME ty
pe multipart/mixed; boundary="----=_Part_0_2951274.1031999878766"
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at com.css.mail.Mail.startSend(Mail.java:83)
at test.main(test.java:24)
javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME ty
pe multipart/mixed; boundary="----=_Part_0_2951274.1031999878766"
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:356)
at javax.mail.Transport.send0(Transport.java:163)
at javax.mail.Transport.send(Transport.java:80)
at com.css.mail.Mail.startSend(Mail.java:83)
at test.main(test.java:24)
我检查了一下,不知道程序中到底哪里出了问题,还请大虾指教!
|
我认为代码没有问题
你的classpath里应该包括activation.jar,mail.jar吧
你的classpath还有些什么?
你的classpath里应该包括activation.jar,mail.jar吧
你的classpath还有些什么?
|
好象是因为你发的是复合邮件,而你的邮件服务器不支持,我不是很肯定。
javax.activation.UnsupportedDataTypeException: no object DCH for MIME ty
pe multipart/mixed; boundary="----=_Part_0_2951274.1031999878766"
javax.activation.UnsupportedDataTypeException: no object DCH for MIME ty
pe multipart/mixed; boundary="----=_Part_0_2951274.1031999878766"