java發送郵件demo

來源:互聯網
上載者:User

標籤:

import java.io.File;import java.net.URL;import org.apache.commons.mail.Email;import org.apache.commons.mail.EmailAttachment;import org.apache.commons.mail.EmailException;import org.apache.commons.mail.HtmlEmail;import org.apache.commons.mail.MultiPartEmail;import org.apache.commons.mail.SimpleEmail;public class SendMail {    /**     * 利用 Apache Commons Email 組件(commons-email-1.4)發送郵件     *      * @param args     * @throws Exception      */    public static void main(String[] args) throws Exception {//        simpleMail();        sendHTMLMail();//        sendAttachmentMail();    }    public void simpleMail(){        Email email = new SimpleEmail();        email.setHostName("smtp.test.com");        email.setSmtpPort(25);        email.setAuthentication("[email protected]","admin123");        email.setSSLOnConnect(true);        try {            email.setFrom("[email protected]");            email.setSubject("TestMail");            email.setMsg("This is a test mail ... :-)");            email.addTo("[email protected]");            email.send();        } catch (EmailException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }            }    private static void sendHTMLMail() throws Exception{        // Create the email message          HtmlEmail email = new HtmlEmail();          email.setHostName("smtp.test.com");          email.setSmtpPort(25);          email.setAuthentication("[email protected]", "password");//使用者名稱,密碼          email.setSSLOnConnect(true);          email.addTo("[email protected]", "Ken");          email.addTo("[email protected]");// 設定收件者                    email.setFrom("[email protected]", "Me");          email.setSubject("Test HTML email ");                    // embed the image and get the content id          URL url = new URL("http://qq.test.com/images/111.png");          String cid = email.embed(url, "qhcaifu logo");                    // set the html message          email.setHtmlMsg("<html>The qhcaifu logo - <img src=\"cid:"+cid+"\"></html>");          // set the alternative message          email.setTextMsg("Your email client does not support HTML messages");          // send the email          email.send();    }        private static void sendAttachmentMail() {        MultiPartEmail email = new MultiPartEmail();// 如果要發送帶附件的郵件,需使用這個類        email.setHostName("smtp.test.com");        email.setAuthentication("[email protected]","admin123");        email.setCharset("UTF-8");        try {            email.setFrom("[email protected]");// 設定寄件者            email.addTo("[email protected]");// 設定收件者            email.setSubject("測試郵件");// 設定主題            email.setMsg("測試郵件測試郵件測試郵件");// 設定郵件內容            File file = new File("C:\\testEmail.txt");// 要發送的附件            EmailAttachment attachment = new EmailAttachment();            attachment.setPath(file.getPath());            attachment.setName(file.getName());            attachment.setDescription("附件描述");            attachment.setDisposition(EmailAttachment.ATTACHMENT);// 附件的類型            email.attach(attachment);            email.send();            System.out.println("發送成功");        } catch (EmailException e) {            e.printStackTrace();        }    }}

-------所需jar包:

 

-------異常解決:

java發送郵件demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.