javax.email 发送邮件 javaEmail ,java 邮件
首先导入mail的jar包
然后代码如下
package aaa;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class MailClient {
// 用户名
protected static String username = "1419052183@qq.com";
// 密码
protected static String passwrod = "axdjxsinansijhbh";
// 服务器地址
protected static String hostIp = "smtp.qq.com";
// 需要验证
protected static String auth = "true";
// 端口号 主
// ssl 端口号
protected String sslport = "465";
public static void sendEmail1() {
/**
* 1连接邮件服务器 2创建邮件对象 3邮件发送
*/
Properties pros = System.getProperties();
// 你要链接那个邮箱服务器
pros.put("mail.smtp.host", hostIp);
// 你要链接发送的的端口号
pros.put("mail.smtp.port", "25");
// 是否启用验证
pros.put("mail.smtp.auth", auth);
// ssl验证
pros.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
// ssl端口号
pros.put("mail.smtp.socketFactory.port", "465");
Authenticator auth = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// TODO Auto-generated method stub
return new PasswordAuthentication(username, passwrod);
}
};
Session session = Session.getInstance(pros, auth);
session.setDebug(true);
// 2創建郵件對象
try {
Message message = new MimeMessage(session);
// 发件人
message.setFrom(new InternetAddress("1419052183@qq.com"));
message.setContent("早饭早饭早饭早饭早饭早饭<a href='www.wulilang.cn'>点击我</a>",
"text/html;charset=utf-8");
message.addRecipient(RecipientType.TO, new InternetAddress(
"977280996@qq.com"));
message.setSentDate(new Date());
message.setSubject("主题");
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
sendEmail1();
}
}