javaMail笔记

    xiaoxiao2026-05-12  0

    JavaMail

             javaMail是提供给开发人员在应用程序中实现邮件发送和接收功能而提供的一套表中开发类库,支持常用的邮件协议,如SMTP,POO3,IMAP,开发人员使用javaMail编写邮件程序时,无需考虑底层的通信细节(Socket)

    邮件开发的相关协议

    SMTP:simple message transfer protocol   发送协议,默认端口:25

    POP:post office protocol 邮局协议。POP3 这个版本用的最多,接受协议,默认端口:110

    JavaMail 的使用:

    导包:

             mail.jar

    spring整合javaMail : spring-context-support jar

    传统邮件开发:

     

           public void TestMail() throws Exception{

            Properties props = new Properties();

            props.put("mail.smtp.host[a1] ", "localhost[a2] ");

            props.put("mail.smtp.auth", "true");

            //props.put("mail.debug","true");//显示应答信息

            Session session = Session.getInstance(props)[a3] ;

            //session.setDebug(true)[a4] ; //显示应答信息

            //构造信息体

            MimeMessage message = new MimeMessage(session);

            //发送邮件地址

            Address address = newInternetAddress("Admin@yan.com");

            message.setFrom(address);

            //收件人地址

            Address toAddress = new InternetAddress("tom@yan.com");

            message.setRecipient(MimeMessage.RecipientType.TO, toAddress);

            //主题

            message.setSubject("Helloword");

            message.setText("hei heihei");

            message.saveChanges();

            Transport transport = session.getTransport("smtp");[a5] 

            transport.connect("localhost","Admin@yan.com", "123");

            transport.sendMessage(message, message.getAllRecipients());//发送

            transport.close();[a6] 

        }

     

    核心类:

    Session 设置服务器的参数

    MimeMessage 构造信息体设置发件人收件人邮箱,以及要发送的内容

    Transport 发送信息的类

     

    可以把该类抽取成一个工具类

    Spring整合javaMail

     

    配置mail.properties

        mail.username=Admin@yan.com

    mail.password=123

     

    内容视情况配置

     

    配置applicationContext-mail.xml

        <!-- 加载properties文件 -->

        <context:property-placeholder location="classpath:mail.properties"/>

        <!-- 配置一个简单邮件对象 -->

        <bean id="simpleMailMessage"class="org.springframework.mail.SimpleMailMessage[a7] ">

            <property name="from"value="${mail.from}[a8] "></property>

        </bean>

        <!-- 邮件的发送对象 -->

        <bean id="javaMailSenderImpl"class="org.springframework.mail.javamail.JavaMailSenderImpl">

            <property name="username"value="${mail.username}"/>

            <property name="password"value="${mail.password}"/>

            <property name="host"value="${mail.host}"/>

            <property name="defaultEncoding"value="${mail.encode}"/>

            <!-- 邮件发送相关信息 -->

            <property name="javaMailProperties">

                <props>

                     <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>

                      <prop key="mail.debug">${mail.debug}</prop>

                      <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>

                </props>

            </property>

        </bean>

     

    Spring整合了javaMail所以有些方法不一样

     

    普通邮件的发送:

       

    SimpleMailMessage message = (SimpleMailMessage) context.getBean("simpleMailMessage");//加载邮件对象

    JavaMailSenderImpl sender = (JavaMailSenderImpl) context.getBean("javaMailSenderImpl");//创建发送对象

           //设置发送信息

           message.setSubject("主题");

           message.setText("内容");

           message.setTo("收件人邮箱");

           //发送邮件

           sender.send(message);

     

     

    复杂邮件的发送:

     

        //获取发送对象

        JavaMailSenderImpl sender = (JavaMailSenderImpl) context.getBean("javaMailSenderImpl");

           //获取mimeMessage

           MimeMessagemessage = sender.createMimeMessage();

           MimeMessageHelperhelper = new MimeMessageHelper(message,true);

           //通过工具类设置主题,内容,图片,附件

           helper.setFrom("Admin@yan.");[a9] 

           helper.setTo("tom@yan.com");[a10] 

           helper.setSubject("主题");

           helper.setText("<html><head></head><body><h1>显示图片</h1><img src=cid:image[a11] [a12] /></body></html>",true);

          

           //发送图片

           FileSystemResourceimg = new FileSystemResource(new File("C:/Users/admin/Pictures/SavedPictures/b.jpg"));

           helper.addInline("image[a13] ", img);

           //发送附件

           FileSystemResourcezipResource = new FileSystemResource(new File("C:/Users/admin/Pictures/SavedPictures/b.jpg"));

           helper.addAttachment("b.zip", zipResource);

           sender.send(message);

    注意:配置文件要灵活应用,很多属性可配可不配或者在java类中设置

    Userservice层实现向新用户发送邮件:

    新创建一个线程用来发送邮件,这样访问数据库的程序和发送邮件的程序互不影响(发送邮件的业务执行的较慢)


     [a1]协议名称 也可以写成: mail.host

     [a2]服务器地址,发送邮箱的smtp 如136:smtp.136.com

     [a3]创建一个session(应答)

     [a4]显示应答应答信息

     [a5]创建运输机,用来发送信息

     [a6]释放资源

     [a7]Spring还提供了发送复杂邮件的类:MimeMailMessage.class 这个类是有javaMailSenderImpl创建的所以此处不需要配置MimeMailMessage.class

     [a8]代表properties属性中的mail.from的值

    这个格式是spell,spring的一个标签

     [a9]发件人邮件邮箱

     [a10]收件人邮箱

     [a11]cid的固定的,Image是自定义的,

     [a12]要加空格,因为html中地址后就有空格,不加空格html标签不对

     [a13]给自定义名称image赋值

    转载请注明原文地址: https://ju.6miu.com/read-1309612.html
    最新回复(0)