程序源代码如下...
[code]
package test;import java.io.IOException;
import java.io.Writer;
import java.net.SocketException;
import java.util.Date;
import java.util.Properties;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;import org.apache.commons.io.IOUtils;
import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;public class Test {
private SMTPClient smtpClient = new SMTPClient();
private String host = "smtp.kingdee.com";
private int port = 25;
private int soTimeout = 60000; public Test() { } protected void sendMessage() {
System.out.println("send message...");
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.126.com");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
MyAuthenticator auth = new MyAuthenticator("[email protected]", "711570");
javax.mail.Session session =
javax.mail.Session.getDefaultInstance(props, auth);
MimeMessage msg =new MimeMessage(session);
try {
msg.setText("this is my first email...","gb2312");
msg.setContent("hello","text/html;charset=gb2312");
msg.saveChanges(); InternetAddress fromAddress = new
InternetAddress("[email protected]");
msg.setFrom(fromAddress);
msg.setRecipient(MimeMessage.RecipientType.TO, new
InternetAddress("[email protected]"));
System.out.println("1111111111111");
Writer writer = smtpClient.sendMessageData();
Transport transport = session.getTransport("smtp");
// transport.connect(host, user, password);
// transport.sendMessage(message, message.getAllRecipients());
// transport.close();
System.out.println("2222222222222222");
transport.send(msg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} protected void startSession() throws IOException {
disconnect();
try {
connect();
if (!smtpClient.login()) {
throw new IOException("failed HELO:"
+ smtpClient.getReplyString());
}
} catch (IOException e) {
disconnect();
throw e;
}
} protected void endSession() {
try {
disconnect();
} catch (Exception e) {
e.printStackTrace();
} finally {
disconnect();
}
} protected void disconnect() {
if (smtpClient.isConnected()) {
try {
smtpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
} protected void connect() throws SocketException, IOException {
smtpClient.setDefaultTimeout(60000);
smtpClient.connect(host, port);
smtpClient.setSoTimeout(soTimeout);
int reply = smtpClient.getReplyCode();
System.out.println("reply = " + reply);
if (!SMTPReply.isPositiveCompletion(reply)) {
throw new IOException("SMTP server refused connection: "
+ smtpClient.getReplyString());
}
} public static void main(String[] args) {
try {
Test test = new Test(); test.startSession();
test.sendMessage();
test.endSession(); } catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
[/code]运行时异常如下:
reply = 220
send message...
1111111111111
2222222222222222
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 type text/html;charset=gb2312
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at test.Test.sendMessage(Test.java:57)
at test.Test.main(Test.java:115)
困扰我很久了,网上说mail.jar 和 Activation.jar 没有导,但是我都导了
请问怎么回事呢?望达人指教?
[code]
package test;import java.io.IOException;
import java.io.Writer;
import java.net.SocketException;
import java.util.Date;
import java.util.Properties;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;import org.apache.commons.io.IOUtils;
import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;public class Test {
private SMTPClient smtpClient = new SMTPClient();
private String host = "smtp.kingdee.com";
private int port = 25;
private int soTimeout = 60000; public Test() { } protected void sendMessage() {
System.out.println("send message...");
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.126.com");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
MyAuthenticator auth = new MyAuthenticator("[email protected]", "711570");
javax.mail.Session session =
javax.mail.Session.getDefaultInstance(props, auth);
MimeMessage msg =new MimeMessage(session);
try {
msg.setText("this is my first email...","gb2312");
msg.setContent("hello","text/html;charset=gb2312");
msg.saveChanges(); InternetAddress fromAddress = new
InternetAddress("[email protected]");
msg.setFrom(fromAddress);
msg.setRecipient(MimeMessage.RecipientType.TO, new
InternetAddress("[email protected]"));
System.out.println("1111111111111");
Writer writer = smtpClient.sendMessageData();
Transport transport = session.getTransport("smtp");
// transport.connect(host, user, password);
// transport.sendMessage(message, message.getAllRecipients());
// transport.close();
System.out.println("2222222222222222");
transport.send(msg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} protected void startSession() throws IOException {
disconnect();
try {
connect();
if (!smtpClient.login()) {
throw new IOException("failed HELO:"
+ smtpClient.getReplyString());
}
} catch (IOException e) {
disconnect();
throw e;
}
} protected void endSession() {
try {
disconnect();
} catch (Exception e) {
e.printStackTrace();
} finally {
disconnect();
}
} protected void disconnect() {
if (smtpClient.isConnected()) {
try {
smtpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
} protected void connect() throws SocketException, IOException {
smtpClient.setDefaultTimeout(60000);
smtpClient.connect(host, port);
smtpClient.setSoTimeout(soTimeout);
int reply = smtpClient.getReplyCode();
System.out.println("reply = " + reply);
if (!SMTPReply.isPositiveCompletion(reply)) {
throw new IOException("SMTP server refused connection: "
+ smtpClient.getReplyString());
}
} public static void main(String[] args) {
try {
Test test = new Test(); test.startSession();
test.sendMessage();
test.endSession(); } catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
[/code]运行时异常如下:
reply = 220
send message...
1111111111111
2222222222222222
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 type text/html;charset=gb2312
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at test.Test.sendMessage(Test.java:57)
at test.Test.main(Test.java:115)
困扰我很久了,网上说mail.jar 和 Activation.jar 没有导,但是我都导了
请问怎么回事呢?望达人指教?
import java.util.Date;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.MessagingException;/**
* <p>Title: 给客户发邮件的类 </p>
*
* <p>Description: 包含给客户发邮件的方法 </p>
*
* <p>Copyright: Copyright (c) 2008</p>
*
* <p>Company: </p>
*
* <p>Date:2008-06-26</p>
*
* @author lqj
* @version 1.0
*/
public class SendMail {
String emailTo;
String subject;
String body;
public SendMail(String emailTo, String subject, String body) {
this.emailTo = emailTo;
this.subject = subject;
this.body = body;
} public boolean send() {
boolean b = false;
try {
Properties props = new Properties();
//注入4个数据到属性对象
props.put("mail.smtp.host", "127.0.0.1"); //建立会话对象 - 发送端与主机之间的连接
Session mailsession = Session.getDefaultInstance(props, null); //建立消息对象 -邮件
Message msg = new MimeMessage(mailsession);
//设置消息的属性- 发件地址,收件地址,消息主题,消息正文
msg.setFrom(new InternetAddress("[email protected]"));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(emailTo));
msg.setSentDate(new Date());
msg.setSubject(subject);
msg.setText(body);
//发送 -先连接,再发送,断开连接,完毕.
Transport.send(msg);
b = true;
} catch (MessagingException ex) {
ex.printStackTrace();
b = false;
}
return b;
}
}你发邮件的类是这样写的吗?
你照着对照一下!~!
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type text/html;charset=gb2312
是你的数据类型不支持
[code]package test;import java.io.IOException;
import java.io.Writer;
import java.net.SocketException;
import java.util.Date;
import java.util.Properties;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;import org.apache.commons.io.IOUtils;
import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;public class Test {
private SMTPClient smtpClient = new SMTPClient();
private String host = "smtp.kingdee.com";
private int port = 25;
private int soTimeout = 60000; public Test() { } protected void sendMessage() {
System.out.println("send message...");
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.126.com");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
MyAuthenticator auth = new MyAuthenticator("[email protected]", "711570");
javax.mail.Session session =
javax.mail.Session.getDefaultInstance(props, auth);
MimeMessage msg =new MimeMessage(session);
try {
msg.setText("this is my first email...","gb2312");
msg.setContent("hello","text/html;charset=gb2312");
msg.saveChanges(); InternetAddress fromAddress = new
InternetAddress("[email protected]");
msg.setFrom(fromAddress);
msg.setRecipient(MimeMessage.RecipientType.TO, new
InternetAddress("[email protected]"));
System.out.println("1111111111111");
Writer writer = smtpClient.sendMessageData();
Transport transport = session.getTransport("smtp");
// transport.connect(host, user, password);
// transport.sendMessage(message, message.getAllRecipients());
// transport.close();
System.out.println("2222222222222222");
transport.send(msg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} protected void startSession() throws IOException {
disconnect();
try {
connect();
if (!smtpClient.login()) {
throw new IOException("failed HELO:"
+ smtpClient.getReplyString());
}
} catch (IOException e) {
disconnect();
throw e;
}
} protected void endSession() {
try {
disconnect();
} catch (Exception e) {
e.printStackTrace();
} finally {
disconnect();
}
} protected void disconnect() {
if (smtpClient.isConnected()) {
try {
smtpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
} protected void connect() throws SocketException, IOException {
smtpClient.setDefaultTimeout(60000);
smtpClient.connect(host, port);
smtpClient.setSoTimeout(soTimeout);
int reply = smtpClient.getReplyCode();
System.out.println("reply = " + reply);
if (!SMTPReply.isPositiveCompletion(reply)) {
throw new IOException("SMTP server refused connection: "
+ smtpClient.getReplyString());
}
} public static void main(String[] args) {
try {
Test test = new Test(); test.startSession();
test.sendMessage();
test.endSession(); } catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
[/code]我看了,我字符编码设置了 能建立连接有一个 IOException 不知道怎么回事 昨天才开始用这个框架 比较急,请指教