简体   繁体   中英

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 that will send an email to a specific inbox with an excel report attachment. I have the function:

public void sendEmail(String to, String from, String subject, String body)
{

}

I am trying to use Spring, and I'm trying to stick to xml configuration in the appcontext file for now instead of annotations (for learning purposes). I want to inject a static resource which is an excel file, and for learning purposes for this module I am avoiding using FileSystemResource for the attachment per my mentor/teacher. I also don't need the body to say anything. The subject line will be "Report" for dummy purposes. Here is what I have so far, just need the meat of the actual email function that's needed so I could pass the parameters of sendEmail by reference in the main class:

public class SendEmail 
{
    private JavaMailSender mailSender;

    public SendEmail(JavaMailSender ms)
    {
        this.mailSender = ms;
    }

    public void sendEmail(String from, String to, String Subject, String body)
    {

        MimeMessage message = mailSender.createMimeMessage();

        try
        {
            MimeMessageHelper helper = new MimeMessageHelper(message);
            helper.setTo("whatever@xyz.com");
            helper.setText("Test email!");

            mailSender.send(message);
        }

        catch (MessagingException e)
        {
            throw new MailParseException(e);
        }
    }

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

This is the applicationContext.xml code:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation=`
    "http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:component-scan base-package="com.transportation"/>

    <bean id = "mailSender" class = "org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name = "host" value = "Whatever" />
        <property name = "port" value = "25" />
    </bean>

    <bean id = "sendEmail" class = "com.transportation.email.util.SendEmail">
        <constructor-arg ref="mailSender"/>
    </bean>

</beans>

Try this one.

public void sendMail(final String messageStr, final boolean isHtml) throws MessagingException {

        final MimeMessage message = mailSender.createMimeMessage();
        final MimeMessageHelper helper = 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", ResourceUtil.loadResourceAsFileSystemResource("NameOfresource"));
        mailSender.send(message);
    }


public static FileSystemResource loadResourceAsFileSystemResource(final String fileRoute) {

        File file = null;
        FileSystemResource fileSystemResource;
        try {
            file = new ClassPathResource(fileRoute).getFile();
            fileSystemResource = new FileSystemResource(file);
        }
        catch (final IOException e) {
            fileSystemResource = null;
        }

        return fileSystemResource;
    }



<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.host">${mail.smtp.host}</prop>
            <prop key="mail.smtp.port">${mail.smtp.port}</prop>
        </props>
    </property>
</bean> 

<bean id="sfErrorMailSender" class="XXX.MailSender">
    <property name="mailSender" ref="mailSender" />
    <property name="simpleMailMessage" ref="sfErrorNotificationMailMessage" />
</bean>

<bean id="sfErrorNotificationMailMessage" class="org.springframework.mail.SimpleMailMessage">
    <property name="from" value="${mail.message.error.sf.to}" />
    <property name="to" value="${mail.message.error.sf.from}" />
    <property name="subject" value="${mail.message.error.sf.subject}" />
    <property name="text" value="${mail.message.error.sf.body}" />
    <property name="cc" value="${mail.message.error.sf.cc}" />
</bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM