Home > Java > javaTutorial > Methods to implement email sending function in Java API development

Methods to implement email sending function in Java API development

PHPz
Release: 2023-06-17 22:28:38
Original
1694 people have browsed it

Java is a powerful programming language, and the development of Java APIs is becoming increasingly mature. During the development process, we often need to use the email sending function, so this article will introduce how to implement the email sending function in Java API development.

1. Create a mail session

The JavaMail API provides a javax.mail.Session class for creating a session, which is an object that connects to the mail server. Creating a session requires specifying some properties, such as the address of the mail server, port number, authentication type, etc. The code is as follows:

import java.util.Properties;
import javax.mail.Session;

public class MailSender {
    public static void main(String[] args) {
        // 创建一个属性对象
        Properties properties = new Properties();
        // 邮件服务器地址
        properties.setProperty("mail.host", "smtp.163.com");
        // 邮件服务器端口号
        properties.setProperty("mail.smtp.port", "25");
        // 是否需要身份验证
        properties.setProperty("mail.smtp.auth", "true");
        // 创建一个会话对象
        Session session = Session.getInstance(properties);
    }
}
Copy after login

2. Create a MIME message object

Mail sending uses the MIME (Multipurpose Internet Mail Extensions) protocol, so you need to create a javax.mail.internet.MimeMessage type The object serves as the message body of the email. The code is as follows:

import java.util.Properties;
import javax.mail.*;    
import javax.mail.internet.*;

public class MailSender {
    public static void main(String[] args) throws MessagingException {
        // 创建一个属性对象
        Properties properties = new Properties();
        // 邮件服务器地址
        properties.setProperty("mail.host", "smtp.163.com");
        // 邮件服务器端口号
        properties.setProperty("mail.smtp.port", "25");
        // 是否需要身份验证
        properties.setProperty("mail.smtp.auth", "true");
        // 创建一个会话对象
        Session session = Session.getInstance(properties);
        // 创建一个MimeMessage对象
        MimeMessage message = new MimeMessage(session);
        //设置发件人邮箱地址
        message.setFrom(new InternetAddress("发件人邮箱"));
        //设置收件人邮箱地址
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("收件人邮箱"));
        //设置邮件的主题
        message.setSubject("邮件主题");
        //设置邮件的正文
        message.setText("邮件正文");
    }
}
Copy after login

3. Set the email attachment

If you need to add an attachment to the email, you can use javax.mail.internet.MimeMultipart type objects to assemble the various parts of the email. The code is as follows:

import java.util.Properties;
import javax.mail.*;    
import javax.mail.internet.*;

public class MailSender {
    public static void main(String[] args) throws MessagingException, AddressException {
        // 创建一个属性对象
        Properties properties = new Properties();
        // 邮件服务器地址
        properties.setProperty("mail.host", "smtp.163.com");
        // 邮件服务器端口号
        properties.setProperty("mail.smtp.port", "25");
        // 是否需要身份验证
        properties.setProperty("mail.smtp.auth", "true");
        // 创建一个会话对象
        Session session = Session.getInstance(properties);
        // 创建一个MimeMessage对象
        MimeMessage message = new MimeMessage(session);
        //设置发件人邮箱地址
        message.setFrom(new InternetAddress("发件人邮箱"));
        //设置收件人邮箱地址
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("收件人邮箱"));
        //设置邮件的主题
        message.setSubject("邮件主题");
        //创建一个MimeMultipart对象,用于组装邮件的各个部分(正文、附件)
        Multipart multipart = new MimeMultipart();
        //创建邮件正文部分
        BodyPart bodyPart = new MimeBodyPart();
        bodyPart.setText("邮件正文内容");
        multipart.addBodyPart(bodyPart);
        //创建附件部分
        bodyPart = new MimeBodyPart();
        String filename = "附件路径";
        DataSource source = new FileDataSource(filename);
        bodyPart.setDataHandler(new DataHandler(source));
        bodyPart.setFileName(filename);
        multipart.addBodyPart(bodyPart);
        //将multipart对象设置为整个邮件的内容
        message.setContent(multipart);
    }
}
Copy after login

4. Send mail

To send mail, you need to use the javax.mail.Transport class. First connect to the mail server, and then call the send() method to send the mail. The code is as follows:

import java.util.Properties;
import javax.mail.*;    
import javax.mail.internet.*;

public class MailSender {
    public static void main(String[] args) throws MessagingException, AddressException {
        // 创建一个属性对象
        Properties properties = new Properties();
        // 邮件服务器地址
        properties.setProperty("mail.host", "smtp.163.com");
        // 邮件服务器端口号
        properties.setProperty("mail.smtp.port", "25");
        // 是否需要身份验证
        properties.setProperty("mail.smtp.auth", "true");
        // 创建一个会话对象
        Session session = Session.getInstance(properties);
        // 创建一个MimeMessage对象
        MimeMessage message = new MimeMessage(session);
        //设置发件人邮箱地址
        message.setFrom(new InternetAddress("发件人邮箱"));
        //设置收件人邮箱地址
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("收件人邮箱"));
        //设置邮件的主题
        message.setSubject("邮件主题");
        //创建一个MimeMultipart对象,用于组装邮件的各个部分(正文、附件)
        Multipart multipart = new MimeMultipart();
        //创建邮件正文部分
        BodyPart bodyPart = new MimeBodyPart();
        bodyPart.setText("邮件正文内容");
        multipart.addBodyPart(bodyPart);
        //创建附件部分
        bodyPart = new MimeBodyPart();
        String filename = "附件路径";
        DataSource source = new FileDataSource(filename);
        bodyPart.setDataHandler(new DataHandler(source));
        bodyPart.setFileName(filename);
        multipart.addBodyPart(bodyPart);
        //将multipart对象设置为整个邮件的内容
        message.setContent(multipart);
        //连接邮件服务器
        Transport transport = session.getTransport();
        transport.connect("发件人邮箱", "发件人邮箱密码");
        //发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        //关闭连接
        transport.close();
    }
}
Copy after login

In summary, the above is the method to implement the email sending function in Java API development. Through understanding and practicing the JavaMail API, we can easily use Java to implement the email sending function, which is of great help to email communication in our daily work and life.

The above is the detailed content of Methods to implement email sending function in Java API development. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template