使用Spring MVC发送邮件

时间:2015-06-19 09:09:30

标签: spring spring-mvc spring-integration

我有一个自定义MailDto对象,我在其中设置to,cc,bCc字段并使用RestTemplate将其发送到Spring MVC控制器 如下

@RequestMapping(value = "/SendMail"  )
    public ResponseEntity<String> SendMail( @RequestBody MailMessageDto mailDto)
            throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException

从控制器我调用网关(Spring集成)。 Gateway有一个方法public void sendMail(MailMessageDto mailDto)。网关通道调用一个转换器,它将mailDto转换为MimeMessageHelper对象。 mimeMessageHelper对象使用mailoutbound适配器作为邮件发送。现在我想使用RestTemplate发送邮件,但使用相同的MailDto附件。

问题是当我更改Gateway和Transformmer方法签名以容纳Multipart文件和MailMessageDto时,我将转换为MimeMessageHelper对象,在转换方法中,spring集成无法理解转换方法。

我想知道控制器方法的签名应该是什么,以及如何在xml中声明Transformer转换方法。

The mail-context file
<context:component-scan base-package="com.infra.mail,com.infra.audit " />  
    <mvc:annotation-driven />  

    <!-- <context:property-placeholder 
location="classpath:/mail.properties" ignore-unresolvable="true" /> -->

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.host}"/>
        <property name="port" value="${mail.port}"/>
        <property name="username" value="${mail.username}"/>
        <property name="password" value="${mail.password}"/>
        <property name="defaultEncoding" value="UTF-8"/> 
        <property name="javaMailProperties">
            <props>
                <!-- Use SMTP transport protocol -->
                <prop key="mail.transport.protocol">smtp</prop>
                <!-- Use SMTP-AUTH to authenticate to SMTP server -->
                <prop key="mail.smtp.auth">true</prop>
                <!-- Use TLS to encrypt communication with SMTP server -->
                <prop key="mail.smtp.starttls.enable">false</prop>
                <prop key="mail.debug">true</prop>
            </props>
        </property>
    </bean>




     <int:channel id="xfromMailChannel">
    <int:queue/> 
    </int:channel>

The Transformer file

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.MailMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.velocity.VelocityEngineUtils;


/**
 * The Class MailMessageTransformer.
 */
public class MailMessageTransformer {

/** The velocity engine. */
@Autowired
private VelocityEngine velocityEngine;

/** The mail sender. */
@Autowired
private JavaMailSender mailSender;

/** The Constant LOG. */
private static final Logger LOG = Logger.getLogger(MailMessageTransformer.class);



/**
* Transform MailMessageDto to SimpleMailMessage.
*
* @param mailDto the mail dto
* @return the mail message
*/
public MailMessage transformSimple(MailMessageDto mailDto) {

LOG.debug("MailMessageTransformer.transform.MailMessageDto:::" + mailDto);
if (mailDto == null) {
return null;
}

if (mailDto.getEncoding() == null) {
mailDto.setEncoding(IMailConstants.DEFAULT_ENCODING);
}

if (mailDto.getTemplateName() == null
|| !mailDto.getTemplateName().endsWith(IMailConstants.VELOCITY_TEMPLATE_EXTN)) {
mailDto.setTemplateName(getTemplateName(mailDto.getEmailType()));
}

SimpleMailMessage message = new SimpleMailMessage();

message.setFrom(mailDto.getFrom());
message.setTo(mailDto.getTo());
message.setSubject(mailDto.getSubject());
message.setText(getMailContent(mailDto));
message.setSentDate(new Date(System.currentTimeMillis()));

message.setBcc(mailDto.getBcc());
message.setCc(mailDto.getCc());
message.setReplyTo(mailDto.getReplyTo());

/*
* if (null != mailDto.getBcc()) { message.setBcc(mailDto.getBcc()); }
* if (null != mailDto.getBcc()) { message.setCc(mailDto.getCc()); } if
* (null != mailDto.getReplyTo() && !mailDto.getReplyTo().isEmpty()) {
* message.setReplyTo(mailDto.getReplyTo()); }
*/

LOG.debug("MailMessageTransformer.transform.message:::" + message);
return message;
}

/**
* Create the email content/body using velocity engine, velocity template
* and MailMessageDto.
*
* @param mailDto the mail dto
* @return the mail content
*/
private String getMailContent(MailMessageDto mailDto) {
Map<String, Object> model = getVelocityModel(mailDto);
String mailContent = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, mailDto.getTemplateName(),
mailDto.getEncoding(), model);
LOG.debug("MailMessageTransformer.getMailContent.mailContent:::" + mailContent);
return mailContent;
}

