當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring发送带附件邮件
生活随笔
收集整理的這篇文章主要介紹了
Spring发送带附件邮件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下面是一個例子使用Spring通過Gmail?SMTP服務器來發送電子郵件附件。為了包含附件的電子郵件,你必須使用?Spring的JavaMailSender及MimeMessage?來代替 MailSender&SimpleMailMessage。 2.Spring的郵件發件人
必須使用 JavaMailSender 代替 MailSender 發送附件,并用 MimeMessageHelper 附加的資源。在這個例子中,它會得到?“c:\\log.txt”?從文件系統(FileSystemResource)作為電子郵件附件的文本文件。
除了文件系統,您還可以從URL路徑(UrlResource對象),類路徑(使用ClassPathResource),InputStream(InputStreamResource)的任何資源......請參考 Spring 的 AbstractResource?類的實現。File : MailMail.java
package com.yiibai.common;import javax.mail.MessagingException; import javax.mail.internet.MimeMessage;import org.springframework.core.io.FileSystemResource; import org.springframework.mail.MailParseException; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper;public class MailMail {private JavaMailSender mailSender;private SimpleMailMessage simpleMailMessage;public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {this.simpleMailMessage = simpleMailMessage;}public void setMailSender(JavaMailSender mailSender) {this.mailSender = mailSender;}public void sendMail(String dear, String content) {MimeMessage message = mailSender.createMimeMessage();try{MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(simpleMailMessage.getFrom());helper.setTo(simpleMailMessage.getTo());helper.setSubject(simpleMailMessage.getSubject());helper.setText(String.format(simpleMailMessage.getText(), dear, content));FileSystemResource file = new FileSystemResource("C:\\log.txt");helper.addAttachment(file.getFilename(), file);}catch (MessagingException e) {throw new MailParseException(e);}mailSender.send(message);} }3. Bean配置文件
配置 mailSender?bean,電子郵件模板,并指定Gmail的SMTP服務器電子郵件的詳細信息。File : Spring-Mail.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="host" value="smtp.gmail.com" /><property name="port" value="587" /><property name="username" value="yiibai.com@gmail.com" /><property name="password" value="password" /><property name="javaMailProperties"><props><prop key="mail.smtp.auth">true</prop><prop key="mail.smtp.starttls.enable">true</prop></props></property> </bean><bean id="mailMail" class="com.yiibai.common.MailMail"><property name="mailSender" ref="mailSender" /><property name="simpleMailMessage" ref="customeMailMessage" /> </bean><bean id="customeMailMessage"class="org.springframework.mail.SimpleMailMessage"><property name="from" value="from@no-spam.com" /><property name="to" value="to@no-spam.com" /><property name="subject" value="Testing Subject" /><property name="text"><value><![CDATA[Dear %s,Mail Content : %s]]></value></property> </bean></beans>4. 運行它
package com.yiibai.common;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main( String[] args ){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");MailMail mm = (MailMail) context.getBean("mailMail");mm.sendMail("Yiibai", "This is text content");} }輸出結果
Dear Yiibai,Mail Content : This is text contentAttachment : log.txt 下載代碼 –??http://pan.baidu.com/s/1jHn9VLW總結
以上是生活随笔為你收集整理的Spring发送带附件邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 100万一年利息是多少
- 下一篇: 什么是创业投资 一种私募股权投资