今天主要研究了一下关于java发送邮件给其他邮箱,不说废话,上代码,代码从以为大神那里看的,附上地址:http://www.cnblogs.com/codeplus/archive/2011/10/30/2229391.html 大神有几个类没有给,我这里都给贴上:
package com.jtzh.core.model;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* * @author 作者 E-mail:
*
* @date 创建时间:2015年10月22日 上午10:05:47
* @version 1.0
* @parameter
* @since
* @return
*/
public class JavaEmail extends Authenticator {
private String username;
private String password;
public JavaEmail(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
package com.jtzh.app.util;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import com.jtzh.core.model.JavaEmail;
import com.jtzh.core.model.SimpleMail;
/**
* * @author 作者 E-mail:
*
* @date 创建时间:2015年10月22日 上午10:16:07
* @version 1.0
* @parameter
* @since
* @return
*/
public class SimpleMailSender {
private final transient Properties props = System.getProperties();
private transient JavaEmail javaEmail;
private transient Session session;
// 初始化邮件发送器
public SimpleMailSender(final String smtpHostName, final String username,
final String password) {
init(username, password, smtpHostName);
}
// 初始化邮件发送器
public SimpleMailSender(final String username, final String password) {
final String smtpHostName = "smtp." + username.split("@")[1];
System.out.println(smtpHostName);
init(username, password, smtpHostName);
}
// 初始化
private void init(String username, String password, String smtpHostName) {
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtpHostName);
// 验证
javaEmail = new JavaEmail(username, password);
// 创建session
session = Session.getInstance(props, javaEmail);
}
public void send(String recipient, String subject, Object content)
throws Exception {
// 创建mime类型邮件
final MimeMessage message = new MimeMessage(session);
// 设置发件人
message.setFrom(new InternetAddress(javaEmail.getUsername()));
// 设置收件人
message.setRecipient(RecipientType.TO, new InternetAddress(recipient));
// 设置主题
message.setSubject(subject);
// 设置邮件内容
message.setContent(content.toString(), "text/html;charset=utf-8");
// 发送
Transport.send(message);
}
// 发送邮件
public void send(String recipient, SimpleMail mail) throws Exception {
send(recipient, mail.getSubject(), mail.getContent());
}
}
package com.jtzh.app.util;
/**
* * @author 作者 E-mail:
*
* @date 创建时间:2015年10月22日 上午10:46:30
* @version 1.0
* @parameter
* @since
* @return
*/
public class MailSenderFactory {
private static SimpleMailSender serviceSms = null;
/**
* 获取邮箱
*
* @param type
* 邮箱类型
* @return 符合类型的邮箱
*/
public static SimpleMailSender getSender() {
if (serviceSms == null) {
serviceSms = new SimpleMailSender("xxx", "xxxxx");
}
return serviceSms;
}
}
package com.jtzh.core.model;
/**
* * @author 作者 E-mail:
*
* @date 创建时间:2015年10月22日 上午10:39:34
* @version 1.0
* @parameter
* @since
* @return
*/
public class SimpleMail {
private String content;
private String subject;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public SimpleMail(String subject, String content) {
this.subject = subject;
this.content = content;
}
}
我是用qq邮箱进行测试的,结果报错:Exception in thread "main" javax.mail.AuthenticationFailedException: 535 。。。。
这是因为你的qq邮箱还没有开启smtp的协议,去qq邮箱开启:http://jingyan.baidu.com/article/0f5fb099dffe7c6d8334ea31.html这里有详细步骤。如果有不明白的可以问我:QQ:504183224欢迎一起讨论开发中所遇到的问题