Reputation: 3429
I am trying to send an email with javamail api, and then copy it to sent folder.
I am using yahoo mail.
I can send the email, but the copy to sent folder doesn't work.
Here is the code to copy to sent folder :
private void copyIntoSent(Session session,Message msg) throws MessagingException{
Store store = session.getStore("imap");
store.connect("imap.mail.yahoo.com", SMTP_AUTH_USER, SMTP_AUTH_PWD);
Folder folder = (Folder) store.getFolder("Inbox.Sent.Notifications");
if (!folder.exists()) {
folder.create(Folder.HOLDS_MESSAGES);
}
folder.open(Folder.READ_WRITE);
folder.appendMessages(new Message[]{msg});
}
I am getting this exception :
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: imap.mail.yahoo.com, 143; timeout -1; nested exception is: java.net.ConnectException: Connexion terminée par expiration du délai d'attente
I don't know if the problem only comes from my settings of IMAP or if the method to copy to sent folder is wrong.
Thanks in advance.
EDIT : send email code :
Properties properties = System.getProperties();
// Setup mail server
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.user", from);
properties.put("mail.smtp.password", pass);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
String filename = "SendAttachment.java";//change accordingly
File f = new File("/path_to_file/test.pdf");
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(f);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);
// Send message
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
copyIntoSent(session, message);
transport.close();
Upvotes: 1
Views: 6468
Reputation: 29971
You need to use SSL with IMAP for Yahoo. Set "mail.imap.ssl.enable" to "true".
Also, since you're using transport.connect explicitly, you shouldn't need to set "mail.smtp.host" and "mail.smtp.user"; and there is no "mail.smtp.password" property so you don't need to set that either.
And you should probably change Sesion.getDefaultInstance to Session.getInstance.
Upvotes: 1
Reputation: 1859
From your exception it seems that your code is trying to connect to port 143 that's wrong use below settings for yahoo.
Incoming Mail (IMAP) Server
Server - imap.mail.yahoo.com
Port - 993
Requires SSL - Yes
And yes, imap allows 2-way synchronizing, which means everything you do remotely is reflected back in your Yahoo Mail account.so i don't think you externally need to move a mail to sent folder by default it will be reflected in sent mails of account
Upvotes: 0