欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java mail使用qq邮箱发邮件的配置方法

程序员文章站 2024-02-08 16:39:40
程序入口:test_email_n.java 复制代码 代码如下:import java.io.ioexception;import java.util.date;imp...

程序入口:
test_email_n.java

复制代码 代码如下:

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属性表示接收者的类型为to
        mailmessage.setrecipient(message.recipienttype.to, new internetaddress(to));
        mailmessage.setsubject(subject, "utf-8");
        mailmessage.setsentdate(new date());
        // minimultipart类是一个容器类,包含mimebodypart类型的对象
        multipart mainpart = new mimemultipart();
        // 创建一个包含html内容的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包.

复制代码 代码如下:

<dependency>
            <groupid>javax.mail</groupid>
            <artifactid>mail</artifactid>
            <version>1.5.0-b01</version>
        </dependency>


email_authenticator.java,这里继承了authenticator 类,用来封装name,和password的:

复制代码 代码如下:

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);
    }
}

配置就这么多,把邮箱密码改成自己的就可以了,否则会报错。程序到这就可以运行了!

下面介绍的是配置properties文件来管理账号密码:

新建一个email.propertis文件。

email.propertis:

复制代码 代码如下:

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

test_email.java 代码改为如下:

复制代码 代码如下:

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";//邮件内容
        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属性表示接收者的类型为to
        mailmessage.setrecipient(message.recipienttype.to, new internetaddress(to));
        mailmessage.setsubject(subject, "utf-8");
        mailmessage.setsentdate(new date());
        // minimultipart类是一个容器类,包含mimebodypart类型的对象
        multipart mainpart = new mimemultipart();
        // 创建一个包含html内容的mimebodypart
        bodypart html = new mimebodypart();
        html.setcontent(content.trim(), "text/html; charset=utf-8");
        mainpart.addbodypart(html);
        mailmessage.setcontent(mainpart);
        transport.send(mailmessage);
    }
}

ok,到此为止。