Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.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
Javamail API如何在sent中保存消息_Java_Automation_Jakarta Mail - Fatal编程技术网

Javamail API如何在sent中保存消息

Javamail API如何在sent中保存消息,java,automation,jakarta-mail,Java,Automation,Jakarta Mail,我正在尝试从acc1向acc2发送电子邮件。代码似乎正在运行,我可以在acc2上找到我的邮件。但我在acc1上的“已发送”文件夹中找不到它。(我以yandex为例) 我尝试使用imap协议并在之后保存它,但代码完成时没有例外,但消息仍然无法在sent中找到 我的代码: public class sendMail { public static void main(String[] args) { String to = "TO_ACC"; // sende

我正在尝试从acc1向acc2发送电子邮件。代码似乎正在运行,我可以在acc2上找到我的邮件。但我在acc1上的“已发送”文件夹中找不到它。(我以yandex为例)

我尝试使用imap协议并在之后保存它,但代码完成时没有例外,但消息仍然无法在sent中找到

我的代码:

public class sendMail {
    public static void main(String[] args) {
        String to = "TO_ACC";         // sender email
        String from = "FROM_ACC";       // receiver email

        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", "smtp.yandex.ru");
        properties.setProperty("mail.smtp.port", "465");
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.smtp.socketFactory.port", "465");
        properties.setProperty("mail.smtp.auth", "true");

        Session session = Session.getInstance(properties,new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("FROM_ACC","FROM_PASS");
            }
        }); // default session

        try {
            MimeMessage message = new MimeMessage(session); // email message

            message.setFrom(new InternetAddress(from)); // setting header fields

            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.addRecipient(Message.RecipientType.CC, new InternetAddress(to));
            message.addRecipient(Message.RecipientType.BCC, new InternetAddress(to));

            message.setSubject("new hjhjh"); // subject line

            // actual mail body
            message.setText("some kjkjk");

            // Send message
            Transport.send(message); System.out.println("Email Sent successfully....");

            //Copy in sent

            Store store = session.getStore("imap");

            store.connect("imap.yandex.ru",993,"FROM_ACC","FROM_PASS" );
            Folder folder = store.getFolder("Sent");
            folder.open(Folder.READ_WRITE);
            message.setFlag(Flags.Flag.SEEN, true);
            folder.appendMessages(new Message[] {message});
            store.close();
        } catch (MessagingException mex){ mex.printStackTrace(); }
    }

刚刚从
Store Store=session.getStore(“imap”)更改为
to
Store Store=session.getStore(“imaps”)

还添加了一个新属性

 properties.setProperty("mail.imap.starttls.enable", "true");

请发布,并修复这些。调试SMTP:邮件已成功传递到邮件服务器退出221 2.0.0关闭连接。电子邮件已成功发送。。。。调试:getProvider()返回javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]调试IMAP:mail.IMAP.fetchsize:16384调试IMAP:mail.IMAP.ignorebodystructuresize:false调试IMAP:mail.IMAP.statuscachetimeout:1000调试IMAP:mail.IMAP.appendbuffersize:-1调试IMAP:mail.IMAP.minidletime:10调试IMAP:CloseFoldersStoreFailure调试IMAP:尝试连接到主机“IMAP.yandex.ru”,端口993,isSSL false常见错误已修复,因此我修复了IMAP问题:尝试连接到主机“IMAP.yandex.ru”,端口993,isSSL更改为Store Store=session.getStore(“imaps”);和新属性properties.setProperty(“mail.imap.starttls.enable”、“true”);由于您使用的是“imaps”协议(使用SSL进行连接),因此将忽略所有mail.imap.*属性。如果要使用STARTTLS,而不是从一开始就使用SSL连接,请保留该属性并切换回使用“imap”协议。