java 發送郵件

來源:互聯網
上載者:User
import java.util.ArrayList;import java.util.Date;import java.util.List;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;public class SendMail {public static String SMTP_HOST = "smtp.126.com";public static String SMTP_PORT = "25";public static String SMTP_PROTOCOL = "SMTP";public static String SMTP_AUTH = "true";public static String AUTH_USER = "t***gon@126.com";public static String AUTH_PASSWORD = "asdfsdf";public static String FROM_ADDRESS = "*****@126.com";/** * 發送email,目的地址為一個 *  * @param to *            目的email地址 * @param title *            email的標題 * @param content *            email的內容 * @return 返回是否發送成功 */public static boolean send(String to, String title, String content) {boolean isSuccess = true;if (to == null || title == null || content == null)return false;Properties property = new Properties();// 設定一些基本屬性property.put("mail.smtp.host", SMTP_HOST);property.put("mail.smtp.port", SMTP_PORT);property.put("mail.smtp.protocol", SMTP_PROTOCOL);property.put("mail.smtp.auth", SMTP_AUTH);MyAuthenticator myauth = new MyAuthenticator(AUTH_USER, AUTH_PASSWORD);// 獲得發送郵件的會話Session mailSession = Session.getDefaultInstance(property, myauth);// 產生發送的訊息Message message = new MimeMessage(mailSession);try {// 形成發送的mail地址InternetAddress fromAddress = new InternetAddress(FROM_ADDRESS);message.setFrom(fromAddress);InternetAddress toAddress = new InternetAddress(to);// 加入發送訊息的目的地址addRecipients()兩個重載函數message.addRecipient(Message.RecipientType.TO, toAddress);// 設定訊息題message.setSubject(title);// 設定訊息主題message.setText(content);// 儲存message.saveChanges();} catch (Exception e) {isSuccess = false;System.out.println(e.getMessage());}// 發送mailtry {Transport.send(message,message.getRecipients(Message.RecipientType.TO));} catch (Exception e) {isSuccess = false;System.out.println(e.getMessage());}return isSuccess;}/** * 發送email,目的地址為一組 *  * @param toList *            一組email地址 * @param title *            email的標題 * @param content *            email的內容 * @return boolean 返回是否成功 */public static boolean send(List<String> toList, String title, String content) {boolean isSuccess = true;if (toList == null || title == null || content == null|| toList.size() == 0)return false;Properties property = new Properties();// 設定一些基本屬性property.put("mail.smtp.host", SMTP_HOST);property.put("mail.smtp.port", SMTP_PORT);property.put("mail.smtp.protocol", SMTP_PROTOCOL);property.put("mail.smtp.auth", SMTP_AUTH);MyAuthenticator myauth = new MyAuthenticator(AUTH_USER, AUTH_PASSWORD);// 獲得發送郵件的會話Session mailSession = Session.getDefaultInstance(property, myauth);// 產生發送的訊息Message message = new MimeMessage(mailSession);try {// 形成發送的mail地址InternetAddress fromAddress = new InternetAddress(FROM_ADDRESS);message.setFrom(fromAddress);for (String to : toList) {InternetAddress toAddress = new InternetAddress(to);// 加入發送訊息的目的地址addRecipients()兩個重載函數message.addRecipient(Message.RecipientType.TO, toAddress);}// 設定訊息題message.setSubject(title);// 設定訊息主題message.setText(content);// 儲存message.saveChanges();} catch (Exception e) {isSuccess = false;System.out.println(e.getMessage());}// 發送mailtry {Transport.send(message,message.getRecipients(Message.RecipientType.TO));} catch (Exception e) {isSuccess = false;System.out.println(e.getMessage());}return isSuccess;}/** * @param args */public static void main(String[] args) {// TODOString to = "xuzhanyi@yahoo.com.cn";String title = "title";String content = "content ";List<String> toList = new ArrayList<String>();toList.add(to);int i;for(;;){//System.out.println(new Date() + ":" + SendMail.send(toList, title + (int)(1000*Math.random()), content));try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}}}}class MyAuthenticator extends Authenticator {private String user;private String password;public MyAuthenticator(String user, String password) {this.user = user;this.password = password;}protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(user, password);}}

需要mail.jar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.