java - How would I create the function for sending an email with the parameters set as String from, String to, String Subject, and String body? -


I am trying to create a Java batch email program which send an email to a specific inbox with the Excel report attachment Will give I have the function:

  Send Public Zero Email (String To String, String Subject, String Body) {}   

I try to use Spring And I'm trying to leave XML configuration in an incognito file instead of encoding (for learning purposes). I want to inject a static resource which is an Excel file, and I'm avoiding using FileSystemResource for attachment to my mentor / teacher to learn this module. I do not have to say anything to the body. For the Dummy Purposes the subject line will be "Report" even if I have so far, the actual email function needs the meat which is necessary so that I can pass the parameters of sending e-mail by reference in the main category :

  Public class SendEmail {Private Java MailSender mailSender; Public SendEmail (JavaMailSender MS) {this.mailSender = ms; } Public Zero Send e-mail (string to string to string subject, string body) {Mime Message Message = MailSender.CreditMem Message (); {MimeMessageHelper Assistant = Try New MimeMessageHelper (Message); helper.setTo ("whatever@xyz.com"); helper.setText ("Test Email!"); mailSender.send (message); } Catch (Messaging Up E) {New Mailer Aspen (E); }} Public Zero Set-MailSender (JavaMessenger MailSendor) {this.mailSender = mailSender; }}   

This is the applicationContext.xml code:

   
ID =" MailSender "class =" org.springframework.mail.javamail.JavaMailSenderImpl "& gt; & lt; property name =" host "value =" whatever "/> & Lt; Property Name = "Port" value = "25" /> gt; bean & gt; & lt; / bean & gt;

Try it out.

  Public Zero sendMail (last string message, last boolean is HTML) Throws messaging expeditions {Last Mime Message Message = MailSender.CretemMem Message (); Last MimeMessageHelper Assistant = New MimeMessageHelper (message, true); helper.setFrom (simpleMailMessage.getFrom ()); Helper.setTo (simpleMailMessage.getTo ()); Helper.setcc (simpleMailMessage.getCc ()); Helper.setSubject (simpleMailMessage.getSubject ()); helper.setText (messageStr, isHtml); Helper.addInline ("myFile", Resources Util.loadResourceAsFileSystemResource ("NameOfresource")); mailSender.send (message); } Public static FileSystemResource loadResourceAsFileSystemResource (final string fileRoute) {file file = zero; FileSystemResource fileSystemResource; Try {File = New ClassPath Resource (fileRoute) .getFile (); fileSystemResource = new FileSystemResource (file); } Hold (last IOException e) {fileSystemResource = null; } Return File System Resource; } & Lt; Bean id = "MailSender" class = "org.springframework.mail.javamail.JavaMailSenderImpl" & gt; & Lt; Property Name = "javaMailProperties" & gt; & Lt; Theater Content & gt; & Lt; prop key = "mail.smtp.host" & gt; $ {Mail.smtp.host} & lt; / Prop & gt; & Lt; prop key = "mail.smtp.port" & gt; $ {Mail.smtp.port} & lt; / Prop & gt; & Lt; / Theater & gt; & Lt; / Property & gt; & Lt; / Bean & gt; & Lt; Bean id = "sfErrorMailSender" class = "XXX.MailSender" & gt; & Lt; Property Name = "MailSender" Riff = "MailSender" /> & Lt; Property Name = "Simple Mail Message" ref = "sfErrorNotificationMailMessage" /> & Lt; / Bean & gt; & Lt; Bean id = "sfErrorNotificationMailMessage" class = "org.springframework.mail.SimpleMailMessage" & gt; & Lt; Property name = "to" value = "$ {mail.message.error.sf.to}" /> & Lt; Property name = "to" value = "$ {mail.message.error.sf.from}" /> & Lt; Property name = "topic" value = "$ {mail.message.error.sf.subject}" /> & Lt; Property Name = "Text" value = "$ {mail.message.error.sf.body}" /> & Lt; Property Name = "CC" value = "$ {mail.message.error.sf.cc}" /> & Lt; / Bean & gt;    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -