无法将电子邮件发送到TOList,CC使用Spring Batch可以正常工作

时间:2018-08-20 12:12:21

标签: java spring email

我正在使用春季电子邮件和MIME消息发送电子邮件。将别名放入“收件人”列表时,我无法向别名发送电子邮件。如果我将相同的别名放在抄送列表中,则我的代码工作正常。我不明白那是什么问题。请输入您的意见。

private JavaMailSender mailSender;

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

    public void sendMail(String User, String login, String subject, String msg)
            throws FileNotFoundException, MessagingException {
        // creating message
        log.info("In Email now");

        String to = null, sender = null, Cc = null;
        try {

            sender = EXTUtilClass.getProperty(
                    EXTConstants.BATCH_EMAIL_FROM_LIST,
                    EXTConstants.EXT_CONFIG_PROPERTIES);

            to = EXTUtilClass.getProperty(EXTConstants.BATCH_EMAIL_TO_LIST,
                    EXTConstants.EXT_CONFIG_PROPERTIES);

            Cc = User;

        } catch (IOException e) {
            // TODO Auto-generated catch block
            log.error("Error in email with error:" + e.getMessage());
            System.out.println("Error in email with error:" + e.getMessage());
            e.printStackTrace();
        }

        // sending message
        MimeMessage MIMEmessage = mailSender.createMimeMessage();
        //MimeMessageHelper helper = null;
        try {
            //helper = new MimeMessageHelper(MIMEmessage, true);
            //helper.setFrom(sender);
            // helper.setTo(message.getTo());
            /*to.replaceAll( ";", ","); 
            helper.getMimeMessage().setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(to));

            Cc.replaceAll(";", ",");
            helper.getMimeMessage().setRecipients(Message.RecipientType.CC,
                    InternetAddress.parse(Cc));

            helper.setSubject(subject);
            helper.setText(msg, true);*/

            MIMEmessage.addFrom(InternetAddress.parse( sender ));
            to = to.replaceAll( ";", ",");  
            MIMEmessage.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(to));

            Cc = Cc.replaceAll(";", ",");
            MIMEmessage.setRecipients(Message.RecipientType.CC,
                    InternetAddress.parse(Cc));

            MIMEmessage.setSubject(subject);
            MIMEmessage.setContent(msg, "text/html; charset=utf-8");
            //MIMEmessage.setText(msg);


        } catch (MessagingException e) {

            MIMEmessage.setSubject(subject);
            StringBuilder WrongEmailmsg = new StringBuilder(msg);
            WrongEmailmsg.insert(msg.indexOf("Note"),
                    "<b>Error: Email address of " + login
                            + " is not correct.</b><br><br>");
            MIMEmessage.setText(msg);

            log.info("Error in email with error:" + e.getMessage());
            log.error("Error in email with error:" + e.getMessage());
            System.out.println("Error in email with error:" + e.getMessage());
        }

        mailSender.send(MIMEmessage);

        log.info("Email sent successfully.");
        System.out.println("Email sent successfully.");

    }

XMl文件:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="#{configProperties['host.name']}" />
        <property name="javaMailProperties">
              <props>
                    <prop key="mail.transport.protocol">smtp</prop>
                    <prop key="mail.smtp.auth">false</prop>
                    <prop key="mail.smtp.starttls.enable">false</prop>
                    <prop key="mail.debug">true</prop>

              </props>
        </property>
     </bean> 

例如在cc中,如果我使用的是group_mails.gmail.com,而group_mails包含“ xyz.gmail.com.abc.gmail.com”。然后这段代码对我来说很好用,但是如果我使用的是to = group_mails.com,那么这段代码将无法正常工作。我找不到我犯错的地方。

0 个答案:

没有答案