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

Java 發送郵件完整教學:JavaMail API、簡單郵件、HTML 郵件、附件與 SMTP 認證實作

     編輯:Java 程式設計入門

Java 發送郵件完整實作指南

使用 Java 應用程式發送 E-mail 需安裝 JavaMail API 與 Java Activation Framework (JAF)。


必要函式庫下載

解壓後將 mail.jar、activation.jar 加入 CLASSPATH。


發送簡單純文字郵件

SendEmail.java

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail {
    public static void main(String[] args) {
        String to = "abcd@gmail.com";
        String from = "web@gmail.com";
        String host = "localhost";

        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", host);
        Session session = Session.getDefaultInstance(props);

        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            msg.setSubject("This is the Subject Line!");
            msg.setText("This is actual message");
            Transport.send(msg);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) { mex.printStackTrace(); }
    }
}

編譯執行:java SendEmail → Sent message successfully....

多收件人

void addRecipients(Message.RecipientType type, Address[] addresses)
  • type:TO、CC(副本)、BCC(密件副本)
  • addresses:InternetAddress[] 陣列

發送 HTML 格式郵件

SendHTMLEmail.java

// ... 相同設定 ...
msg.setContent("<h1>This is actual message</h1>", "text/html");
Transport.send(msg);

關鍵差異:使用 setContent() 並指定 MIME 類型 text/html。


發送帶附件郵件

SendFileEmail.java

// ... 相同設定 ...
BodyPart msgBodyPart = new MimeBodyPart();
msgBodyPart.setText("This is message body");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);

// 附件部分
msgBodyPart = new MimeBodyPart();
String filename = "file.txt";
DataSource source = new FileDataSource(filename);
msgBodyPart.setDataHandler(new DataHandler(source));
msgBodyPart.setFileName(filename);
multipart.addBodyPart(msgBodyPart);

msg.setContent(multipart);
Transport.send(msg);

SMTP 認證(以 QQ 郵箱為例)

若郵件伺服器需帳密驗證:

props.put("mail.smtp.auth", "true");
props.setProperty("mail.user", "myuser");
props.setProperty("mail.password", "mypwd");

QQ 郵箱設定步驟

  1. 登入 QQ 郵箱 → 設定 → 帳號 → 開啟 POP3/SMTP 服務
  2. 生成授權碼作為密碼(非 QQ 登入密碼)

QQ 開啟 SMTP

QQ 生成授權碼

SendEmail2.java(完整認證範例)

import java.util.Properties;
import javax.mail.*;

public class SendEmail2 {
    public static void main(String[] args) {
        String to = "xxx@qq.com";
        String from = "xxx@qq.com";
        String host = "smtp.qq.com";

        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("xxx@qq.com", "qq郵箱授權碼");
            }
        });

        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            msg.setSubject("This is the Subject Line!");
            msg.setText("This is actual message");
            Transport.send(msg);
            System.out.println("Sent message successfully....from w3cschool.cc");
        } catch (MessagingException mex) { mex.printStackTrace(); }
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved