spring自带JavaMailSender发送邮件

spring自带的MailSender类在spring-context-support-xxx.RELEASE.jar文件中,这里以4.3.4版本为例。

1、新建gradle项目,引入依赖配置。

 compile group:"org.springframework",name:"spring-context-support",version:"4.3.4.RELEASE"
 compile group:"javax.mail",name:"mail",version:"1.4.7"

2、编写发送邮件服务类。

package com.xx.sendmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
public class MailSenderService {
	
	@Autowired
	private JavaMailSender mailSender;
	
	public void send(SimpleMailMessage message){
		mailSender.send(message);
		System.out.println("send message successfully.");
	}
}

3、新建配置文件applicationContext.xml与mail.properties。

<?xml version="1.0" encoding="UTF-8"?>
<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 
                            http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
	<bean id="propertyConfigurer" 
           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	     <property name="locations">
	          <value>mail.properties</value>
	     </property>
	</bean>
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
	     <property name="host" value="${mail.host}"></property>
	     <property name="javaMailProperties">
	          <props>
	                <prop key="mail.smtp.auth">true</prop>
	                <prop key="mail.smtp.timeout">25000</prop>
	                <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
	                <prop key="mail.smtp.starttls.enable">true</prop>
	          </props>
	     </property>
	     <property name="username" value="${mail.username}"></property>
	     <property name="password" value="${mail.password}"></property>
	     <property name="port" value="${mail.port}"></property>
	</bean>
	<bean id="mailSenderService" class="com.xx.sendmail.MailSenderService"></bean>
</beans>

mail.properties,我这里使用的是126邮箱作为测试邮箱,因此,密码这里需要使用客户端授权码。

mail.host=smtp.126.com
mail.username=y******9@126.com
mail.password=y******2//这里是客户端授权码,不是邮箱登录密码
mail.port=465

4、编写测试邮件发送程序。

package com.xx.sendmail;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
public class Application {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
                                                         new String[]{"applicationContext.xml"});
		MailSenderService senderService = (MailSenderService)context.getBean("mailSenderService");
		SimpleMailMessage mail = new SimpleMailMessage();
		mail.setTo("******5@sina.com");
		mail.setFrom("y******9@126.com");
		mail.setSubject("spring sendmail test");
		mail.setText("Hello,***,this is my test,do not reply.");
		
		senderService.send(mail);
	}
}

运行,如果配置均正确,就会看到打印send message successfully.然后去收件箱检查,发现邮件一封。


这里如果设置邮箱服务器的密码不是客户端授权码,那么可能会遇到以下错误。

Exception in thread "main" org.springframework.mail.MailAuthenticationException: Authentication failed; 
nested exception is javax.mail.AuthenticationFailedException: 535 Error: authentication failed

这个问题在126邮箱设置中有明确说明:


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
JavaMailSenderJavaMail API中的一个类,用于发送电子邮件。使用JavaMailSender发送带有附件的邮件可以按照以下步骤进行: 1. 创建一个JavaMailSender实例,可以通过注入或手动创建。 2. 创建一个MimeMessage实例,它将被用来构建电子邮件。 3. 使用MimeMessageHelper类的帮助方法,如addAttachment(),来添加附件。该类可以通过传入MimeMessage实例来实例化。 4. 设置电子邮件的其他属性,如收件人地址、主题、邮件内容等。 5. 使用JavaMailSendersend()方法发送邮件。该方法需要一个MimeMessage参数。 以下是一个示例代码片段,展示了使用JavaMailSender发送带有附件的邮件的步骤: ```java @Autowired private JavaMailSender javaMailSender; public void sendEmailWithAttachment(String toEmail, String subject, String body, String attachmentPath) { try { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(toEmail); helper.setSubject(subject); helper.setText(body); // 添加附件 FileSystemResource file = new FileSystemResource(new File(attachmentPath)); helper.addAttachment(file.getFilename(), file); javaMailSender.send(message); System.out.println("邮件发送成功!"); } catch (MessagingException e) { System.out.println("邮件发送失败:" + e.getMessage()); } } ``` 在上面的示例中,我们首先创建一个MimeMessage对象,然后使用MimeMessageHelper类的实例化对象来构建邮件消息。将收件人地址、邮件主题和内容都设置好之后,使用addAttachment()方法将附件添加至邮件中。 最后,通过调用JavaMailSendersend()方法来发送邮件。如果发送成功,将输出"邮件发送成功!",否则输出"邮件发送失败:"并附上错误信息。 以上就是使用JavaMailSender发送邮件附件的简单步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值