ssm框架實現發送郵件


ssm框架實現發送郵件

獲取授權碼: arpaxwwaeoqvtbceb // 個人經過加密處理, 你們無法使用啊. 可以去自己的qq郵箱獲取授權碼

1.導入依賴

<!--spring支持-->
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>5.0.0.RELEASE</version>
</dependency>
<!--郵件發送-->
<dependency>
      <groupId>com.sun.mail</groupId>
      <artifactId>javax.mail</artifactId>
      <version>1.6.1</version>
</dependency>

2.配置文件(mail.properties)

#服務器主機名 smtp.xx.com
mail.smtp.host=smtp.qq.com
mail.smtp.username=*********@qq.com
#密碼/客戶端授權碼
mail.smtp.password=**********
#編碼字符
mail.smtp.defaultEncoding=utf-8
#是否進行用戶名密碼校驗
mail.smtp.auth=true
#設置超時時間
mail.smtp.timeout=20000

3.注入spring容器

<!--郵件配置-->
<!--
        讀取郵件配置文件,
        其中ignore-unresolvable="true"屬性是配置文件中存在
        多個property-placeholder時出現解析不了的占位符進行忽略掉,
    -->
<context:property-placeholder location="classpath:mail.properties" ignore-unresolvable="true"/>
<!--配置郵件接口-->
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${mail.smtp.host}"/>
    <property name="username" value="${mail.smtp.username}"/>
    <property name="password" value="${mail.smtp.password}"/>
    <property name="defaultEncoding" value="${mail.smtp.defaultEncoding}"/>
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
            <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
        </props>
    </property>
</bean>

4.測試

3.1 簡單的郵件

@Autowired
private JavaMailSenderImpl javaMailSender;

@Test
void contextLoads() {
    SimpleMailMessage message = new SimpleMailMessage(); // 創建消息對象
    message.setSubject("標題"); // 標題
    message.setText("正文"); // 只支持文本, 不支持html
    message.setTo("546279462@qq.com"); // 收件人
    message.setFrom("546279462@qq.com"); // 發件人
    javaMailSender.send(message); // 發送
}

3.2 復雜的郵件

@Autowired
private JavaMailSenderImpl javaMailSender;

@Test
void testMail() throws MessagingException {
    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setSubject("標題"); // 標題
    // 內容, 第二個參數為true則以html方式發送, 否則以普通文本發送
    helper.setText("<h1 style='red'>內容</h1>", true);
    //發送附件
    helper.addAttachment("1.jpg",new File("C:\\Users\\zpk\\Desktop\\loading\\加載-063.gif"));
    helper.addAttachment("2.jpg",new File("C:\\Users\\zpk\\Desktop\\loading\\加載-067.gif"));
    helper.setTo("546279462@qq.com"); // 收件人
    helper.setFrom("546279462@qq.com"); // 發件人
    javaMailSender.send(message); // 發送
}

不注入容器方式

不注入容器, 直接讀取第二部的配置文件

@Test
public void testMail(){
    AbstractApplicationContext as = new  ClassPathXmlApplicationContext("applicationContext.xml");
    JavaMailSenderImpl javaMailSender = as.getBean("javaMailSender",JavaMailSenderImpl.class);
    MimeMessage mailMessage = javaMailSender.createMimeMessage();//創建郵件對象
    MimeMessageHelper mimeMessageHelper;
    Properties prop = new Properties();
    String from;
    try {
        // 從配置文件讀取發件人郵箱地址
        prop.load(this.getClass().getResourceAsStream("util/mail.properties"));
        from = prop.getProperty("mail.smtp.username");
        mimeMessageHelper = new MimeMessageHelper(mailMessage,true);
        mimeMessageHelper.setFrom(from); //發件人地址
        mimeMessageHelper.setTo("546279462@qq.com"); //收件人郵箱
        mimeMessageHelper.setSubject("郵件主題"); //郵件主題
        mimeMessageHelper.setText("<p style='color: red'>郵件內容123345</p>" +
                "<img src='cid:06'/>",true); //true表示以html方式發送
        File file = new File("C:\\Users\\54627\\Pictures\\06.jpg"); //導入其他資源
        FileSystemResource resource = new FileSystemResource(file);
        mimeMessageHelper.addInline("06",resource); //可以指定id,在內容中引用(cid:id)
        mimeMessageHelper.addAttachment("06.jsp",resource); //附件
        javaMailSender.send(mailMessage);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    }
    System.out.println("發送成功");
}

擴展: maven讀取properties

try {
    //讀取配置文件
    InputStream is = Test.class.getClassLoader().getResourceAsStream("util/mail.properties");
    Properties properties = new Properties();
    properties.load(is);
    System.out.println(properties.getProperty("mail.smtp.host"));
} catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException(e);
}


免責聲明!

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



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