zoukankan      html  css  js  c++  java
  • JavaMail--发送html邮件

    //发邮件用到的两个包
    "org.springframework:spring-context-support:$springVersion",
    "javax.mail:mail:1.4.7"

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
        <servlet>
            <servlet-name>mvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:Spring_mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>mvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>

    Spring_mvc.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- 开启mvc-->
        <mvc:annotation-driven/>
    
        <!-- 配置扫描发现所有具有 @Controller 注解的类,加载到容器 -->
        <context:component-scan base-package="text"/>
    
    
        <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
            <property name="host" value="smtp.qq.com" /><!--设置邮件服务主机-->
            <property name="username" value="8962xxx@qq.com" /><!--/发送者邮箱的用户名 -->
            <property name="password" value="密码" /><!--//发送者邮箱的密码-->
            <property name="port" value="25" />
            <property name="javaMailProperties">
                <props>
                    <prop key="mail.transport.protocol">smtp</prop>
                    <prop key="mail.smtp.auth">true</prop>
                    <prop key="mail.smtp.starttls.enable">true</prop>
    
                    <prop key="mail.debug">true</prop>
                </props>
            </property>
        </bean>
    
    </beans>

    UserService.java

    package text;
    
    
    import org.junit.Test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;
    
    @Service
    public class UserService {
       @Resource
        private JavaMailSender mailSender;//,mailSender找Spring_mvc.xml的id的
    
        public void sendMailDemo() throws MessagingException {
       String html="<html>" +
                    "<p>我HTML哦</p>" +
                    "</html>";
            MimeMessage msg = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(msg,true,"utf-8");
           //SimpleMailMessage mailMsg = new SimpleMailMessage();//只支持文字
            helper.setFrom("89@qq.com");//发件人
            helper.setTo("110@qq.com");//收件人
            helper.setSubject("很棒哦,今晚你回去扫地了");//邮件标题
            helper.setText(html,true); //测试内容(html)

        
        如果想添加附件的话,只需添加以下代码即可
    ---------------------------------------------------------

    //加载文件资源,作为附件]
          FileSystemResource file = new FileSystemResource(new File("E:\dd/2.png"));
    //加入附件
    helper.addAttachment(file.getFilename(), file);
    -----------------------------------------------------------------------

    mailSender.send(msg);//发送*
    } @Test
    public void test() throws MessagingException { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("Spring_mvc.xml"); mailSender = (JavaMailSender) ctx.getBean("mailSender"); sendMailDemo(); }; }

  • 相关阅读:
    POJ 1753(1)
    2015.12 杭电校赛 Bitwise Equations
    codeforces B
    POJ2387 水水最短路
    CodeForces 337C SDUT秋季训练--周赛(10.31)
    HDU 1241
    POJ 3126 Prime Path
    HDU 2612
    POJ 1426 Find The Multiple
    day94 flask-session 表单验证 信号 SQLALchemy
  • 原文地址:https://www.cnblogs.com/896240130Master/p/6435708.html
Copyright © 2011-2022 走看看