2014-04-21 66 views
0

我正在嘗試創建一個java批處理電子郵件程序,該程序將發送一封電子郵件到一個Excel報告附件的特定收件箱。我具備的功能:我將如何創建發送電子郵件的函數,其參數設置爲String,String,String Subject和String body?

public void sendEmail(String to, String from, String subject, String body) 
{ 

} 

我想使用Spring,我試圖堅持xml配置在appcontext文件,而不是現在的註釋(用於學習目的)。我想注入一個靜態資源,這是一個excel文件,並且爲了學習這個模塊的目的,我避免使用FileSystemResource作爲我的導師/老師的附件。我也不需要身體說什麼。主題行爲虛擬目的的「報告」。這裏是我到目前爲止,只需要一個真實需要,所以我可以參照在主類傳遞sendEmail的參數的實際電子郵件功能的肉:

這是applicationContext.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:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=` 
    "http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 

    <context:component-scan base-package="com.transportation"/> 

    <bean id = "mailSender" class = "org.springframework.mail.javamail.JavaMailSenderImpl"> 
     <property name = "host" value = "Whatever" /> 
     <property name = "port" value = "25" /> 
    </bean> 

    <bean id = "sendEmail" class = "com.transportation.email.util.SendEmail"> 
     <constructor-arg ref="mailSender"/> 
    </bean> 

</beans> 

回答

0

試試這個。

public void sendMail(final String messageStr, final boolean isHtml) throws MessagingException { 

     final MimeMessage message = mailSender.createMimeMessage(); 
     final MimeMessageHelper helper = new MimeMessageHelper(message, true); 
     helper.setFrom(simpleMailMessage.getFrom()); 
     helper.setTo(simpleMailMessage.getTo()); 
     helper.setCc(simpleMailMessage.getCc()); 
     helper.setSubject(simpleMailMessage.getSubject()); 
     helper.setText(messageStr, isHtml); 
     helper.addInline("myFile", ResourceUtil.loadResourceAsFileSystemResource("NameOfresource")); 
     mailSender.send(message); 
    } 


public static FileSystemResource loadResourceAsFileSystemResource(final String fileRoute) { 

     File file = null; 
     FileSystemResource fileSystemResource; 
     try { 
      file = new ClassPathResource(fileRoute).getFile(); 
      fileSystemResource = new FileSystemResource(file); 
     } 
     catch (final IOException e) { 
      fileSystemResource = null; 
     } 

     return fileSystemResource; 
    } 



<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
    <property name="javaMailProperties"> 
     <props> 
      <prop key="mail.smtp.host">${mail.smtp.host}</prop> 
      <prop key="mail.smtp.port">${mail.smtp.port}</prop> 
     </props> 
    </property> 
</bean> 

<bean id="sfErrorMailSender" class="XXX.MailSender"> 
    <property name="mailSender" ref="mailSender" /> 
    <property name="simpleMailMessage" ref="sfErrorNotificationMailMessage" /> 
</bean> 

<bean id="sfErrorNotificationMailMessage" class="org.springframework.mail.SimpleMailMessage"> 
    <property name="from" value="${mail.message.error.sf.to}" /> 
    <property name="to" value="${mail.message.error.sf.from}" /> 
    <property name="subject" value="${mail.message.error.sf.subject}" /> 
    <property name="text" value="${mail.message.error.sf.body}" /> 
    <property name="cc" value="${mail.message.error.sf.cc}" /> 
</bean> 
相關問題