public static void main(String[] args) throws Exception {
final String user = "779554589";
final String password = "";
String fromAddress = "
779554589@qq.com";
String toAddress = "
loadfate@163.com";
String subject = "郵件測試主題";
String content = "這是一個測試郵件<b>哈哈</b>";
//配置參數
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.qq.com");
// 方法一:使用transport對象發送郵件
{
//通過參數生成會話
Session session = Session.getInstance(props);
//啟用調試模式
session.setDebug(true);
//創建一封郵件,并設置信息
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
message.setSubject(subject);
message.setText(content);
//創建傳輸
Transport transport = session.getTransport();
//連接smtp服務器
transport.connect(user, password);
//發送
transport.sendMessage(message, new InternetAddress[] { new InternetAddress(toAddress) });
transport.close();
}
// 方法二:使用Transport類靜態方法發送郵件
{
//生成Session時以獲取授權連接
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
session.setDebug(true);
//創建一封郵件,并設置信息
Message message = new MimeMessage(session);
message.setSubject(subject);
message.setFrom(new InternetAddress(fromAddress));
message.setRecipient(RecipientType.TO, new InternetAddress(toAddress));
message.setContent(content, "text/html;charset=utf-8");
//直接發送,message通過已經授權的Session生成
Transport.send(message);
}
}
public class ApacheMailTest {
// smtp服務器
private String hostName = "smtp.qq.com";
// 帳號與密碼
private String userName = "779554589";
private String password = "這是個秘密";
// 發件人
private String fromAddress = "
779554589@qq.com";
// 發件人姓名
private String fromName = "loadfate";
public static void main(String[] args) throws Exception {
// 收件人與收件人名字
String toAddress = "
loadfate@163.com";
String toName = "loadfate";
ApacheMailTest test = new ApacheMailTest();
// 所有的異常都為處理,方便瀏覽
test.sendSimpleEmail(toAddress, toName);
test.sendHtmlEmail(toAddress, toName);
test.sendMultiPartEmail(toAddress, toName);
System.out.println("發送完成");
}
// 發送簡單郵件,類似一條信息
public void sendSimpleEmail(String toAddress, String toName) throws Exception {
SimpleEmail email = new SimpleEmail();
email.setHostName(hostName);// 設置smtp服務器
email.setAuthentication(userName, password);// 設置授權信息
email.setCharset("utf-8");
email.setFrom(fromAddress, fromName, "utf-8");// 設置發件人信息
email.addTo(toAddress, toName, "utf-8");// 設置收件人信息
email.setSubject("測試主題");// 設置主題
email.setMsg("這是一個簡單的測試!");// 設置郵件內容
email.send();// 發送郵件
}
// 發送Html內容的郵件
public void sendHtmlEmail(String toAddress, String toName) throws Exception {
HtmlEmail email = new HtmlEmail();
email.setHostName(hostName);
email.setAuthentication(userName, password);
email.setCharset("utf-8");
email.addTo(toAddress, toName, "utf-8");
email.setFrom(fromAddress, fromName, "utf-8");
email.setSubject("這是一個html郵件");
// 設置html內容,實際使用時可以從文本讀入寫好的html代碼
email.setHtmlMsg("<div style='width:100px;height:200px;'>a</div>");
email.send();
}
// 發送復雜的郵件,包含附件等
public void sendMultiPartEmail(String toAddress, String toName) throws Exception {
MultiPartEmail email = null;
email = new MultiPartEmail();
email.setHostName(hostName);
email.setAuthentication(userName, password);
email.setCharset("utf-8");
email.addTo(toAddress, toName, "utf-8");
email.setFrom(fromAddress, fromName, "utf-8");
email.setSubject("這是有附件的郵件");
email.setMsg("<a href='#'>測試內容</a>");
// 為郵件添加附加內容
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("D://郵件.txt");// 本地文件
// attachment.setURL(new URL("
http://xxx/a.gif"));//遠程文件
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("描述信息");
// 設置附件顯示名字,必須要編碼,不然中文會亂碼
attachment.setName(MimeUtility.encodeText("郵件.txt"));
// 將附件添加到郵件中
email.attach(attachment);
email.send();
}
}