0

This is my EmailController.java file and i want to send stylish and formatted messaged to the receiver.This is my code.Please provide a solution.

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;
    }

}

This is EmailSender.java file

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);

        }

}

This is my EmailMessage.java file

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;
    }
}

This is my EmailFormView.jsp file

   <%@ 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>
  • just provide css inside head tag – Monis Majeed Mar 21 '17 at 08:31
  • Just like Monis said add css file or tag inside head. [Refer here for implementation detail](http://stackoverflow.com/questions/26276603/how-to-include-js-and-css-in-jsp-with-spring-mvc} – phoenix Mar 21 '17 at 09:36
  • my question is that i am going to send email using spring MVC and i want to print the message body bold and stylish means whatever i write inside the message body it will make bold..i provide the internal css but it cant work. Please help – bhushan ramse Mar 21 '17 at 10:11

1 Answers1

0

Hi there a couple of ways you can apply CSS to your HTML, External Style sheet, Internal style sheet and inline style.

External Style Sheet.

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

Internal Style Sheet.

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

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

Inline Styles.

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

I hope this helps you with what you are looking for, a lot of tutorials online that will help you research this further, here is one.

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

Jason Delaney
  • 458
  • 8
  • 21
  • I apply inline and internal css but it cant work..when i am checking my mail the msg it remains same not applying the css on message body – bhushan ramse Mar 21 '17 at 10:40
  • Hi bhushan could you please provide the code for this, are you putting your message body into paragraph tag or how is this been displayed, – Jason Delaney Mar 21 '17 at 10:46
  • you could add the bold tag to make the data bold e.g below.............This is the response – Jason Delaney Mar 21 '17 at 10:50
  • Try something like this you are not given us enough information..https://jsfiddle.net/Lfgmon7h/4/ – Jason Delaney Mar 21 '17 at 10:55
  • Message Body

    This is the response

    – Jason Delaney Mar 21 '17 at 10:57
  • The image you posted doesnt reflect the code you send, where is the inputs for the text fields add the styles to these.... – Jason Delaney Mar 21 '17 at 11:20
  • The style you have at the top is not linked to anything either... .abc{} which is referring to a class abc, add class to the values you want to change, like so

    This is a paragraph with class name abc

    – Jason Delaney Mar 21 '17 at 11:23
  • bhusahan your are not making yourself clear as to what it is you want, "please help me, I pest my code". what is that you need help with is it the styling, you seem to be editing the question a lot. – Jason Delaney Mar 22 '17 at 09:12