java发送邮件
2023-02-15 10:05:12 561浏览
邮箱注册笔记
先把自己的邮箱设置一下接口服务
登录你的邮箱,点击设置——账户下操作。最重要的是生成授权码!!!
需要的jar
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
案例代码
<bean class="org.springframework.mail.javamail.JavaMailSenderImpl" id="javaMailSender"> <property name="password" value="${mail.password}"/> <property name="username" value="${mail.account}"/> <property name="host" value="${mail.host}"/> <property name="port" value="${mail.port}"/> </bean>
@Value("${mail.account}") private String account; @Autowired JavaMailSender javaMailSender; @Override public void send( String to, String context) { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); //account simpleMailMessage.setFrom(account); simpleMailMessage.setTo(to); simpleMailMessage.setSubject("爱旅行的激活码");//标题 simpleMailMessage.setText(context); javaMailSender.send(simpleMailMessage); }
测试类demo
public class SpringSendMail { public static void main(String[] args) { JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); javaMailSender.setHost("smtp.qq.com"); javaMailSender.setPassword("换成你的授权码");//授权码 javaMailSender.setPort(25); javaMailSender.setUsername("jf3q@qq.com"); SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setFrom("jf3q@qq.com"); simpleMailMessage.setTo("1913284695@qq.com"); simpleMailMessage.setSubject("爱旅行的激活码");//标题 simpleMailMessage.setText("123456"); javaMailSender.send(simpleMailMessage); } }
好博客就要一起分享哦!分享海报
此处可发布评论
评论(0)展开评论
暂无评论,快来写一下吧
展开评论
他的专栏
他感兴趣的技术