1
votes

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

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?

4
Have you looked into any VBA/Office scripting? - admdrew
@admdrew what is VBA/Office scripting? is it visual basic related? No i do not have knowledge on above scripting language.. preferrably i would like some solution in java. - Mrunal Gosar
what is VBA/Office scripting? Go ahead and look it up! You might also be able to use standard .NET to do this as well; I would recommend just doing some additional research. - admdrew
@alexanoid: the issue i have is the mail that i've sent is not visible in my sent Items? does java outlook connector solves this problem? - Mrunal Gosar

4 Answers

1
votes

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

Try this. It's working for me for 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
1
votes

Try this to store mail into sentbox for 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();
0
votes

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