I am facing an issue with the javamail.setFrom() method in my Spring application

When using Java mail, the user is trying to implement the functionality of receiving inquiries via email. However, there seems to be an issue with the setFrom() function where the authenticated mail address overrides the email address that is being set by the user.

Even when the user inputs their own email address and sets it in the setFrom(), it keeps getting overwritten every time.

I have attempted passing parameter values as a string, but unfortunately, it did not resolve the issue. The code for sending emails utilizes ajax.

context-mail.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="email" />
    <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>

Controller

@RequestMapping(value = "/mail")
public void mailSend(HttpServletRequest request, HttpServletResponse response,
        ModelMap model, @RequestParam Map param) throws Exception {
    SessionVO session = getSessionVO(request, response);

    final Map<String, String> qna = (Map<String, String>)((List)qnaService.selectQna(param).get("qnaResultList")).get(0);
    final List<Map> qnaFile= qnaService.selectQnaFile(param);
    final Map[] file = new HashMap[qnaFile.size()];

    for(int i=0;i<qnaFile.size();i++){                  
        FileVO vo=new FileVO();
        vo.setFileId((String)qnaFile.get(i).get("fileId"));
        file[i]=fileService.getimgfile(vo, response);
    }

    logger.info(qna+"");
    logger.info(qnaFile+"");


    final MimeMessagePreparator preparator = new MimeMessagePreparator() { 
        @Override 
        public void prepare(MimeMessage mimeMessage) throws Exception { 
            final MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            helper.setSentDate(new Date());
            helper.setFrom(qna.get("email"), qna.get("userName")); //
            helper.setTo(new InternetAddress("auth email"));
            helper.setSubject(qna.get("userName")+"-"+qna.get("qnaType"));
            helper.setText(qna.get("content"), true);

            for (Map m : file) {
                helper.addAttachment((String)m.get("fileNm"), (File)m.get("file"));                 
            }
        }
    };

    mailSender.send(preparator);
}

Although no errors are thrown, there seems to be an incorrect sender's email address appearing.

Answer №1

Typically, Gmail (as well as many other mail servers) restricts the usage of a From address to only those that match your account credentials. If you wish to utilize an alternate From address, refer to this Gmail support page.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The specified type 'ReturnType' mandates one type argument. Error code: ts(2314)

After transitioning from Flow to Typescript, I have encountered errors while converting some of the codebase. Most of the issues have been resolved using the Utility-Types package, but I am stuck with the code below without any helpful documentation or ans ...

Contrast and remove the elements within jQuery array objects

As part of my data collection process, I am creating arrays to store lists of users for each request. Here is the code snippet that shows how I populate these arrays: $.each(data, function (i, item) { jQuery.each(array, function (index, data) { ...

What is the reason for utilizing the object name in the object's method rather than using "this"?

Looking at the code snippet above, you can see that store.nextId and store.cache are used in the add method. It makes me wonder why not use this instead? var store = { nextId: 1, cache: {}, add: function(fn) { if (!fn.id) { fn.id = this. ...

What is the reason behind WP AJAX consistently returning a value of 0?

Having trouble retrieving a proper response, as it always returns 0. Here is the JavaScript code in the head section: q = new XMLHttpRequest(); q.open('POST', ajaxUrl); q.onreadystatechange = function () { if (q.readyState === 4) { ...

Having trouble executing the cucumber project as a Java application

After setting up a Cucumber project in Eclipse IDE with Maven Build and exporting it as a .jar file, I have declared a main method to call the feature file using cucumber.api.cli.Main. Here is an example of my main method: public class NewMain { publ ...

Tips for receiving a reply from a post request

After successfully making and completing a post request, I'm wondering about using console.log(res.valor) in the code to display the data: consultarCorreio(){ this.cartS.cart.valorTotalItems this.cartS.getCorreiosPrazoPreco(this.cartS.cart.pesoTot ...

Exploring the dynamic duo of MongoDB and GridFS

We currently manage a large-scale project that accommodates thousands of users daily. Our database system is MySQL, but we are considering transitioning to MongoDB along with GridFS. Is it feasible to utilize MongoDB and GridFS for projects on this scale? ...

Elements recognized worldwide, Typescript, and a glitch specific to Safari?

Consider a scenario where you have a select element structured like this: <select id="Stooge" name="Stooge"> <option value="0">Moe</option> <option value="1">Larry</option> <option value="2">Curly</option ...

Click on the nearest Details element to reveal its content

Is there a way to create a button that can open the nearest details element (located above the button) without relying on an ID? I've experimented with different versions of the code below and scoured through various discussions, but I haven't be ...

The background appears only in the upper portion of the screen upon refreshing the table

Whenever the user clicks on the edit button or when the page is initially loading, a backdrop appears and disappears once the modal is displayed. The backdrop effectively highlights the entire page. However, after updating my table with data via Ajax witho ...

Setting a class in AngularJS when there is a change in ng-repeat with pagination

I am currently attempting to assign a class to an item in a paginated list by simulating a click on the element within the directive's link property: link : function (scope, element, attrs) { element.bind('click', function () { ...

Determine the most efficient Arima(p, d, q) value automatically

Currently in the process of designing autonomous prediction software using both JAVA & R. This cutting-edge system employs the following set of strategies in R to predict the next 18 data points: trends <- scan("c:/data_for_R/trends.dat") auto.arima( ...

What causes ngClick to stop working following $compile?

http://plnkr.co/edit/kL2uLPQu2vHHKIvRuLPp?p=preview Upon button click, the controller calls a service to compile HTML and inject it into the body. The compiled HTML (displaying "Hello World" from $scope.name) is referring to the scope of the controller, ...

Angular 2 dropdown list that allows users to add a personalized value in the HTML code

Here is the scenario I am dealing with We are displaying a fixed dropdown list to the user For example, a dropdown list has 4 options such as apple, orange, grape, pineapple and 'create your own' If the user is not satisfied with the provided ...

The updates made to data in Vue.js are not reflecting on the screen

<template> <div class="parent"> <div class="container"> <h1 class="start" @click="start_timer"> {{ timerText }} </h1> </div ...

Integrate PHP code into a div using Javascript and Ajax without losing any previous inclusions

Having issues with my navigation bar and elements. <ul><li> The element in question is: <li> My elements are linked via ajax to a separate php function file: switch ($_GET['menu']){} The functions are separated on a php fi ...

Specialized Node.js extension for automatic dependency installation

My current setup involves a custom Yeoman generator for specific applications, which comes with its own set of dependencies and configurations. - GruntJS must be installed globally; - Bower must be installed globally; - Yeoman must be installed globally ...

What is the process by which Oracle HRMS retrieves the complete description of a job posting?

With my limited understanding of JavaScript, I noticed that the HRMS refreshes the page and displays the job details under the same URL as the job list page. I expected to find job detail information in the network requests, but upon inspecting them, I dis ...

Tips for refreshing a live ajax call to animate a circle

Currently, I am utilizing the gaugeMeter plugin to dynamically display the CPU usage of my elasticSearch Host. While the values are being displayed correctly, there seems to be an issue with the animation of the gauge/circle itself. Instead of the indicato ...

Strategies for smoothly navigating the page to a specific div every time

Currently, I am working on a form that requires submitting multiple child forms. To enhance user experience, I have incorporated jQuery functionality to display a message at the top of the page upon each submission. Now, my goal is to implement a feature w ...