java实现发送邮件
1.前期准备
2.pom.xml
<!-- java 提供的支持邮件发送相关业务的类 -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>3.代码演示
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class TestMail {
public static void main(String[] args) throws MessagingException {
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.put("mail.smtp.host","smtp.163.com");// smtp服务器地址
Session session = Session.getInstance(props);
session.setDebug(true);
Message msg = new MimeMessage(session);
msg.setSubject("这是一个测试程序....");
msg.setText("第一个java-mail程序---CW");
msg.setFrom(new InternetAddress("17809213302@163.com"));//发件人邮箱(我的163邮箱)
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress("1161564760@qq.com")); //收件人邮箱
msg.saveChanges();
Transport transport = session.getTransport();
transport.connect("17809213302@163.com","123456789abc");//发件人邮箱,授权码(可以在邮箱设置中获取到授权码的信息)
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("邮件发送成功...");
transport.close();
}
}
文章标题:java实现发送邮件
发布时间:2019-11-19, 10:33:39
最后更新:2019-11-19, 10:33:40