blogjava's web log

          blogjava's web log
          ...

          spring郵件發(fā)送例

          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"));
          ????????????????????
          //發(fā)件人
          ????????????????????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);
          ????????}

          ????}

          }



          相關java發(fā)送郵件有用的資源

          使用JavaMail收發(fā)郵件,解決了中文附件名問題

          JavaMail 深入淺出

          ?

          posted on 2006-06-22 17:37 record java and net 閱讀(1590) 評論(0)  編輯  收藏 所屬分類: Spring

          導航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動態(tài)語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 密山市| 嘉义县| 乌拉特中旗| 康保县| 集安市| 大埔县| 佛学| 博兴县| 临沧市| 阿图什市| 昔阳县| 铁岭县| 凉城县| 怀柔区| 和田市| 都江堰市| 九寨沟县| 黔江区| 淮安市| 滦平县| 元谋县| 望城县| 广昌县| 长岭县| 白河县| 五河县| 汉源县| 公主岭市| 沐川县| 石景山区| 东乡县| 贵德县| 灌云县| 襄城县| 阜平县| 新绛县| 顺昌县| 福建省| 惠水县| 乌鲁木齐市| 东宁县|