首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

JavaMail + Apache James 郵件系統完整搭建與發送帶附件郵件實戰

     編輯:Java 相關知識

核心協議

  • SMTP:發送郵件(端口 25)
  • POP3:接收郵件(端口 110)
  • IMAP:現代替代 POP3,支援多端同步

James 服務器搭建

  1. 下載 apache-james-2.3.2.zip,解壓
  2. 運行 bin/run.bat 啟動
  3. Telnet 連接管理端口 4555:telnet localhost 4555
  4. 默認用戶 root/root,執行 adduser username password 創建郵箱賬號
  5. 修改 apps/james/SAR-INF/config.xml 配置域名、DNS 等

Outlook 客戶端配置測試

  • 賬號類型:POP3/SMTP
  • 服務器:localhost(或實際 IP)
  • 用戶名/密碼:James 中創建的賬號
  • 測試收發驗證服務器通暢
  • JavaMail 發送純文本/HTML 郵件

    public class Mail {
        private String host = "192.168.17.176";
        private String from = "wj";
        private String to = "wj@mail.com";
        private String user = "hq@mail.com";
        private String pass = "hq";
    
        public void send() throws Exception {
            Properties props = System.getProperties();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "25");
    
            Session session = Session.getInstance(props, new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user, pass);
                }
            });
    
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            msg.setSubject("我們都是好孩子");
            msg.setContent("這是一封測試郵件!", "text/html;charset=gbk");
            msg.setSentDate(new Date());
    
            Transport.send(msg);
        }
    }

    發送帶附件郵件(Spring 整合)

    public class MailWithAttachment {
        private JavaMailSender mailSender;  // 注入
    
        public void send() throws MessagingException, IOException {
            MimeMessage msg = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8");
            helper.setFrom("hq@mail.com");
            helper.setTo("wj@mail.com");
            helper.setSubject("生活不止眼前的苟且");
            helper.setText("生活不止眼前的苟且!!!");
    
            // 附件 1
            ClassPathResource f1 = new ClassPathResource("/cn/bdqn/attachfiles/test.doc");
            helper.addAttachment(f1.getFilename(), f1.getFile());
    
            // 附件 2(中文文件名需編碼)
            ClassPathResource f2 = new ClassPathResource("/cn/bdqn/attachfiles/附件測試文件.doc");
            helper.addAttachment(MimeUtility.encodeWord(f2.getFilename()), f2.getFile());
    
            mailSender.send(msg);
        }
    }

    Spring 配置 applicationContext.xml

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="192.168.17.176"/>
        <property name="port" value="25"/>
        <property name="username" value="hq@mail.com"/>
        <property name="password" value="hq"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.timeout">5000</prop>
            </props>
        </property>
    </bean>
    
    <bean id="mailWithAttachment" class="...MailWithAttachment">
        <property name="mailSender" ref="mailSender"/>
    </bean>

    關鍵避坑

    • James 管理端口 4555 需防火牆放行
    • 郵件內容編碼統一:text/html;charset=gbk 或 UTF-8,發送端與接收端一致
    • 中文附件名必須 MimeUtility.encodeWord() 編碼,否則 Outlook 顯示亂碼
    • 生產環境建議使用 STARTTLS 加密傳輸,配置 mail.smtp.starttls.enable=true
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved