Struggler7 2014-02-12 08:10
浏览 5184

javamail发送邮件报错,求指教。

前段时间写了一个简单的javamail发送邮件的功能,测试功能正常后就没管了。今天再次测试的时候,发现居然报错了。求大神指点指点。
package com.temp.test;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
@author xuyl@scoretk.com
@date 2014-2-11 上午06:53:10
*/
public class MailUtil {
static int port = 25;
static String server = "smtp.163.com";//邮件服务器mail.cpip.net.cn
static String from = "张三";//发送者,显示的发件人名字
static String user = "cqsiai@163.com";//发送者邮箱地址
static String password = "admin123";//密码
public static void sendEmail(String email, String subject, String body) throws UnsupportedEncodingException {
try {
Properties props = new Properties();
props.put("mail.smtp.host", server);
props.put("mail.smtp.port", String.valueOf(port));
props.put("mail.smtp.auth", "true");
Transport transport = null;
Session session = Session.getDefaultInstance(props, null);
transport = session.getTransport("smtp");
transport.connect(server, user, password);
MimeMessage msg = new MimeMessage(session);
msg.setSentDate(new Date());
InternetAddress fromAddress = new InternetAddress(user,from,"UTF-8");
msg.setFrom(fromAddress);
InternetAddress[] toAddress = new InternetAddress[1];
toAddress[0] = new InternetAddress(email);
msg.setRecipients(Message.RecipientType.TO, toAddress);
msg.setSubject(subject, "UTF-8");

msg.setText(body, "UTF-8");
msg.saveChanges();
transport.sendMessage(msg, msg.getAllRecipients());
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws UnsupportedEncodingException
{

        sendEmail("378675113@qq.com","邮件测试","hello");//收件人
        System.out.println("ok");
    }

}

报错内容:
javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type text/plain; charset=UTF-8
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1177)
at com.temp.test.MailUtil.sendEmail(MailUtil.java:45)
at com.temp.test.MailUtil.main(MailUtil.java:55)
Caused by: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type text/plain; charset=UTF-8
at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:841)
at javax.activation.DataHandler.writeTo(DataHandler.java:295)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1485)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1773)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1121)
... 2 more
ok

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型
    • ¥15 求学软件的前人们指明方向🥺
    • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
    • ¥20 双层网络上信息-疾病传播