版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/qingfeng812/article/details/8947855
package Test;
import java.net.MalformedURLException;
import java.net.URL;
import javax.mail.internet.*;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
public class Send3 {
public Send3(){}
// 需要的jar包6個:commons-email-1.2.jar commons-email-1.2-javadoc.jar
// commons-email-1.2-sources.jar mail.jar activation.jar,mail.jar
public static void main(String[] args) {
send();
}
// QQ郵箱對應的pop3和smtp伺服器位址為:pop3:pop.qq.com;
// smtp:smtp.qq.com,請參考
public static void send() {
try {
HtmlEmail email = new HtmlEmail();
email.setTLS(true);
email.setHostName("smtp.qq.com"); // 這裡是發送伺服器的名字
email.setAuthentication("
[email protected]", "郵箱密碼");//填寫自己的QQ郵箱和密碼,也就是發送方的郵箱
email.addTo("
[email protected]", "收件人"); // 接收方郵件位址和收件人
email.setFrom("
"); // 發送方
email.setSubject("Java發送郵件測試,十分神奇"); // 标題
email.setCharset("GB2312"); // 設定Charset
email.setMsg("這是一封Java程式發出的測試郵件,帶附件。"); // 内容
// embed the image and get the content id
URL url;
url = new URL("
http://img.baidu.com/img/image/ilogob.gif");
String cid = email.embed(url, "baidu logo");
email.setHtmlMsg("<html><a href='http://www.baidu.com'>baidu.com</a> <img src=\"cid:" + cid
+ "\"><br><P>haha</P></html>");
email.send();//發送
System.out.println("郵件發送成功");
} catch (EmailException e) {
e.printStackTrace();
}catch (MalformedURLException e) {
}
}
/*
* 有的郵箱需要開啟POP3/SMTP服務,例如本例的qq郵箱,在測試時彈出需要開啟此服務的 提示(qq郵箱發來的),進入郵箱的設定,開啟就行了
*/