spring邮件发送例

 

appcontextcos.xml配置文件

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
  <property name="host">
   <value>smtp.163.com</value>
  </property>
  <property name="username">
   <value>wujun1866</value>
  </property>
  <property name="password">
   <value>邮箱密码</value>
  </property>
        <property name="javaMailProperties">
        <props>
        <prop key="mail.smtp.auth">true</prop>
        </props>
        </property>

    </bean>

 <bean id="TestSendMessage" class="com.wjj.testMail">
  <property name="javaMailSender">
   <ref bean="sender"/>
  </property>
  <property name="to">
   <value>renxianqi.2008@163.com</value>
  </property>
  <property name="from">
   <value>wujun1866@163.com</value>
  </property>
  <property name="subject">
   <value>test</value>
  </property>
 </bean>
</beans>

 

 public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(
                "appcontext.xml");
        JavaMailSender mailSend = (JavaMailSender) ac.getBean("TestSendMessage");
        MimeMessagePreparator mail = new MimeMessagePreparator() {
                public void prepare(MimeMessage message)
                    throws MessagingException {
                     //收件人
                    message.setRecipient(Message.RecipientType.TO,
                        new InternetAddress("renxianqi.2008#163.com"));
                    //发件人
                    message.setFrom(new InternetAddress(
                            "wujun1866#163.com"));
                    //内容
                    message.setText("this is a test !");
                    message.setSubject("title标题");
                    //添加附件
                    Multipart multipart = new MimeMultipart();

                    BodyPart attachmentPart= new MimeBodyPart();
                    DataSource source = new FileDataSource("C://1.bmp");
                    attachmentPart.setDataHandler(new DataHandler(source));
                    attachmentPart.setFileName("1.bmp");
                    //attachmentPart.setText("tedddddd");
                  //  sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
                  //  messageBodyPart.setFileName("=?GBK?B?"+enc.encode(attachmentName.getBytes())+"?=");
                  multipart.addBodyPart(attachmentPart);
                  message.setContent(multipart);

                }
            };

        try {
            mailSend.send(mail);
            System.out.print("k");
        } catch (MailException ex) {
            log.error("error:", ex);
        }
    }
}

 

关于发送邮件有用的资源

使用JavaMail收发邮件,解决了中文附件名问题

JavaMail 深入浅出

 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值