首先先介绍如何使用Mail.jar包发送邮件,这里须要去下载mail.jar包导入进当前工程,能够去maven仓库里面查找下载,代码以下:java
public class TestMain { public static void main(String[] args) throws MessagingException { //一、下载一个mail.jar //二、导包 //三、建立一个用于存放配置信息的对象 Properties prop = new Properties(); //四、设置发送邮件须要的一些信息 //设置发送邮件的协议 prop.put("mail.transport.protocol", "smtp"); //设置发送邮件的主机名 prop.put("mail.smtp.host", "smtp.qq.com"); //设置发送邮件的端口,默认25,可不写 // prop.put("mail.smtp.port", "xx"); //设置发送邮件时,是否须要进行身份认证,可不写 // prop.put("mail.smtp.auth", "true"); //设置是否使用ssl安全链接 prop.put("mail.smtp.ssl.enable", "true"); //五、建立一个Session对象(在Java和邮箱之间创建一个链接) Session session = Session.getDefaultInstance(prop); //六、经过session对象获取一个Transport对象(能够理解为一个输出流) Transport transport = session.getTransport(); //七、获取邮件服务器的认证 //user:发件人的邮箱 //password:发件人的认证码 String user = "cing_self0731@qq.com"; String password = "ufhannqpmjvdccbi"; transport.connect(user, password); //八、建立对象和邮件的映射 关系 Message message = createMessage(session); //九、发送邮件 //第二个参数是获取全部收件人的地址 transport.sendMessage(message, message.getAllRecipients()); //十、关闭通道 transport.close(); } //建立一个邮件对象 //参数:一个Session(链接对象) //返回值:邮件对象(映射) MimeMessage private static Message createMessage(Session session) throws MessagingException { //一、建立一个邮件对象 Message message = new MimeMessage(session); //二、设置邮件信息 //1)设置发送人 message.setFrom(new InternetAddress("cing_self0731@qq.com")); //2)设置接收人 //参数: // 收件人类型 //To:收件人 //CC:抄送人 //BCC:密送 //收件人地址 message.setRecipient(Message.RecipientType.TO, new InternetAddress("cing_self0731@qq.com")); //3)设置发送时间 //这里用的是当前时间 message.setSentDate(new Date()); //4)设置邮件主题 message.setSubject("邮件主题"); //5)设置邮件正文 message.setText("邮件正文"); //保存以上设置 message.saveChanges(); return message; } }
上述代码中涉及到一个邮箱服务器的认证码,获取步骤以下(我这里用的是QQ邮箱):spring
使用Spring发送邮件须要依赖spring-context-support包,因此在使用以前先确认这个包导没导入安全
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- email--> <bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.qq.com"/> <property name="username" value="cing_self0731@qq.com"/> <property name="password" value="xentlpmffyadccgi"/> <property name="defaultEncoding" value="UTF-8"/> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> <prop key="mail.smtp.ssl.enable">true</prop> </props> </property> </bean> </beans>
public class TestMain { public static void main(String[] args) throws MessagingException { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); JavaMailSender sender = (JavaMailSender) context.getBean("sender"); //建立一个邮件对象 MimeMessage mimeMessage = sender.createMimeMessage(); //经过helper帮咱们设置邮件内容 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage); //设置发件人 helper.setFrom("cing_self0731@qq.com"); //设置收件人 helper.setTo("cing_self0731@qq.com"); //设置邮件主题 helper.setSubject("主题"); //设置邮件正文 helper.setText("正文"); //发送邮件 sender.send(mimeMessage); } }
若是有什么问题的话能够留言或者私信我!!!服务器