通過Java代碼發送OutLook郵件


准備

我們想通過Java代碼實現發送OutLook郵件,必須准備以下材料:

  • OutLook郵箱
  • 目標郵箱

查看OutLook郵箱信息

打開OutLook郵箱,在Settings中搜索或找到SMTP:

打開以下界面,拿到我們想要的數據(ServerName 以及 Port),如圖:

JAVA項目

使用Maven或者創建一個普通項目,選擇導入Maven依賴或導入jar包,我這里使用的是Maven創建的Java項目,所以我導入了以下依賴:

<!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.6.2</version>
    </dependency>

編寫發送代碼

復制或編寫以下代碼,理解以下代碼並不困難,我幾乎標注了每一行代碼:

public static boolean SendEmail(String sender,String password,String host,String port,String receiver)
    {

        try{

            Properties props = new Properties();
            // 開啟debug調試
            props.setProperty("mail.debug", "true");  //false
            // 發送服務器需要身份驗證
            props.setProperty("mail.smtp.auth", "true");
            // 設置郵件服務器主機名
            props.setProperty("mail.host", host);
            // 發送郵件協議名稱 這里使用的是smtp協議
            props.setProperty("mail.transport.protocol", "smtp");
            // 服務端口號
            props.setProperty("mail.smtp.port", port);
            props.put("mail.smtp.starttls.enable", "true");

            // 設置環境信息
            Session session = Session.getInstance(props);

            // 創建郵件對象
            MimeMessage msg = new MimeMessage(session);

            // 設置發件人
            msg.setFrom(new InternetAddress(sender));

            // 設置收件人
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(receiver));

            // 設置郵件主題
            msg.setSubject("this is subject");

            // 設置郵件內容
            Multipart multipart = new MimeMultipart();

            MimeBodyPart textPart = new MimeBodyPart();
            //發送郵件的文本內容
            textPart.setText("this is the text");
            multipart.addBodyPart(textPart);

            // 添加附件
            MimeBodyPart attachPart = new MimeBodyPart();
            //可以選擇發送文件...
            //DataSource source = new FileDataSource("C:\\Users\\36268\\Desktop\\WorkSpace\\MyApp\\Program.cs");
            //attachPart.setDataHandler(new DataHandler(source));
            //設置文件名
            //attachPart.setFileName("Program.cs");
            multipart.addBodyPart(attachPart);

            msg.setContent(multipart);

            Transport transport = session.getTransport();
            // 連接郵件服務器
            transport.connect(sender, password);
            // 發送郵件
            transport.sendMessage(msg, new Address[]{new InternetAddress(receiver)});
            // 關閉連接
            transport.close();

            return true;
        }catch( Exception e ){
            e.printStackTrace();
            return false;
        }
    }

運行方法

我這里直接使用main方法直接運行了(為了調試方便和更好的理解),如果是具體業務,你應該對這些代碼包括參數進行封裝:

public static void main(String[] args) {

        String sender = "erosionzhu@outlook.com";
        String password = ""; //填寫你的outlook帳戶的密碼

        // 收件人郵箱地址
        String receiver = "362687440@qq.com";

        // office365 郵箱服務器地址及端口號
        //這個就是之前的Server Name,注意:你使用的Outlook應用可能使用了不同的服務器,根據自己剛才拿到的地址為准
        String host = "smtp.office365.com";
        String port = "587";    //這個就是拿到的port
        boolean b = SendEmail(sender, password, host, port, receiver);
        if(b)
        {
            System.out.println("發送成功");
        }else
        {
            System.out.println("發送失敗");
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM