Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何创建发送电子邮件的函数,参数设置为String from、String to、String Subject和String body?_Java_Xml_Spring - Fatal编程技术网

Java 如何创建发送电子邮件的函数,参数设置为String from、String to、String Subject和String body?

Java 如何创建发送电子邮件的函数,参数设置为String from、String to、String Subject和String body?,java,xml,spring,Java,Xml,Spring,我正在尝试创建一个java批处理电子邮件程序,该程序将通过excel报告附件向特定收件箱发送电子邮件。我有以下功能: public void sendEmail(String to, String from, String subject, String body) { } 我正在尝试使用Spring,现在我正在尝试坚持appcontext文件中的xml配置,而不是注释(出于学习目的)。我想插入一个静态资源,它是一个excel文件,为了学习本模块,我避免使用FileSystemResourc

我正在尝试创建一个java批处理电子邮件程序,该程序将通过excel报告附件向特定收件箱发送电子邮件。我有以下功能:

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

}
我正在尝试使用Spring,现在我正在尝试坚持appcontext文件中的xml配置,而不是注释(出于学习目的)。我想插入一个静态资源,它是一个excel文件,为了学习本模块,我避免使用FileSystemResource作为我导师/老师的附件。我也不需要身体说什么。出于虚拟目的,主题行将为“报告”。这是我到目前为止所拥有的,只需要实际电子邮件功能的具体内容,这样我就可以在主类中通过引用传递sendEmail的参数:

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;
    }
}
这是applicationContext.xml代码:

<?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>

试试这个

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>
public void sendMail(最终字符串messageStr,最终布尔值isHtml)抛出MessaginException{
final MimeMessage message=mailssender.createMimeMessage();
final mimessagehelper=新的mimessagehelper(消息,true);
setFrom(simpleEmailMessage.getFrom());
setTo(simpleEmailMessage.getTo());
setCc(simpleEmailMessage.getCc());
setSubject(simpleEmailMessage.getSubject());
setText(messageStr,isHtml);
helper.addInline(“myFile”,ResourceUtil.loadResourceAsFileSystemResource(“NameOfresource”);
发送(消息);
}
公共静态文件系统资源loadResourceAsFileSystemResource(最终字符串文件路由){
File=null;
文件系统资源文件系统资源;
试一试{
file=新类路径资源(fileRoute).getFile();
fileSystemResource=新的fileSystemResource(文件);
}
捕获(最终IOE例外){
fileSystemResource=null;
}
返回文件系统资源;
}
${mail.smtp.host}
${mail.smtp.port}