最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java mail使用qq郵箱發(fā)郵件的配置方法

 更新時(shí)間:2014年01月03日 09:33:38   作者:  
本文為你介紹了java mail使用qq郵箱發(fā)郵件的方法,大家參考使用吧

程序入口:
Test_Email_N.java

復(fù)制代碼 代碼如下:

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

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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;

public class Test_Email_N {
    public static void  main(String args[]){
        try {
            send_email();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void send_email() throws IOException, AddressException, MessagingException{

        String to = "1219999@qq.com";
        String subject = "subject";
        String content = "content";
        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.port", "25");
        properties.put("mail.smtp.auth", "true");
        Authenticator authenticator = new Email_Authenticator("1219999@qq.com", "password");
        javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);
        MimeMessage mailMessage = new MimeMessage(sendMailSession);
        mailMessage.setFrom(new InternetAddress("1219999@qq.com"));
        // Message.RecipientType.TO屬性表示接收者的類型為T(mén)O
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        mailMessage.setSubject(subject, "UTF-8");
        mailMessage.setSentDate(new Date());
        // MiniMultipart類是一個(gè)容器類,包含MimeBodyPart類型的對(duì)象
        Multipart mainPart = new MimeMultipart();
        // 創(chuàng)建一個(gè)包含HTML內(nèi)容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        html.setContent(content.trim(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        mailMessage.setContent(mainPart);
        Transport.send(mailMessage);
    }
}

其中依賴的jar包為javax.mail,我這里是maven管理的,直接用maven去下載jar包,也可以到https://java.net/projects/javamail/pages/Home直接下載jar包.

復(fù)制代碼 代碼如下:

<dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.5.0-b01</version>
        </dependency>


Email_Authenticator.java,這里繼承了Authenticator 類,用來(lái)封裝name,和password的:

復(fù)制代碼 代碼如下:

package com.infomorrow.webtest.JuxinliTest.restdetect;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Authenticator extends Authenticator {
    String userName = null;
    String password = null;
    public Email_Authenticator() {
    }
    public Email_Authenticator(String username, String password) {
        this.userName = username;
        this.password = password;
    }
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password);
    }
}

配置就這么多,把郵箱密碼改成自己的就可以了,否則會(huì)報(bào)錯(cuò)。程序到這就可以運(yùn)行了!

下面介紹的是配置properties文件來(lái)管理賬號(hào)密碼:

新建一個(gè)email.propertis文件。

email.propertis:

復(fù)制代碼 代碼如下:

mail.smtp.host=smtp.qq.com
mail.smtp.port=25
username=1219999@qq.com
password=password

Test_Email.java 代碼改為如下:

復(fù)制代碼 代碼如下:

package com.infomorrow.webtest.JuxinliTest.restdetect;


import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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;

public class Test_Email {

  public static void main(String args[]){
        try {
            send_email();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void send_email() throws IOException, AddressException, MessagingException{

        String to = "1215186706@qq.com";
        String subject = "subject";//郵件主題
        String content = "content";//郵件內(nèi)容
        Properties properties = new Properties();
        InputStream resourceAsStream = null;
        try {
             resourceAsStream = Object.class.getResourceAsStream("/email.properties");
            properties.load(resourceAsStream);
        } finally{
            if (resourceAsStream!=null) {
                resourceAsStream.close();
            }
        }
        System.err.println("properties:"+properties);
        properties.put("mail.smtp.host", properties.get("mail.smtp.host"));
        properties.put("mail.smtp.port", properties.get("mail.smtp.port"));
        properties.put("mail.smtp.auth", "true");
        Authenticator authenticator = new Email_Authenticator(properties.get("username").toString(), properties.get("password").toString());
        javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);
        MimeMessage mailMessage = new MimeMessage(sendMailSession);
        mailMessage.setFrom(new InternetAddress(properties.get("username").toString()));
        // Message.RecipientType.TO屬性表示接收者的類型為T(mén)O
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        mailMessage.setSubject(subject, "UTF-8");
        mailMessage.setSentDate(new Date());
        // MiniMultipart類是一個(gè)容器類,包含MimeBodyPart類型的對(duì)象
        Multipart mainPart = new MimeMultipart();
        // 創(chuàng)建一個(gè)包含HTML內(nèi)容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        html.setContent(content.trim(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        mailMessage.setContent(mainPart);
        Transport.send(mailMessage);
    }
}

ok,到此為止。

相關(guān)文章

  • Spring Boot深入分析講解日期時(shí)間處理

    Spring Boot深入分析講解日期時(shí)間處理

    項(xiàng)目中使用LocalDateTime系列作為DTO中時(shí)間的數(shù)據(jù)類型,但是SpringMVC收到參數(shù)后總報(bào)錯(cuò),為了配置全局時(shí)間類型轉(zhuǎn)換,嘗試了如下處理方式
    2022-06-06
  • SpringMVC HttpMessageConverter消息轉(zhuǎn)換器

