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.