/**
* Create the Model Map referred in velocity mail template file.
*
* @param mailDto the mail dto
* @return the velocity model
*/
private Map<String, Object> getVelocityModel(MailMessageDto mailDto) {
Map<String, Object> model = new HashMap<String, Object>();
model.put(IMailConstants.VM_USER, getUser(mailDto.getTo()));
model.put(IMailConstants.VM_ALERT_TYPE, getAlertType(mailDto.getEmailType()));
model.put(IMailConstants.VM_CONTENT, mailDto.getText());
return model;
}

/**
* Fetch the user (receients) name from MailMessageDto. This is referred in
* velocity template file. Currently from TO email list. Can be changed to
* look up user store if future
*
* @param toList the to list
* @return the user
*/
private String getUser(String[] toList) {

StringBuffer user = new StringBuffer();

for (String mailId : toList.clone()) {
StringTokenizer token = new StringTokenizer(mailId, IMailConstants.EMAIL_AT);
String to = token.nextToken();
user.append(to).append(IMailConstants.COMMA);
}

return user.toString();
}

/**
* Fetch the email type from MailMessageDto. This is referred in velocity
* template file.
*
* @param emailType the email type
* @return the alert type
*/
private String getAlertType(String emailType) {

String alertType = IMailConstants.SYSTEM_ALERT_TEXT;

if (IMailConstants.REALTIME_EMAILTYPE.equalsIgnoreCase(emailType)) {
alertType = IMailConstants.REALTIME_ALERT_TEXT;
} else if (IMailConstants.OFFLINE_EMAILTYPE.equalsIgnoreCase(emailType)) {
alertType = IMailConstants.OFFLINE_ALERT_TEXT;
}

return alertType;
}

/**
* Fetch the name of velocity template file based on the emailType. Used by
* Velocity Engine.
*
* @param emailType the email type
* @return the template name
*/
private String getTemplateName(String emailType) {

String templateName = IMailConstants.SYSTEM_ALERT_VM;
if (IMailConstants.REALTIME_EMAILTYPE.equalsIgnoreCase(emailType)) {
templateName = IMailConstants.REALTIME_ALERT_VM;
} else if (IMailConstants.OFFLINE_EMAILTYPE.equalsIgnoreCase(emailType)) {
templateName = IMailConstants.OFFLINE_ALERT_VM;
}
return templateName;
}


/**
* Transform MailMessageDto to MimeMessage.
*
* @param mailDto the mail dto
* @return the mime message
*/
//public MimeMessage transform(final MailMessageDto mailDto,final CommonsMultipartFile file) {