    SpringMVC HttpMessageConverter消息轉(zhuǎn)換器

    ??HttpMessageConverter???,報(bào)文信息轉(zhuǎn)換器,將請(qǐng)求報(bào)文轉(zhuǎn)換為Java對(duì)象,或?qū)ava對(duì)象轉(zhuǎn)換為響應(yīng)報(bào)文。???HttpMessageConverter???提供了兩個(gè)注解和兩個(gè)類型:??@RequestBody,@ResponseBody???,??RequestEntity,ResponseEntity??
    2023-04-04
  • MyBatis標(biāo)簽之Select?resultType和resultMap詳解

    MyBatis標(biāo)簽之Select?resultType和resultMap詳解

    這篇文章主要介紹了MyBatis標(biāo)簽之Select?resultType和resultMap,在MyBatis中有一個(gè)ResultMap標(biāo)簽,它是為了映射select標(biāo)簽查詢出來(lái)的結(jié)果集,下面使用一個(gè)簡(jiǎn)單的例子,來(lái)介紹 resultMap 的使用方法,需要的朋友可以參考下
    2022-09-09
  • SpringBoot2 整合 ClickHouse數(shù)據(jù)庫(kù)案例解析

    SpringBoot2 整合 ClickHouse數(shù)據(jù)庫(kù)案例解析

    這篇文章主要介紹了SpringBoot2 整合 ClickHouse數(shù)據(jù)庫(kù)案例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • jmeter壓力測(cè)試工具簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    jmeter壓力測(cè)試工具簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了jmeter壓力測(cè)試工具相關(guān)介紹資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Java基礎(chǔ)教程之包(package)

    Java基礎(chǔ)教程之包(package)

    這篇文章主要介紹了Java基礎(chǔ)教程之包(package),本文詳細(xì)講解了包的創(chuàng)建、使用等方法,需要的朋友可以參考下
    2014-08-08
  • 解決運(yùn)行jar包出錯(cuò):ClassNotFoundException問(wèn)題

    解決運(yùn)行jar包出錯(cuò):ClassNotFoundException問(wèn)題

    這篇文章主要介紹了解決運(yùn)行jar包出錯(cuò):ClassNotFoundException問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring security實(shí)現(xiàn)對(duì)賬戶進(jìn)行加密

    Spring security實(shí)現(xiàn)對(duì)賬戶進(jìn)行加密

    這篇文章主要介紹了Spring security實(shí)現(xiàn)對(duì)賬戶進(jìn)行加密,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • selenium-java實(shí)現(xiàn)自動(dòng)登錄跳轉(zhuǎn)頁(yè)面方式

    selenium-java實(shí)現(xiàn)自動(dòng)登錄跳轉(zhuǎn)頁(yè)面方式

    利用Selenium和Java語(yǔ)言可以編寫(xiě)一個(gè)腳本自動(dòng)刷新網(wǎng)頁(yè),首先,需要確保Google瀏覽器和Chrome-Driver驅(qū)動(dòng)的版本一致,通過(guò)指定網(wǎng)站下載對(duì)應(yīng)版本的瀏覽器和驅(qū)動(dòng),在Maven項(xiàng)目中添加依賴,編寫(xiě)腳本實(shí)現(xiàn)網(wǎng)頁(yè)的自動(dòng)刷新,此方法適用于需要頻繁刷新網(wǎng)頁(yè)的場(chǎng)景,簡(jiǎn)化了操作,提高了效率
    2024-11-11
  • Java中的Cookie和Session詳細(xì)解析

    Java中的Cookie和Session詳細(xì)解析

    這篇文章主要介紹了Java中的Cookie和Session詳細(xì)解析,客戶端會(huì)話技術(shù),服務(wù)端給客戶端的數(shù)據(jù),存儲(chǔ)于客戶端(瀏覽器),由于是保存在客戶端上的,所以存在安全問(wèn)題,需要的朋友可以參考下
    2024-01-01

最新評(píng)論

新邵县| 阳曲县| 买车| 肃宁县| 垦利县| 黄骅市| 杭州市| 灌阳县| 兴山县| 都江堰市| 甘德县| 天峻县| 惠来县| 浙江省| 容城县| 城口县| 沙洋县| 安图县| 阿鲁科尔沁旗| 大悟县| 竹北市| 乌鲁木齐市| 方城县| 左权县| 东源县| 伊宁县| 临清市| 台北市| 民权县| 丹凤县| 茂名市| 祁连县| 明水县| 汉阴县| 龙海市| 乌鲁木齐县| 江口县| 鹤岗市| 成都市| 马关县| 临汾市|