jsp发送E-Mail需要activation.jar 和javamail.jar两个.jar包,将其放至项目的lib目录下!
以下是源代码及在开发过程中遇到的问题和解决办法:
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailService {
// 默认主机;邮箱发送;下面的用户名和密码就是你sohu邮箱的username/pwd
private static final String host = "smtp.sohu.com";
// 是谁发的信息
private static final String fromEmail = "nihao919833@sohu.com";
public static void main(String[] args) {
EmailService email = new EmailService();
email.sendMess(null, "libaogang.1990@gmail.com", null, "标题", "内容");
}
/**
* 客户端调用用于发送信息
*
* @param emailToOne:第一个要收信息的邮箱
* @param emailTwo:第二个要收信息的邮箱
* @param emailThree:第三个要收信息的邮箱
* @param subject:标题
* @param body:内容
*/
public void sendMess(String emailToOne, String emailTwo, String emailThree,
String subject, String body) {
send(emailToOne, emailTwo, emailThree, subject, body);
}
private void send(String emailToOne, String emailTwo, String emailThree,
String subject, String body) {
Properties props = null;// 属性类
Session mailsession = null;// 在每一次发送邮件信息是要创建的一个session
Message msg = null;// 用于存储信息类
try {
props = new Properties();
props.put("mail.smtp.host", host);// 所使用的转移邮箱
props.put("mail.smtp.auth", "true");// 必需的
mailsession = Session.getDefaultInstance(props, SmtpAuth.getInstance());
// true:显示运行时信息显示结果;false:不显示执行的结果信息
mailsession.setDebug(false);
/**
* Message.RecipientType.TO
*
* Message.RecipientType.CC
*
* Message.RecipientType.BCC
*/
// 使用每次session创建一个存储信息的类对象、说明此信息只在当前session范围内使用
msg = new MimeMessage(mailsession);
msg.setFrom(new InternetAddress(fromEmail));
if (null != emailToOne || !emailToOne.equals("")) {
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailToOne));
} else if (null != emailTwo || !emailTwo.equals("")) {
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(emailTwo));
} else if (null != emailThree || !emailThree.equals("")) {
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(emailThree));
}
msg.setSentDate(new Date());// 发送的时间既当前时间
msg.setSubject(subject);// 相当于标题
msg.setText(body);// 设置内容
msg.saveChanges();// 保存修改项
Transport.send(msg);// 发送信息
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
/**
* 用于保存输入的用户名/密码
*
* @author Administrator
*
*/
class SmtpAuth extends javax.mail.Authenticator {
private String user;
private String password;
private static SmtpAuth smtpAuth = null;
// 返回当前类的对象;
public static SmtpAuth getInstance() {
if (null == smtpAuth) {
smtpAuth = new SmtpAuth();
}
return smtpAuth;
}
// 使用默认构造函数使用默认制定用户名和密码
public SmtpAuth() {
user = "nihao919833";
password = "**********";// 将*改为你的真正密码
}
public void setUserinfo(String getuser, String getpassword) {
user = getuser;
password = getpassword;
}
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(user, password);
}
}
1、NoSuchProviderException:ClassPatch 没有包含javamailAPI,放到jre/lib/后可解决
2、Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataSource 没有下载jaf,将下载的activation.jar放入lib目录下解决
3、com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required,smtp8,wKjADxuAyCAfmnZE8BwtIA==.32705S2
这样的异常,要求你必须进行授权校验,它的目的就是阻止他人任意乱发邮件,也算是为了减少垃圾邮件的出现吧。这时候,我们就要使用
props.put("mail.smtp.auth", "true");
MyAuthenticator myauth = new MyAuthenticator("你自己的电子信箱", "你自己的信箱密码");
Session session = Session.getDefaultInstance(props, myauth);
4、javax.activation.UnsupportedDataTypeException: no object DCH for MIME type xxx/xxxx javax.mail.MessagingException: IOException while sending message:使用的是Javamail1.2,下载javamail1.4则问题解决。
5、port:25这样的异常 如果用了MACFEE杀毒软件就会有,你要对其进行设置,让它允许javaw.exe
6、What causes an “javax.activation.UnsupportedDataTypeException: no object DCH for MIME type xxx/xxxx javax.mail.MessagingException: IOException while sending message;” to be sent and how do I fix this? [This happens for known MIME types like text/htm
用这个办法可解决 The file or resource named META-INF/mailcap.default (usually found only in the activation.jar file).在生成的Jar文件中加入了 META-INF/mailcap.default(从activation.jar中meta-inf中获取)
#
# This is a very simple 'mailcap' file
#
image/gif;; x-java-view=com.sun.activation.viewers.ImageViewer
image/jpeg;; x-java-view=com.sun.activation.viewers.ImageViewer
text/*;; x-java-view=com.sun.activation.viewers.TextViewer
text/*;; x-java-edit=com.sun.activation.viewers.TextEditor
text/html;; x-java-content-handler=com.sun.mail.handlers.text_html
text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml
text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain
multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed
message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822