Java 郵件發送,java郵件發送

來源:互聯網
上載者:User

Java 郵件發送,java郵件發送
郵件發送小程式:

import java.util.Properties;import java.util.Scanner;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMessage.RecipientType;public class Main {        // 寄件者    private static String user = "";    // 密碼    private static String pass = "";     // 收件者    private static String to = "";    // 主題    private static String subject = "";    // 內容    private static String content = "";    // 初始化串連郵件伺服器的會話資訊    private static Properties props = new Properties();         static {        // 郵件發送協議         props.setProperty("mail.transport.protocol", "smtp");        // SMTP郵件伺服器        props.setProperty("mail.smtp.host", "smtp.263.net");        // SMTP郵件伺服器預設連接埠        props.setProperty("mail.smtp.port", "25");    }    public static void main(String[] args) throws Exception {                sendTextEmail();    }        public static void sendTextEmail() throws Exception {                //輸入擷取        Scanner info = new Scanner(System.in);        System.out.println("帳號:");        user = info.nextLine();                System.out.println("密碼:");        pass = info.nextLine();                System.out.println("收件者郵箱:");        to = info.nextLine();                System.out.println("主題:");        subject = info.nextLine();                System.out.println("郵件內容:");        content = info.nextLine();                        // 建立Session        Session session = Session.getDefaultInstance(props);                 // 建立MimeMessage        MimeMessage message = new MimeMessage(session);        // 設定寄件者        message.setFrom(new InternetAddress(user));        // 設定郵件主題        message.setSubject(subject);        // 設定收件者        message.setRecipient(RecipientType.TO, new InternetAddress(to));        // 設定內容        message.setText(content);        // 儲存郵件內容        message.saveChanges();                 // 獲得Transport        Transport transport = session.getTransport();        transport.connect(user, pass);        // 發送郵件        transport.sendMessage(message, message.getAllRecipients());        transport.close();    } }

 

聯繫我們

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