使用 java mail api 从 Outlook 2010 发送邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21681620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:03:47  来源:igfitidea点击:

Send mail from outlook 2010 using java mail api

javaemailoutlookjavamailoutlook-2010

提问by Mrunal Gosar

Hi i am trying to send an email from outlook 2010 with the help of below code.

嗨,我正在尝试借助以下代码从 Outlook 2010 发送电子邮件。

package javamail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JavaMailTest {
    public static void main(String[] args) {
        String host="host";  
        final String user="[email protected]";//change accordingly  
        String to="[email protected]";//change accordingly  

        //Get the session object  
        Properties props = new Properties();  
        props.put("mail.smtp.host",host);  
        props.put("mail.smtp.auth", "false");

        Session session=Session.getDefaultInstance(props, null);
        session.setDebug(true);

        //Compose the message  
        try {
            MimeMessage message = new MimeMessage(session);
            message.saveChanges();
            message.setFrom(new InternetAddress(user));  
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));  
            message.setSubject("Test mail");  
            message.setText("This is test mail.");  

            //send the message
            Transport.send(message);

            System.out.println("message sent successfully...");
        }
        catch (MessagingException e) {e.printStackTrace();}

    }
}

The above code works properly and i am able to send the mail(after my tech admin enabled relaying on server). But the problem is i am not able to see the sent mail in my outlook. On analysis i found out that java mail api sends the mail directly from the smtp server. But i want the mail to send from my outlook profile i.e i should be able to see it in my sent mail folder. How should i do it? What api or 3rd party open source library can be used to achieve this?

上面的代码工作正常,我可以发送邮件(在我的技术管理员启用服务器中继后)。但问题是我无法在 Outlook 中看到已发送的邮件。经过分析,我发现 java mail api 直接从 smtp 服务器发送邮件。但我希望邮件从我的 Outlook 配置文件发送,即我应该能够在我发送的邮件文件夹中看到它。我该怎么做?可以使用什么 api 或 3rd 方开源库来实现这一点?

回答by Bill Shannon

If you want the message to be copied to your Sent folder as well as being sent, you need to explicitly copy it there.

如果您希望将邮件复制到您的已发送文件夹以及发送,您需要明确地将其复制到那里。

Transport.send(msg);
Folder sent = store.getFolder("Sent");
sent.appendMessages(new Message[] { msg });

回答by user4719223

Getting below error while running your code .

运行代码时出现以下错误。

com.sun.mail.util.MailConnectException:

 Couldn't connect to host, port: host, 25; timeout -1;
 nested exception is:   java.net.UnknownHostException: host

回答by Raushan Prabhakar

Try this. It's working for me for Outlook.

尝试这个。它对我的 Outlook 有用。

String host = "outlook.office365.com"; 
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);       //     mail server host
props.put("mail.smtp.port", "587");      // port

回答by Payal Sharma

Try this to store mail into sentbox for Outlook.

尝试将邮件存储到 Outlook 的发送箱中。

Store store = session.getStore("imaps");
store.connect("imap-mail.outlook.com", "username", "password");
Folder folder = store.getFolder("Sent Items");
folder.open(Folder.READ_WRITE);  
message.setFlag(Flag.SEEN, true);  
folder.appendMessages(new Message[] {message});  
store.close();