BASE64編碼和解碼---JavaMail發郵件

Base64加密事後的字符串html

要輸入的東西也必須是Base64加密事後的字符串java

在sun.misc.BASE64Encoder包下,在JDK中有,這個類不該該咱們來用,這個包是java和javax包的底層依賴服務器

 在window/Preferences下進行配置session

 使用BASE64進行編碼和解碼:---不用導jar包ide

package com.xjs.base64;

import java.io.IOException;


import org.junit.Test;

import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

public class Demo1 {
    
    @Test
    public void fun1() throws IOException{
        //BASE64編碼
        String s="13271315317";
        BASE64Encoder encoder=new BASE64Encoder(); 
        String str = encoder.encode(s.getBytes("utf-8"));
        System.out.println(str);
        
        //BASE64解碼
        BASE64Decoder decoder=new BASE64Decoder(); 
        byte[] bytes = decoder.decodeBuffer(str);
        System.out.println(new String(bytes,"UTF-8"));
    }
}

 


 

JavaMail

在JavaEE的文檔上測試

1.導包!編碼

  *  mail.jar加密

  *  activation.jarspa

核心類:code

1.Session

  * 若是你獲得了它,表示已經與服務器鏈接上了,與Connection的做用類似!

  獲得Session,須要使用Session.getInstance(Properties,Authenticator);

  Properties props=new Properties();

  props.setProperty("mail.host", "smtp.163.com");

  props.setProperty("mail.smtp.auth", "true");

  

  Authenticator auth = new Authenticator() {

    protected PasswordAuthentication getPasswordAuthentication() {

      return new PasswordAuthentication("用戶名",  "密碼");

    }

  }

  Session session = Session.getInstance(props, auth);

2.MimeMessage

  * 它表示一個郵件對象,你能夠調用它的setFrom(),設置發件人、設置收件人、設置主題、設置正文!

3.TransPort。

  * 它只有一個功能,發郵件。

package com.xjs.javamail;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.junit.Test;


public class Demo1 {
    @Test
    public void fun1() throws AddressException, MessagingException {
        /*
         * 1.獲得Session
         */
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.163.com");
        props.setProperty("mail.smtp.auth", "true");

        Authenticator auth = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("13271315317", "郵箱的登陸受權碼");
            }
        };

        Session session = Session.getInstance(props, auth);
        /*
         * 2.建立MimMessage
         */
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("13271315317@163.com"));// 設置發件人
        msg.setRecipients(RecipientType.BCC, "1820494894@qq.com");// 設置收件人
        msg.setRecipients(RecipientType.BCC, "1874704478@qq.com");
        /*
         * msg.setRecipients(RecipientType.CC, "itcast_cxf@126.com");//設置抄送
         * msg.setRecipients(RecipientType.BCC, "itcast_cxf@126.com");//設置暗送
         */
        msg.setSubject("這是來自謝軍帥的測試郵件");
        msg.setContent("謝軍帥最帥,謝軍帥最帥,謝軍帥最帥,謝軍帥最帥!!!", "text/html;charset=utf-8");
        /*
         * 3.發
         */
        Transport.send(msg);
    }

    /**
     * 帶有附件的郵件
     * 
     * @throws Exception
     */

    @Test
    public void fun2() throws Exception {
        /*
         * 1.獲得Session
         */
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.163.com");
        props.setProperty("mail.smtp.auth", "true");

        Authenticator auth = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("13271315317", "郵箱的登陸受權碼");
            }
        };

        Session session = Session.getInstance(props, auth);
        /*
         * 2.建立MimMessage
         */
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("13271315317@163.com"));// 設置發件人
        msg.setRecipients(RecipientType.BCC, "1874704478@qq.com");

        msg.setSubject("這是來自謝軍帥的測試郵件--有附件");
        // ////////////////////////////////////////
        /*
         * 當發送包含附件的郵件時,郵件體就爲多部件形式! 1.建立一個多部件的部件內容!MimeMultipart
         * MimeMultipart就是一個集合,用來裝載多個主體部件! 2.咱們須要建立兩個主體部件,一個是文本內容的,另外一個是附件的。
         * 主體部件叫MimeBodyPart 3.把MimeMultipart設置給MimeMessage的內容!
         */

        MimeMultipart list = new MimeMultipart();// 建立多部份內容

        // 建立MimeBodyPart
        MimeBodyPart part1 = new MimeBodyPart();
        // 設置主體部件的內容
        part1.setContent("這是一份包含附件的郵件,,,", "text/html;charset=utf-8");
        // 把主體部件添加到集合中
        list.addBodyPart(part1);

        
        //建立MimeBodyPart
        MimeBodyPart part2 = new MimeBodyPart();
        part2.attachFile(new File("G:/金軟軟.jpg"));//設置附件的內容
        part2.setFileName(MimeUtility.encodeText("我喜歡的金歌手.jpg"));//設置顯示的文件名稱,其中encodeText處理中文亂碼問題
        list.addBodyPart(part2);
        
        msg.setContent(list);// 把它設置給郵件做爲郵件的內容

        // ////////////////////////////////////////
        /*
         * 3.發
         */
        Transport.send(msg);
    }
}
相關文章
相關標籤/搜索