繁体   English   中英

我将如何创建用于发送电子邮件的函数,其参数设置为String from,String to,String Subject和String body?

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

我正在尝试创建一个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>

如何创建列表<object>带有字段字符串和 Map <string, set<string> &gt; 从另一个列表<object2><div id="text_translate"><p> Class Object2具有标准的 getter 并具有String字段folder 、 file和version 。 它被命名为SourceInfo</p><p> List&lt;SourceInfo&gt; source包含上面提到的三个字段。</p><p> 我的目标是从List&lt;SourceInfo&gt;创建一个List&lt;Info&gt; &gt; 。</p><p> 新List的 class 为Info ,如下图所示。</p><pre> public class Info { private final String folder; private final Map&lt;String, Set&lt;String&gt;&gt; file; public static Builder builder() { return new Builder(); } public static Builder builder(Info info) { return new Builder(info); } private Info(Builder builder) { this.folder = builder.folder; this.file = builder.file; } public String getFolder() { return folder; } public Map&lt;String, Set&lt;String&gt;&gt; getFile() { return file; } // autogenerated toString, hashCode, and equals public static class Builder { private String folder; private Map&lt;String, Set&lt;String&gt;&gt; file; private Builder() {} private Builder(Info info) { this.folder = info.folder; this.file = info.file; } public Builder with(Consumer&lt;Builder&gt; consumer) { consumer.accept(this); return this; } public Builder withFolder(String folder) { this.folder = folder; return this; } public Builder withFile(Map&lt;String, Set&lt;String&gt;&gt; file) { this.file = file; return this; } public Info build() { return new Info(this); } }</pre><p> 到目前为止,我尝试的是在构建器模式中创建一个集合。</p><pre> List&lt;SourceInfo&gt; source; // error: gc overhead limit exceeded List&lt;Info&gt; infoList = source.stream().map(e -&gt; Info.builder().withFolder(e.getFolder()).withFile(source.stream().collect(Collectors.groupingBy(SourceInfo::getKey, Collectors.mapping(SourceInfo::getVersion, Collectors.toSet())))).build()).collect(Collectors.toList()); Map&lt;String, Set&lt;String&gt;&gt; map = source.stream().collect(Collectors.groupingBy(SourceInfo::getKey, Collectors.mapping(SourceInfo::getVersion, Collectors.toSet()))); List&lt;Info&gt; info = source.stream().map(e -&gt; Info.builder().withFolder(e.getFolder()).withFile(map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))).build()).collect(Collectors.toList());</pre><p> 所需的 output。 以下语法可能已关闭。</p><pre> // [String, Map&lt;String, Set&lt;String&gt;&gt;] Info [folder, [key=file [value=version]]]...</pre><p> 我是 Java 的新手,不胜感激。</p><p> 我想了解如何使用 java8 和 for 循环来做到这一点。</p><p> 谢谢你。</p></div></object2></string,></object>

[英]How to create a List<Object> with fields String and Map<String, Set<String>> from another List<Object2>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过从列表中获取参数来创建带有 String.format 的字符串 如何使用String class&gt;从字符串中删除电子邮件 我将如何分割此字符串? 如何从字符串中拆分电子邮件 如何创建使用String类型行为进行创建的对象? 如何创建一个反转给定字符串的迭代器? 如何使用 Java 中的字符数组创建字符串? 您将如何以编程方式从存储在字符串中的日期创建模式? 如果字符串是值或单词,我将如何为字符串创建一个“if”函数是否运行特定函数? 如何创建列表<object>带有字段字符串和 Map <string, set<string> &gt; 从另一个列表<object2><div id="text_translate"><p> Class Object2具有标准的 getter 并具有String字段folder 、 file和version 。 它被命名为SourceInfo</p><p> List&lt;SourceInfo&gt; source包含上面提到的三个字段。</p><p> 我的目标是从List&lt;SourceInfo&gt;创建一个List&lt;Info&gt; &gt; 。</p><p> 新List的 class 为Info ,如下图所示。</p><pre> public class Info { private final String folder; private final Map&lt;String, Set&lt;String&gt;&gt; file; public static Builder builder() { return new Builder(); } public static Builder builder(Info info) { return new Builder(info); } private Info(Builder builder) { this.folder = builder.folder; this.file = builder.file; } public String getFolder() { return folder; } public Map&lt;String, Set&lt;String&gt;&gt; getFile() { return file; } // autogenerated toString, hashCode, and equals public static class Builder { private String folder; private Map&lt;String, Set&lt;String&gt;&gt; file; private Builder() {} private Builder(Info info) { this.folder = info.folder; this.file = info.file; } public Builder with(Consumer&lt;Builder&gt; consumer) { consumer.accept(this); return this; } public Builder withFolder(String folder) { this.folder = folder; return this; } public Builder withFile(Map&lt;String, Set&lt;String&gt;&gt; file) { this.file = file; return this; } public Info build() { return new Info(this); } }</pre><p> 到目前为止,我尝试的是在构建器模式中创建一个集合。</p><pre> List&lt;SourceInfo&gt; source; // error: gc overhead limit exceeded List&lt;Info&gt; infoList = source.stream().map(e -&gt; Info.builder().withFolder(e.getFolder()).withFile(source.stream().collect(Collectors.groupingBy(SourceInfo::getKey, Collectors.mapping(SourceInfo::getVersion, Collectors.toSet())))).build()).collect(Collectors.toList()); Map&lt;String, Set&lt;String&gt;&gt; map = source.stream().collect(Collectors.groupingBy(SourceInfo::getKey, Collectors.mapping(SourceInfo::getVersion, Collectors.toSet()))); List&lt;Info&gt; info = source.stream().map(e -&gt; Info.builder().withFolder(e.getFolder()).withFile(map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))).build()).collect(Collectors.toList());</pre><p> 所需的 output。 以下语法可能已关闭。</p><pre> // [String, Map&lt;String, Set&lt;String&gt;&gt;] Info [folder, [key=file [value=version]]]...</pre><p> 我是 Java 的新手,不胜感激。</p><p> 我想了解如何使用 java8 和 for 循环来做到这一点。</p><p> 谢谢你。</p></div></object2></string,></object>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM