发送email邮件

1.发送邮件

不用springboot内置真简单,见了鬼了,为什么要去用springboot内置的

1.添加依赖

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.4</version>
        </dependency>

2.配置SendEmail

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;


public class SendEmail {
    private String fromEmailAddress = "827089025@qq.com";
    private String password = "";//授权码
    //private String toEmailAddress = "13516941274@163.com";//目的地址,实际上没有用到,因为下面函数重写了,然后就是传进来的

    //     普通文本邮件
    public void sendTextMail(String toEmailAddress,String title,String message) throws Exception {
        SimpleEmail email = new SimpleEmail(); //创建一个最简单的email对象
        //QQ邮箱smtp服务器
        email.setHostName("smtp.qq.com");
        //POP3服务器(端口995)
        //SMTP服务器(端口465或587)
        email.setSmtpPort(465);
        //验证信息(发送的邮箱地址与密码) 注:这里的密码是授权码
        email.setAuthenticator(new DefaultAuthenticator(fromEmailAddress, password));
        email.setSSLOnConnect(true); // 是否启用SSL
        email.setFrom(fromEmailAddress); //发送邮件的地址(和验证信息的地址一样)
        email.setSubject(title);  //邮件的标题
        email.setMsg(message); //邮件的内容
        email.addTo(toEmailAddress); //发送给哪一个邮件
        email.send();  //进行邮件发送
        System.out.println("发送成功");
    }
    //    public void testEmailAttachment() throws Exception {
//        EmailAttachment attachment = new EmailAttachment(); //创建一个附件对象
//        attachment.setPath("C:\\zc\\duanxin\\redistest\\src\\main\\java\\com\\example\\redistest\\controller\\zjyyc.jpg");  //放一张项目中的图片(指向真实的附件)
//        attachment.setDisposition(EmailAttachment.ATTACHMENT); //完成附件设置
//        attachment.setDescription("这张图片是一个..."); //设置附件的描述
//        attachment.setName("zjyyc.jpg"); //设置附件的名称
//        //创建email对象(MultiPartEmail可以操作附件)
//        MultiPartEmail email = new MultiPartEmail();
//        email.setHostName("smtp.qq.com");
//        email.setSmtpPort(465);
//        //验证信息(发送的邮箱地址与密码) 注:这里的密码是授权码
//        email.setAuthenticator(new DefaultAuthenticator(fromEmailAddress, password));
//        email.setSSLOnConnect(true); // 是否启用SSL
//        email.setFrom(fromEmailAddress); //发送邮件的地址(和验证信息的
//    }
}

3.然后test类测试就好了

import com.example.demo.config.SendEmail;//换成自己的路径
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/test")
public class Test extends SendEmail{

    @Autowired
    private SendEmail sendEmail;

    @RequestMapping("/hello")
    @ResponseBody  //返回的是一个字符串,否则的话会返回templates里面的网页
    public String hello() throws Exception {
        sendEmail.sendTextMail("13516941274@163.com","nihao","jiandanyoujaijn ");
        return “success”;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值