【Java】javax.mail发送邮件

maven依赖

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

 

1、controller

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.internet.MimeMessage;
import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/v*/personal/mail")
public class MailController extends BaseController {

    @Autowired
    private MailService mailService;

    @Value("${pm.mail.attachment.temp}")
    private String prefix;

    /**
     * 发送邮件
     */
    @PostMapping("/send")
    public Result send(@Valid MailProtocol.Send.Input input, List<MultipartFile> file, BindingResult bindingResult) {

        validateArgs(bindingResult);

        List<String> filesPath = getFilesPath(file);

        mailService.sendMail(StringUtil.join(input.getTo(), ","), StringUtil.join(input.getCc(), ","),
                StringUtil.join(input.getBcc(), ","), input.getSubject(), input.getContent(), filesPath);

        return succeed();
    }

    /**
     * 暂存文件
     *
     * @param attachments 附件
     * @return 附件路径
     */
    private List<String> getFilesPath(List<MultipartFile> attachments) {

        if (isEmpty(attachments)) {
            return new Arra
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
javax.mailJava Mail API的一部分,是Java EE平台中发送电子邮件的标准API。 它提供了一个框架来处理邮件,并支持协议如SMTP,POP3和IMAP。以下是如何在Java中使用javax.mail的示例: 1.添加javax.mail依赖项到您的项目中。 2.编写以下代码来发送电子邮件: ```java import javax.mail.*; import javax.mail.internet.*; import java.util.Properties; public class SendEmail { public static void main(String [] args) { // 收件人的电子邮件ID String to = "abcd@gmail.com"; // 发件人的电子邮件ID String from = "web@gmail.com"; // 假设您是从本地主机发送电子邮件 String host = "localhost"; // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); // 获取默认的Session对象 Session session = Session.getDefaultInstance(properties); try { // 创建默认的MimeMessage对象 MimeMessage message = new MimeMessage(session); // 设置From:header字段 message.setFrom(new InternetAddress(from)); // 设置To:header字段 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置主题 message.setSubject("这是电子邮件主题"); // 设置实际消息 message.setText("这是实际消息"); // 发送消息 Transport.send(message); System.out.println("Sent message successfully...."); } catch (MessagingException mex) { mex.printStackTrace(); } } } ``` 以上代码将向收件人发送一封包含指定主题和正文的电子邮件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值