1. 安装JavaMail API 和Java Activation Framework (JAF)
- 可以从 Java 网站下载最新版本的 JavaMail,打开网页右侧有个 Downloads 链接,点击它下载。
- 可以从 Java 网站下载最新版本的 JAF(版本 1.1.1)。
下载并解压缩这些文件,有用的文件是 mail.jar 和 activation.jar,接下来又几种操作,选一种就可以。
a. 将mail.jar 和 activation.jar文件添加到系统 CLASSPATH 中:D:\Java\java_mail\activation.jar;D:\Java\java_mail\mail.jar
b. 将mail.jar 和 activation.jar文件添加到java安装包下的\lib\ext\下,我的是D:\JavaInstall\lib\ext
前两种方法eclipse和Netbeans都可以。
c. 打开eclipse, Project\Properties\Java build path\libraries\add external JREs, 导入mail.jar和activation.jar。
(可以打开看JRE System Library,里面的路径其实就是b.中我放这两个文件的路径)
之后需要重启eclipse。我选用的是第二种方法。2.打开QQ邮箱的SMTP服务
QQ邮箱对于一般的用户都是默认关闭SMTP服务的。
打开QQ邮箱,点击设置
点击帐户
找到SMTP服务的选项,可以看到此处默认是关闭的,点击开启,然后腾讯会进行一些身份验证,身份验证通过以后,腾讯会给出一个用于使用SMTP的16位授权码,授权码作为密码。
QQ邮箱 POP3 和 SMTP 服务器地址设置如下:
邮箱 | POP3服务器(端口995) | SMTP服务器(端口465或587) |
---|---|---|
qq.com | pop.qq.com | smtp.qq.com |
// 关于QQ邮箱,还要设置SSL加密,加上以下代码即可 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); props.put("mail.smtp.ssl.enable", "true"); props.put("mail.smtp.ssl.socketFactory", sf);
3.程序
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;
/**
* JavaMail发送邮件:前提是QQ邮箱里帐号设置要开启POP3/SMTP协议
*/
public class Test {
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
// 开启debug调试,以便在控制台查看
prop.setProperty("mail.debug", "true");
// 设置邮件服务器主机名
prop.setProperty("mail.host", "smtp.qq.com");
// 发送服务器需要身份验证
prop.setProperty("mail.smtp.auth", "true");
// 发送邮件协议名称
prop.setProperty("mail.transport.protocol", "smtp");
// 开启SSL加密,否则会失败
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
// 创建session
Session session = Session.getInstance(prop);
// 通过session得到transport对象
Transport ts = session.getTransport();
// 连接邮件服务器:邮箱类型,帐号,授权码代替密码(更安全)
ts.connect("smtp.qq.com", "867339526@qq.com", "dxhvjroajbnzbfjb");//后面的字符是授权码
// 创建邮件
Message message = createSimpleMail(session);
// 发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createSimpleMail
* @Description: 创建一封只包含文本的邮件
*/
public static MimeMessage createSimpleMail(Session session)
throws Exception {
// 创建邮件对象
MimeMessage message = new MimeMessage(session);
// 指明邮件的发件人
message.setFrom(new InternetAddress("867339526@qq.com"));
// 指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
message.setRecipient(Message.RecipientType.TO, new InternetAddress("867339526@qq.com"));
// 邮件的标题
message.setSubject("JavaMail测试");
// 邮件的文本内容
message.setContent("JavaMail发送邮件成功!", "text/html;charset=UTF-8");
// 返回创建好的邮件对象
return message;
}
}
4. QQ邮箱发送简单文字
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;
public class SendEmail
{
public static void main(String [] args) throws GeneralSecurityException
{
// 收件人电子邮箱
String to = "XXXXX@qq.com";
// 发件人电子邮箱
String from = "XXXXXX@qq.com";
// 指定发送邮件的主机为 smtp.qq.com
String host = "smtp.qq.com"; //QQ 邮件服务器
// 获取系统属性
Properties properties = System.getProperties();
// 设置邮件服务器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
// 获取默认session对象
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("429240967@qq.com", "授权的 QQ 邮箱密码"); //发件人邮件用户名、密码
}
});
try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(from));
// Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: 头部头字段
message.setSubject("This is the Subject Line!");
// 设置消息体
message.setText("This is actual message");
// 发送消息
Transport.send(message);
System.out.println("Sent message successfully....from runoob.com");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
import java.security.GeneralSecurityException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;
public class SendEmail
{
public static void main(String [] args) throws GeneralSecurityException
{
// 收件人电子邮箱
String to = "XXXXX@qq.com";
// 发件人电子邮箱
String from = "XXXXXX@qq.com";
// 指定发送邮件的主机为 smtp.qq.com
String host = "smtp.qq.com"; //QQ 邮件服务器
// 获取系统属性
Properties properties = System.getProperties();
// 设置邮件服务器
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
// 获取默认session对象
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("429240967@qq.com", "授权的 QQ 邮箱密码"); //发件人邮件用户名、密码
}
});
try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(from));
// Set To: 头部头字段
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: 头部头字段
message.setSubject("This is the Subject Line!");
// 设置消息体
message.setText("This is actual message");
// 发送消息
Transport.send(message);
System.out.println("Sent message successfully....from runoob.com");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
5. qq邮箱发送HTML
// 设置消息体
message.setText("This is actual message");
换成:
// 发送 HTML 消息, 可以插入html标签
message.setContent("<h1>This is actual message</h1>",
"text/html" );
6. qq邮箱发送邮件到多人
如果你想发送一封e-mail给多个收件人,那么使用下面的方法来指定多个收件人ID:
void addRecipients(Message.RecipientType type,
Address[] addresses)
throws MessagingException
下面是对于参数的描述:
-
type:要被设置为 TO, CC 或者 BCC,这里 CC 代表抄送、BCC 代表秘密抄送。举例:Message.RecipientType.TO
-
addresses: 这是 email ID 的数组。在指定电子邮件 ID 时,你将需要使用 InternetAddress() 方法。
也可以用setRecipients(),可以在发送内容前进行设置,如下: // 多个收件人
message.setRecipients(RecipientType.TO, InternetAddress.parse("86733526@qq.com,87035741@qq.com"));
// 抄送人
message.setRecipient(RecipientType.CC, new InternetAddress("aaaaaa@163.com"));
// 暗送人
message.setRecipient(RecipientType.BCC, new InternetAddress("bbbbbbb@163.com"));
// HTML内容
message.setContent("<span style='color:red'>中文呵呵</span>","text/html;charset=utf-8");
// 多个收件人
message.setRecipients(RecipientType.TO, InternetAddress.parse("86733526@qq.com,87035741@qq.com"));
// 抄送人
message.setRecipient(RecipientType.CC, new InternetAddress("aaaaaa@163.com"));
// 暗送人
message.setRecipient(RecipientType.BCC, new InternetAddress("bbbbbbb@163.com"));
// HTML内容
message.setContent("<span style='color:red'>中文呵呵</span>","text/html;charset=utf-8");
需要在程序头上加:
import javax.mail.Message.RecipientType;
7.发送带有附件的邮件
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 消息
messageBodyPart.setText("This is message body");
// 创建多重消息
Multipart multipart = new MimeMultipart();
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
// 附件部分
messageBodyPart = new MimeBodyPart();
String filename = "E:/file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// 发送完整消息
message.setContent(multipart );
参考:http://www.runoob.com/java/java-sending-email.html
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 消息
messageBodyPart.setText("This is message body");
// 创建多重消息
Multipart multipart = new MimeMultipart();
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
// 附件部分
messageBodyPart = new MimeBodyPart();
String filename = "E:/file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// 发送完整消息
message.setContent(multipart );