java發送郵件完整實例 java郵件工具類


http://yuncode.net/code/c_552a2e2dc593894

package com.srie.mail;

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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendmailUtil {
    
    // 設置服務器
    private static String KEY_SMTP = "mail.smtp.host";
    private static String VALUE_SMTP = "10.1.4.125";
    // 服務器驗證
    private static String KEY_PROPS = "mail.smtp.auth";
    private static boolean VALUE_PROPS = true;
    // 發件人用戶名、密碼
    private String SEND_USER = "wangshengdong@itc.cr";
    private String SEND_UNAME = "wangshengdong";
    private String SEND_PWD = "a";
    // 建立會話
    private MimeMessage message;
    private Session s;
 
    /*
     * 初始化方法
     */
    public SendmailUtil() {
        Properties props = System.getProperties();
        props.setProperty(KEY_SMTP, VALUE_SMTP);
        props.put(KEY_PROPS, "true");
        //props.put("mail.smtp.auth", "true");
        s =  Session.getDefaultInstance(props, new Authenticator(){
              protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(SEND_UNAME, SEND_PWD);
              }});
        s.setDebug(true);
        message = new MimeMessage(s);
    }
 
    /**
     * 發送郵件
     * 
     * @param headName
     *            郵件頭文件名
     * @param sendHtml
     *            郵件內容
     * @param receiveUser
     *            收件人地址
     */
    public void doSendHtmlEmail(String headName, String sendHtml,
            String receiveUser) {
        try {
            // 發件人
            InternetAddress from = new InternetAddress(SEND_USER);
            message.setFrom(from);
            // 收件人
            InternetAddress to = new InternetAddress(receiveUser);
            message.setRecipient(Message.RecipientType.TO, to);
            // 郵件標題
            message.setSubject(headName);
            String content = sendHtml.toString();
            // 郵件內容,也可以使純文本"text/plain"
            message.setContent(content, "text/html;charset=GBK");
            message.saveChanges();
            Transport transport = s.getTransport("smtp");
            // smtp驗證,就是你用來發郵件的郵箱用戶名密碼
            transport.connect(VALUE_SMTP, SEND_UNAME, SEND_PWD);
            // 發送
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            System.out.println("send success!");
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        SendmailUtil se = new SendmailUtil();
        se.doSendHtmlEmail("郵件頭文件名", "郵件內容", "wangshengdong@itc.cr");
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM