首頁 >Java >java教程 >Java 應用程式如何透過 Gmail、Yahoo 或 Hotmail 發送電子郵件?

Java 應用程式如何透過 Gmail、Yahoo 或 Hotmail 發送電子郵件?

Susan Sarandon
Susan Sarandon原創
2024-12-23 22:56:14449瀏覽

How Can Java Applications Send Emails via Gmail, Yahoo, or Hotmail?

在Java 應用程式中使用GMail、Yahoo 或Hotmail 發送電子郵件

問題:

Java 應用程式使用電子郵件帳號像GMail、Yahoo 或Hotmail 一樣傳送電子郵件?如何實現這一點?

答案:

第1 步:匯入JavaMail API

在繼續之前,請確保您已下載所需的JavaMail API jar 並將其新增至您的

第 2步驟:GMail 設定

以下Java 程式碼片段示範使用GMail 帳號傳送電子郵件:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class Main {

    private static String USER_NAME = "*****"; // GMail user name (before "@gmail.com")
    private static String PASSWORD = "********"; // GMail password
    private static String RECIPIENT = "[email protected]";

    public static void main(String[] args) {
        String from = USER_NAME;
        String pass = PASSWORD;
        String[] to = { RECIPIENT }; // List of recipient email addresses
        String subject = "Java send mail example";
        String body = "Welcome to JavaMail!";

        sendFromGMail(from, pass, to, subject, body);
    }

    private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
        Properties props = System.getProperties();
        String host = "smtp.gmail.com";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);

        try {
            // Set email properties
            message.setFrom(new InternetAddress(from));

            // Create InternetAddress array for recipients
            InternetAddress[] toAddress = new InternetAddress[to.length];
            for (int i = 0; i < to.length; i++) {
                toAddress[i] = new InternetAddress(to[i]);
            }

            // Set recipients
            for (InternetAddress addr : toAddress) {
                message.addRecipient(Message.RecipientType.TO, addr);
            }

            // Set subject and body
            message.setSubject(subject);
            message.setText(body);

            // Establish connection and send email
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (AddressException ae) {
            ae.printStackTrace();
        } catch (MessagingException me) {
            me.printStackTrace();
        }
    }
}

注意:

將USER_NAME、PASSWORD、RECIPIENTWORDORD、和body變數替換為適當的

其他注意事項:

  • 應該在 catch 區塊中實現正確的錯誤處理。
  • 您可能需要調整程式碼對於不同的電子郵件供應商,透過更新伺服器位址(主機)和連接埠(例如,對於雅虎:主機=“smtp.mail.yahoo.com”和連接埠=“587 ”)。

以上是Java 應用程式如何透過 Gmail、Yahoo 或 Hotmail 發送電子郵件?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn