Java implements sending email function

content

1. Code function

2. Function realization

mail entity

Email parameter configuration

send email

main function

3. Download the source code

4. Business cooperation


1. Code function

Java implements the function of sending mail, and uses the functions in the mail package to implement this function. Without further ado, just paste the code.

2. Function realization

mail entity

package com.sjsq.entity;

/**
 * 邮件实体类
 */
public class Email {

    // 主题
    private String subject;
    // 内容
    private String conetent;

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getConetent() {
        return conetent;
    }

    public void setConetent(String conetent) {
        this.conetent = conetent;
    }
}

Email parameter configuration

package com.sjsq.properties;

/**
 * 邮件参数配置
 */
public class MailProperties {

    // 发件人
    public static final String FROM = "";
    // 发件主机
    public static final String HOST = "";
    // 发件人账号
    public static final String USERNAME = "";
    // 发件人密码
    public static final String PASSWORD = "";

}

send email

package com.sjsq.util;

import com.sjsq.entity.Email;
import com.sjsq.properties.MailProperties;


import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发送邮件
 */
public class SendMail {

    // 发送邮件
    public static void sendMail(Email email,String address){

        // 定义收件人
        InternetAddress to_address[] = new InternetAddress[1];
        try {
            to_address[0] = new InternetAddress(address);
        } catch (AddressException e) {
            e.printStackTrace();
        }

        // 获取系统属性
        Properties properties = System.getProperties();
        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", MailProperties.HOST);
        properties.put("mail.smtp.auth", "true");


        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(MailProperties.USERNAME,MailProperties.PASSWORD);
            }
        });


        // 定义头部字段及发邮件
        try {
            // 创建默认的MimeMessage对象
            MimeMessage message = new MimeMessage(session);
            // 设置发件人From 头部字段
            message.setFrom(new InternetAddress(MailProperties.FROM));
            // 设置收件人To 头部字段
            message.addRecipients(Message.RecipientType.TO,to_address);
            // 设置Subject 头部字段
            message.setSubject(email.getSubject());
            // 设置消息体
            message.setText(email.getConetent());

            // 发送消息
            Transport.send(message);
            System.out.println("发送成功");

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}


main function

package com.sjsq.main;

import com.sjsq.entity.Email;

import static com.sjsq.util.SendMail.sendMail;

/**
 * 主函数
 */
public class Main {


    // 测试
    public static void main(String[] args) {

        String subject = "Java测试邮件";
        String concent = "测试内容!";
        String address = "[email protected]";

        Email email = new Email();
        email.setSubject(subject);
        email.setConetent(concent);

        sendMail(email,address);
    }
}

3. Download the source code

Java implements the function of sending emails. Java implements the function of sending emails. The code has been tested and run correctly after many times. Please download it with confidence! For more download resources and learning materials, please visit the CSDN download channel. https://download.csdn.net/download/helongqiang/79841043

4. Business cooperation

For business cooperation, please contact WeChat: 15754308633           

Remarks: Business Cooperation - Name

Like it, get off the list and get rich as soon as possible, and go to the pinnacle of life! ! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324163120&siteId=291194637