spring mvc发送时尚和格式化的电子邮件给接收器

时间:2017-03-21 08:26:27

标签: html css spring

这是我的EmailController.java文件,我想将时尚和格式化的消息发送给接收者。这是我的代码。请提供解决方案。

EmailController.java

package org.convey.exammple.controller;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.convey.example.email.EmailSender;
import org.convey.example.model.EmailMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java.util.Map;



/**
 * Handles requests for the application home page.
 */
@Controller
public class EmailController {

     ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("emailConfiguration.xml");
        EmailSender emailSender=(EmailSender)context.getBean("emailSenderBean");


        final static Logger logger = LoggerFactory.getLogger(EmailController.class);


        @RequestMapping(value="/emailForm",method= RequestMethod.GET)
        public ModelAndView displayEmailForm(Map<String, Object> map){

            logger.debug(" setting up the Email form ");

            ModelAndView view=new ModelAndView("EmailFormView");

            //setting up the  required parameter value in the request scope for CommandName parameter
            map.put("email", new EmailMessage());

            return view;

        }


//login configuration
        @RequestMapping(value="/sendEmail",method= RequestMethod.POST)
        public ModelAndView sendEmailUsingGmail(@ModelAttribute("email")EmailMessage email){


            logger.debug(" ********************* ready to send the email **********************");
            logger.debug(" receiver email address [{}]", email.getReceiverEmailAddress());
            logger.debug(" email subject [{}]", email.getSubject());
            logger.debug(" email body [{}]", email.getMessageBody());

            ModelAndView view=new ModelAndView("EmailView");

            view.addObject("emailAddress",email.getReceiverEmailAddress());
            emailSender.sendEmail(email);
            logger.debug(" ********************* email was sent **********************");

            return view;
    }

}

这是EmailSender.java文件

package org.convey.example.email;
import org.convey.example.model.EmailMessage;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

/**
 * $LastChangedDate:  $
 * $LastChangedBy:  $
 * $LastChangedRevision:  $
 */


public class EmailSender {
      private MailSender mailSender;

        public void setMailSender(MailSender mailSender) {
            this.mailSender = mailSender;
        }


        public void sendEmail(EmailMessage emailMessage){

            SimpleMailMessage message = new SimpleMailMessage();

            message.setTo(emailMessage.getReceiverEmailAddress());
            message.setSubject(emailMessage.getSubject());
            message.setText(emailMessage.getMessageBody());
            //sending the message
            mailSender.send(message);

        }

}

这是我的EmailMessage.java文件

package org.convey.example.model;
import org.springframework.stereotype.Component;

/**
 * $LastChangedDate:  $
 * $LastChangedBy:  $
 * $LastChangedRevision:  $
 */
@Component

public class EmailMessage {
     private String receiverEmailAddress;
        private String subject;
        private String messageBody;


        public void setMessageBody(String messageBody){

            this.messageBody=messageBody;
        }

        public String getMessageBody(){

            return this.messageBody;
        }

        public void setReceiverEmailAddress(String receiverEmailAddress){

            this.receiverEmailAddress=receiverEmailAddress;
        }

        public String getReceiverEmailAddress(){

            return this.receiverEmailAddress;
        }


        public void setSubject(String subject) {

            this.subject=subject;
        }

        public String getSubject(){

            return this.subject;
    }
}

这是我的EmailFormView.jsp文件

   <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page session="false" %>

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

    <html>
    <head><title>Email Form</title>
    </head>
    <body>

    <form:form commandName="email" method="POST" action="sendEmail">

        <p>Email Form </p>
        <br/><br/>

        Receiver Email
        <form:input path="receiverEmailAddress"/>
        <br/><br/>

        Subject
        <form:input path="subject"/>
        <br/><br/>

        Message Body
        <form:input path="messageBody"/>
        <br/><br/>

        </br>
        <input type="submit" value="Send Email" />

    </form:form>

    </body>
    </html>

<form:form commandName="email" method="POST" action="sendEmail">

    <p>Email Form </p>
    <br/><br/>

    Receiver Email
    <form:input path="receiverEmailAddress"/>
    <br/><br/>

    Subject
    <form:input path="subject"/>
    <br/><br/>



    Message Body
   <p class=abc> <form:input path="messageBody"/></p>

    </br>
    <input type="submit" value="Send Email" />

</form:form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以通过多种方式将CSS应用于HTML,外部样式表,内部样式表和内联样式。

外部样式表。

<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>`

内部样式表。

<head>
<style>
body {
    background-color: linen;
}

h1 {
    color: maroon;
    margin-left: 40px;
}
</style>
</head>

内联样式。

<h1 style="color:blue;margin-left:30px;">This is a heading</h1>

我希望这可以帮助你找到你想要的东西,很多在线教程可以帮助你进一步研究,这里有一个。

https://www.w3schools.com/css/default.asp