Using Java Mail API, after sending the letter is not displayed on the mailbox.

Here is the code

public class MailAPI { static final String ENCODING = "UTF-8"; public static void sendMessage(String emailSender, String passwordSender, String emailRecipient, String subject) throws MessagingException, UnsupportedEncodingException { String content = "Some words"; String smtpHost= "smtp.yandex.ru"; String smtpPort="465"; sendSimpleMessage (emailSender, passwordSender, emailSender, emailRecipient, content, subject, smtpPort, smtpHost); } public static void sendSimpleMessage(String login, String password, String from, String to, String content, String subject, String smtpPort, String smtpHost) throws MessagingException, UnsupportedEncodingException { Authenticator auth = new MyAuthenticator(login, password); Properties props = System.getProperties(); props.put("mail.smtp.port", smtpPort); props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.debug", "true"); props.put("mail.mime.charset", ENCODING); Session session = Session.getDefaultInstance(props, auth); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); msg.setSubject(subject); msg.setText(content); Transport.send(msg); } } 
  • Should it be? SMTP forwards the email to the recipient's mail server. And he knows nothing about the mailboxes of the sender. The box is provided by a mail program specially developed for this. When sending a letter, it saves it in a box and sends it via SMTP to the recipient's server. Like that. - Sergey

2 answers 2

SMTP should not save sent messages on the server, usually email clients are made in such a way that they themselves keep sent messages in their local database. Or some servers represent such "convenience" in addition to sending a letter. You need to send an email using smtp, and then save it in the send folder, for example using the imap protocol using the com.sun.mail.imap.IMAPFolder.addMessages () method

  • those. should we inherit from the IMAPFolder class, then go to the correct folder and add the sent message using the addMessage () method? - MrRuDy pm
  • For example, look here . There are methods sendMessage and saveSentMessage . In general, Google look for examples. - Igor Kudryashov

Use the Gmail SMTP server. It saves emails in the Sent Items folder.