spring send mail configuration file

    1、发送邮件配置文件springmail_config.xml 
    <?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns="<a href="http://www.springframework.org/schema/beans" style="font-size: 14px;">http://www.springframework.org/schema/beans</a>" 
           xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" style="font-size: 14px;">http://www.w3.org/2001/XMLSchema-instance</a>" 
           xsi:schemaLocation="<a href="http://www.springframework.org/schema/beans" style="font-size: 14px;">http://www.springframework.org/schema/beans</a> 
           <a href="http://www.springframework.org/schema/beans/spring-beans.xsd" style="font-size: 14px;">http://www.springframework.org/schema/beans/spring-beans.xsd</a>" 
    > 
       <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
            <property name="host" value="smtp.qq.com" /> 
            <property name="port" value="25" /> 
            <property name="username" value="[email protected]" /> 
            <property name="password" value="xxxxxxx" /> 
            <property name="javaMailProperties"> 
              <props> 
               <prop key="mail.smtp.auth">true</prop> 
                <!-- 根据情况可进行设置  
                <prop key="mail.smtp.timeout">2500</prop> 
               --> 
                         </props>  
                   </property>  
         </bean>  
    </beans> 

Java代码  收藏代码

    2、发送邮件java类 
    package com.yihongyu.exec.javamail; 
     
    import java.io.File; 
     
    import javax.mail.internet.InternetAddress; 
    import javax.mail.internet.MimeMessage; 
    import javax.mail.internet.MimeUtility; 
     
    import org.springframework.context.ApplicationContext; 
    import org.springframework.context.support.ClassPathXmlApplicationContext; 
    import org.springframework.core.io.FileSystemResource; 
    import org.springframework.mail.SimpleMailMessage; 
    import org.springframework.mail.javamail.JavaMailSender; 
    import org.springframework.mail.javamail.MimeMessageHelper; 
     
    /**
     * SpringMail测试类
     * 
     * @author tzz
     * 
     */ 
    public class SpringMailUtil { 
        private ApplicationContext context = null; 
     
        public SpringMailUtil() { 
            context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/springmail_config.xml"); 
        } 
     
        // 简单邮件 
        public void simpleSend() { 
            JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender"); 
            SimpleMailMessage mail = new SimpleMailMessage(); 
            mail.setFrom("[email protected]"); 
            mail.setTo("[email protected]"); 
            mail.setSubject(" 测试spring Mail"); 
            mail.setText("你好,java"); 
            mailSender.send(mail); 
        } 
     
        // 带附件 
        public void attachmentSend() { 
            JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender"); 
     
            MimeMessage mime = mailSender.createMimeMessage(); 
            MimeMessageHelper helper; 
            try { 
     
                helper = new MimeMessageHelper(mime, true, "utf-8"); 
                helper.setFrom("[email protected]"); 
                helper.setTo("[email protected]"); 
                helper.setSubject("测试spring Mail附件"); 
                // Need to display the attachment in html 
                helper.setText("Hello,java", true); 
                FileSystemResource attachment = new FileSystemResource(new File("E:\\Test2.doc")); 
                helper.addAttachment(MimeUtility. encodeWord("Test.doc"), attachment);// Solve the Chinese encoding problem of attachments 
     
                mailSender.send(mime); 
     
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
     
        } 
     
        // Multiple attachments 
        public void moreAttachmentSend() { 
            JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender"); 
     
            MimeMessage mime = mailSender.createMimeMessage(); 
            MimeMessageHelper helper; 
            try { 
     
                helper = new MimeMessageHelper(mime, true, "utf-8"); 
                helper.setFrom("[email protected]"); 
                helper.setTo("[email protected]"); 
                helper.setSubject("测试spring Mail附件"); 
                // 需要将附件显示在html中 
                helper.setText("你好,java", true); 
                FileSystemResource attachment = new FileSystemResource(new File("E:\\zqt.sql")); 
                helper.addAttachment("zqt.sql", attachment); 
                FileSystemResource attachment2 = new FileSystemResource(new File("E:\\Test2.doc")); 
                helper.addAttachment(MimeUtility.encodeWord("测试.doc"), attachment2);// 解决附件中文编码问题 
     
                mailSender.send(mime); 
     
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
     
        } 
     
        // 抄送 
        public void copySend() { 
            JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender"); 
     
            MimeMessage mime = mailSender.createMimeMessage(); 
            MimeMessageHelper helper; 
            try { 
     
                helper = new MimeMessageHelper(mime, true, "utf-8"); 
                helper.setFrom("[email protected]"); 
                helper.setTo("[email protected]"); 
                helper.setCc("[email protected]"); 
                helper.setSubject("Test spring Mail attachments"); 
                // Need to display attachments in html 
                helper.setText ("Hello,java", true); 
                FileSystemResource attachment = new FileSystemResource(new File("E:\\zqt.sql")); 
                helper.addAttachment("zqt.sql", attachment); 
                FileSystemResource attachment2 = new FileSystemResource (new File("E:\\Test2.doc")); 
                helper.addAttachment(MimeUtility.encodeWord("Test.doc"), attachment2);// Solve the Chinese encoding problem of attachments 
     
                mailSender.send(mime);   
     
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
     
        } 
     
        // Multiple attachments, multiple send/CC 
        public void moreUserSend() { 
            JavaMailSender mailSender = (JavaMailSender) context.getBean("mailSender"); 
     
            MimeMessage mime = mailSender.createMimeMessage(); 
            MimeMessageHelper helper; 
            try { 
     
                helper = new MimeMessageHelper(mime, true, "utf-8"); 
                helper.setFrom("[email protected]"); 
                helper.setTo("[email protected]");// send 
                / /helper.setCc("[email protected]");//CC//helper 
                .  setTo(new InternetAddress[] { new InternetAddress("[email protected]"), 
                // new InternetAddress("[email protected]") }); 
                helper.setCc(new InternetAddress[] { new InternetAddress("[email protected]"), 
                        new InternetAddress("[email protected]") }); 
                helper.setSubject("测试spring Mail附件"); 
                // 需要将附件显示在html中 
                helper.setText("你好,java", true); 
                FileSystemResource attachment = new FileSystemResource(new File("E:\\zqt.sql")); 
                helper.addAttachment("zqt.sql", attachment); 
                FileSystemResource attachment2 = new FileSystemResource(new File("E:\\Test2.doc")); 
                helper.addAttachment(MimeUtility.encodeWord("Test.doc"), attachment2);// Solve the problem of Chinese encoding of attachments 
     
                mailSender.send(mime); 
     
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
     
        } 
     
        public static void main(String[] args) { 
            SpringMailUtil springMailUtil = new SpringMailUtil(); 
            // simple mail 
            // springMailUtil.simpleSend (); 
            // attachments 
            // springMailUtil.attachmentSend(); 
            // multiple attachments 
            // springMailUtil.moreAttachmentSend(); 
            // cc 
            // springMailUtil.copySend(); 
            // multiple attachments, multiple send/cc 
            springMailUtil .moreUserSend(); 
            System.out.println("Send successfully"); 
     
        } 
    } 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326569486&siteId=291194637