public MimeMessage transform(MailMessageDto mailDto, final MultipartFile file) {



LOG.debug("MailMessageTransformer.transform.mailDto:::" + mailDto);

if (mailDto == null) {
return null;
}

if (mailDto.getEncoding() == null) {
mailDto.setEncoding(IMailConstants.DEFAULT_ENCODING);
}

if (mailDto.getTemplateName() == null
|| !mailDto.getTemplateName().endsWith(IMailConstants.VELOCITY_TEMPLATE_EXTN)) {
mailDto.setTemplateName(getTemplateName(mailDto.getEmailType()));
}

mailDto.setText(mailDto.getText().replaceAll(System.lineSeparator(), IMailConstants.HTML_NEW_LINE));

MimeMessage mimeMessage = null;
try {
String attachName = file.getOriginalFilename();
mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
message.setTo(mailDto.getTo());
message.setFrom(mailDto.getFrom());
message.setSubject(mailDto.getSubject());
message.setText(getMailContent(mailDto), IMailConstants.TRUE);
message.setSentDate(new Date(System.currentTimeMillis()));
message.addAttachment(attachName, new InputStreamSource() {

               @Override
               public InputStream getInputStream() throws IOException {
                   return file.getInputStream();
               }
           });


if (null != mailDto.getBcc()) {
message.setBcc(mailDto.getBcc());
}
if (null != mailDto.getBcc()) {
message.setCc(mailDto.getCc());
}
if (null != mailDto.getReplyTo() && !mailDto.getReplyTo().isEmpty()) {
message.setReplyTo(mailDto.getReplyTo());
}

}
catch (MessagingException msgex) {
LOG.error("MailMessageTransformer.transformMime.Exception::: MessagingException", msgex);

}
catch (MailException mailex) {
LOG.error("MailMessageTransformer.transformMime.Exception::: MailException ", mailex);

}

catch (Exception ex) {
LOG.error("MailMessageTransformer.transformMime.Exception::: Exception", ex);

}

LOG.debug("MailMessageTransformer.transformMime.mimeMessage:::" + mimeMessage);
return mimeMessage;
}

}

   <int:channel id="outboundMailChannel"/>
   <int:channel id="confirmationChannel"/>



   <int:gateway id="mailGateway" service-interface="com.infra.mail.MailGateway"
      default-request-channel="xfromMailChannel" default-reply-channel="confirmationChannel" error-channel="errorChannel">
      <int:method name="sendMail" payload-expression="#args[0] + #args[1]"> </int:method>
      </int:gateway>  


   <int:transformer input-channel="xfromMailChannel" output-channel="outboundMailChannel"
      ref="mailTransformer" method="transform">
        <int:poller fixed-rate="60000" max-messages-per-poll="100"/> 
  </int:transformer>

   <!-- Configure Mail Sender -->
   <int-mail:outbound-channel-adapter channel="outboundMailChannel" mail-sender="mailSender"/>

   <bean id="mailMessage" scope="prototype">
      <property name="from" value="${mail.user2}"/>
      <property name="replyTo" value="${mail.user3}"/>
   </bean>

   <bean id="mailTransformer" class="com.infra.mail.MailMessageTransformer"/>

</bean>

  <int:service-activator id="mailErrorChannelActivator" input-channel="errorChannel" ref="errorHandler" method="handleMailError"/>  
  <bean id="errorHandler" class="com.infra.audit.ErrorHandler"/>

  <int:channel-interceptor ref="infraInterceptor" pattern="*" order="3"/>
  <bean id="infraInterceptor" class="com.infra.audit.infraInterceptor"/>


    <int:channel id="sftpErrorChannel" />

  <int:chain input-channel="xfromMailChannel" output-channel="outboundMailChannel">
<int:header-enricher>
<int:error-channel ref="errorChannel" />
</int:header-enricher>
<int:poller fixed-rate="6000" max-messages-per-poll="1"/> 
</int:chain>

**The Gateway file**
public interface MailGateway {

/**
* Send mail.
*
* @param mailDto the mail dto
*/

@Gateway
public void sendMail(MailMessageDto mailDto, MultipartFile file);

}

3 个答案:

答案 0 :(得分:2)

你在lib文件夹中添加mailapi.jar然后在为mailsend创建bean时添加xml文件

<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="yourmailid" />
        <property name="password" value="password" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.transport.protocol">smtp</prop>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
            </props>
        </property>
    </bean>  

控制器代码

@RequestMapping(value="/sendMail.do",method=RequestMethod.POST)
    public ModelAndView sendMail(@RequestParam("email")String email){
        ModelAndView model = new ModelAndView("redirect:login.do");
        List<User_master> userList = adminService.getAllUser();
        for(User_master user: userList)
        {
            String e = user.getUser_Email();
            if(e.equals(email))
            {
                String Subject="Forget Username and Password";
                String username = user.getUser_Name();
                String password = user.getUser_Password();
                String message = "User Name : "+ username + " and " +"Password : "+ password;
                SimpleMailMessage obj_email = new SimpleMailMessage();
                obj_email.setTo(email);
                obj_email.setSubject(Subject);
                obj_email.setText(message);
                mailSender.send(obj_email);
                return model;
            }
            else
            {

            }
        }
        return model;
    }

