簡體   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