简体   繁体   English

使用Java Mail API从Outlook 2010发送邮件

[英]Send mail from outlook 2010 using java mail api

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="username@domain.com";//change accordingly  
        String to="username@domain.com";//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. 但是问题是我无法在我的Outlook中看到已发送的邮件。 On analysis i found out that java mail api sends the mail directly from the smtp server. 经过分析,我发现java邮件api直接从smtp服务器发送邮件。 But i want the mail to send from my outlook profile ie i should be able to see it in my sent mail folder. 但是我希望邮件从我的Outlook配置文件发送,即我应该能够在我的已发送邮件文件夹中看到它。 How should i do it? 我该怎么办? What api or 3rd party open source library can be used to achieve this? 可以使用什么api或3rd party开源库来实现此目的?

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 });

Try this to store mail into sentbox for Outlook. 尝试将邮件存储到Outlook的sendbox中。

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();

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM