欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

备忘:Spring发送带附件的邮件(gmail,163)

程序员文章站 2022-06-06 19:46:09
...

备忘录:使用Spring发送各种类型的邮件,经测试,发送方和接收方可以是gmail,163。

 

当发送方是gmail时,设置有点不同。

 

Spring的设置:

 

	<!-- Activates annotation-based bean configuration -->
	<!--
	<context:component-scan base-package="com.aboy" />
	-->
	
	<!-- JavaMailSender -->
	<!--
	<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="smtp.163.com" />
		<property name="username" value="obullxl@163.com" />
		<property name="password" value="********" />
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">true</prop>
				<prop key="mail.smtp.timeout">25000</prop>
			</props>
		</property>
	</bean>
	-->
	<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value="smtp.gmail.com" />
		<property name="username" value="obullxl@gmail.com" />
		<property name="password" value="********" />
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">true</prop>
				<prop key="mail.smtp.timeout">25000</prop>
				<prop key="mail.smtp.port">465</prop>
				<prop key="mail.smtp.socketFactory.port">465</prop>
				<prop key="mail.smtp.socketFactory.fallback">false</prop>
				<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
			</props>
		</property>
	</bean>
	
	<!-- 简单的message -->
	<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
		<property name="to" value="obullxl@163.com" />
		<property name="from" value="obullxl@gmail.com" />
		<property name="subject" value="测试邮件" />
	</bean>
	
	<!-- MimeMessage -->
	<bean id="mimeMessage" class="javax.mail.internet.MimeMessage" factory-bean="javaMailSender"
		factory-method="createMimeMessage" />

 

 

上面的设置中,第一个JavaMailSender的发送方为163邮箱,后面的一个为gmail邮箱的。

 

下面是单元测试代码:

 

    private JavaMailSender mailSender;
    private MailMessage    mailMessage;
    private MimeMessage    mimeMessage;
    
    @Before
    public void setUp() {
        ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/mail-context.xml");
        
        Assert.notNull(context);
        
        this.mailSender = context.getBean(JavaMailSender.class);
        this.mailMessage = context.getBean(MailMessage.class);
        this.mimeMessage = context.getBean(MimeMessage.class);
        
        Assert.notNull(this.mailSender);
        Assert.notNull(this.mailMessage);
        Assert.notNull(this.mimeMessage);
    }
    
    /**
     * 普通文本邮件
     */
    @Test
    public void testSendPlainTextMail() {
        this.mailMessage.setSubject("普通文本邮件");
        this.mailMessage.setText("测试Spring发送Email.");
        
        try {
            mailSender.send((SimpleMailMessage) this.mailMessage);
        } catch (MailException e) {
            System.out.println("Oops: 发送Email失败了....");
            e.printStackTrace();
        }
    }
    
    /**
     * HTML格式邮件
     * @throws MessagingException 
     */
    @Test
    public void testSendHTMLMail() throws MessagingException {
        MimeMessageHelper messageHelper = new MimeMessageHelper(this.mimeMessage, true, "UTF-8");
        
        messageHelper.setTo("obullxl@163.com");
        messageHelper.setFrom("obullxl@gmail.com");
        messageHelper.setSubject("HTML格式邮件");
        messageHelper.setText("<html><head></head><body><h2>Hello! Spring!</h2><b>XXX(中国)信息技术有限公司</b></body></html>", true);
        
        this.mailSender.send(this.mimeMessage);
        
    }
    
    /**
     * 带附件的HTML格式邮件
     * @throws MessagingException 
     */
    @Test
    public void testSetAttachMail() throws MessagingException {
        MimeMessageHelper messageHelper = new MimeMessageHelper(this.mimeMessage, true, "UTF-8");
        
        messageHelper.setTo("obullxl@163.com");
        messageHelper.setFrom("obullxl@gmail.com");
        messageHelper.setSubject("带附件的HTML格式邮件");
        messageHelper.setText("<html><head></head><body><h2>Hello! Spring!</h2><b>XXX(中国)信息技术有限公司</b></body></html>", true);
        
        FileSystemResource file = new FileSystemResource(new File("E:/Study/Spring/Spring-Validator.jpg")); // 根据个人情况设置
        messageHelper.addInline("SpringValidator", file);
        messageHelper.addAttachment("SpringValidator", file);
        
        ClassPathResource resource = new ClassPathResource("jbpm.cfg.xml"); // 根据个人情况设置
        messageHelper.addInline("jbpm.cfg.xml", resource);
        messageHelper.addAttachment("jbpm.cfg.xml", resource);
        
        this.mailSender.send(this.mimeMessage);
    }

 

运行测试用例,即可看到结果!