如何复制已发送文件夹中的电子邮件?

时间:2019-04-19 08:08:54

标签: spring-boot javamail

我正在使用Java邮件API发送邮件。在发送电子邮件中,我配置了yahoo smtp端口。我能够从yahoo帐户发送邮件,但已发送的邮件未保存在已发送的邮件中。服务器。

服务

 @Component
    public class SmtpMailSender {   
        @Autowired
        private JavaMailSender javaMailSender;
        private static String folderName = "Sent";

        private String host="smtp.mail.yahoo.com";
        private String user="abc@yahoo.com";
        private String pass="xxxx";

        public void send(String to,String subject,String body, String from) throws MessagingException
        {
            // Java Mail properties
            Properties props = System.getProperties();
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.auth", "true");

            // Mail session authentified
            Session session = Session.getInstance(props);

            MimeMessage message=javaMailSender.createMimeMessage();
            MimeMessageHelper helper=new MimeMessageHelper(message,true);

            helper.setTo(to); 
            helper.setFrom(from);
            helper.setSubject(subject);  
            helper.setText(body,true);
            javaMailSender.send(message);

            // Copy message to "Sent Items" folder as read
            Store store = session.getStore();
            store.connect("imap.mail.yahoo.com", user, pass);

            Folder folder = store.getFolder(folderName);
            if (!folder.exists()) {
                folder.create(Folder.HOLDS_MESSAGES);
            }
            folder.open(Folder.READ_WRITE);
            folder.appendMessages(new Message[] {message});
            message.setFlag(FLAGS.Flag.RECENT, true);
            System.out.println("Msg send and saved ....");
            store.close();
        }
    }

控制器:

 @RestController
    public class EmailController {
        @Autowired private SmtpMailSender smtpMailSenderObj;
        @RequestMapping("/send")
        public void sendMail() throws MessagingException {
            smtpMailSenderObj
                .send
                    ("pqr@gmail.com", "verify sendMail",
                    "Hii...this is demo for java email send",
                            "abc@yahoo.com");
            }
    }

Application.properties:

spring.mail.host=smtp.mail.yahoo.com
spring.mail.username=abc@yahoo.com
spring.mail.password=xxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.transport.protocol : smtp
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.debug=true
spring.mail.properties.mail.smtp.socketfactory.port=465
spring.mail.properties.mail.imap.ssl.required=true
spring.mail.properties.mail.imap.port=993

2 个答案:

答案 0 :(得分:0)

您需要明确append the message object到“已发送”文件夹。

答案 1 :(得分:0)

这是因为imap端口连接所发送的邮件未保存在已发送的邮件中。 需要在属性文件中添加set属性。

function findMinSum(arr, n){
    if(!arr) return 
    let min 
    for (let i=0; i<arr.length; i++) {

        /* if a number equals the sum, it's obviously
         * the shortest set, just return it
         */
        if (arr[i] == n) return [arr[i]]     
        
        /* recursively call on subset with
         * sum adjusted for removed element 
         */
        let next = findMinSum(arr.slice(i+1), n-arr[i])
        
        /* we only care about next if it's shorter then 
         * the shortest thing we've seen so far
         */
        if (next){
            if(min === undefined || next.length < min.length){
                min = [arr[i], ...next]
            }
        }
    }
    return min && min  /* if we found a match return it, otherwise return undefined */
}

console.log(findMinSum([10, 0, -1, 20, 25, 30], 59).join(', '))
console.log(findMinSum([10, 0, -1, 20, 25, 30], 29).join(', '))
console.log(findMinSum([10, 0, -1, 20, 25, 30], -5)) // undefined when no sum

现在添加此内容,便可以将已发送的邮件保存在已发送的邮件中。