答案 1 :(得分:1)

首先:

@Gateway
public void sendMail(MailMessageDto mailDto, MultipartFile file);

第二

public MimeMessage transform(MailMessageDto mailDto, final MultipartFile file) {

请仔细阅读Spring Integration Manual

所以,你应该在这里理解Spring Integration(和任何Messaging)与Message完全一致。 Framework非常荣幸它允许我们使用POJO方法调用样式交互,但我们应该遵循简单的规则。

payload只有一个方法参数。是的,在某些地方,例如@Publisher,我们可以使用SpEL作为@Payload值来从多个参数构建payload,但通常只有其中一个可以作为payload:或者标有@Payload的标记,或者Map<String, Object>不标记为MessageHeaders的标记。

您可能不会使用@Payload对其进行标记,但是您必须对所有其他人进行类似操作 - 请使用@Header进行标记。

因此,要解决您的问题,您应该这样做:

@Gateway
public void sendMail(MailMessageDto mailDto, @Header("file") MultipartFile file);

public MimeMessage transform(MailMessageDto mailDto, @Header("file") final MultipartFile file) {

答案 2 :(得分:0)

    DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
    Calendar calobj = Calendar.getInstance();
    String date1 = df.format(calobj.getTime());
    System.out.println(df.format(calobj.getTime()));

    try {

        // Email data
        String Email_Id = "ypur email";
        String password = "your pswrd";
        String recipient_mail_id = "client email";
        String mail_subject = "Attendance Report";

        System.out.println(sentEmailFrom);
        System.out.println(emailPassword);
        System.out.println(sentEmailTo);

        // Set mail properties
        Properties props = System.getProperties();
        String host_name = "smtp.gmail.com";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host_name);
        props.put("mail.smtp.user", Email_Id);
        props.put("mail.smtp.password", password);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);

        try {
            // Set email data
            message.setFrom(new InternetAddress(Email_Id));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient_mail_id));
            message.setSubject(mail_subject);
            MimeMultipart multipart = new MimeMultipart();
            BodyPart messageBodyPart = new MimeBodyPart();
            // Set key values
            Map<String, String> input = new HashMap<String, String>();
            input.put("User", presentUser.toString());
            input.put("Date", date1);
            input.put("Content In", "English");
            input.put("Absent", absentUser);
            input.put("Late", lateUser);
            // HTML mail content
            ClassLoader loader = SendMailService.class.getClassLoader();
            URL url = loader.getResource("mailTemplate.html");
            String path = url.toString().replace("file:", "");
            System.out.println(path);
            String htmlText = readEmailFromHtml(path, input);
            messageBodyPart.setContent(htmlText, "text/html");
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
            // Conect to smtp server and send Email
            Transport transport = session.getTransport("smtp");
            transport.connect(host_name, Email_Id, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            log.info("Mail Sent Successfully..!");
        } catch (MessagingException ex) {
        } catch (Exception ae) {
            ae.printStackTrace();
        }
    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

// Method to replace the values for keys
protected String readEmailFromHtml(String filePath, Map<String, String> input) {
    String msg = readContentFromFile(filePath);
    try {
        Set<Entry<String, String>> entries = input.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            msg = msg.replace(entry.getKey().trim(), entry.getValue().trim());
        }
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    return msg;
}

// Method to read HTML file as a String
private String readContentFromFile(String fileName) {
    StringBuffer contents = new StringBuffer();

    try {
        // use buffering, reading one line at a time
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        try {
            String line = null;
            while ((line = reader.readLine()) != null) {
                contents.append(line);
                contents.append(System.getProperty("line.separator"));
            }
        } finally {
            reader.close();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return contents.toString();
}

}