JavaMail API
The JavaMail API 1.4.7 developed by Oracle offers the possibility of sending and receiving emails from a simple Java program through SMTP, POP3 and IMAP protocols. JavaMail is integrated into the Java EE platform, but also provides an optional package for use in Java SE.

Some of the features of this open source library is that JavaMail implements the SMTP protocol (Simple Mail Transfer Protocol) as well as the different types of connection with mail servers such as TLS, SSL, user authentication and password.
Real password of the user UserIssuerMessage
static String UserIssuerMessage = "aitor_@hotmail.es"; static String passwordIssuerMessage = "password";
Server address for this protocol (SMTP) «smtp.live.com» «smtp.gmail.com
static String smtpHost = "Outlook.office365.com";
Port to be used on the server. 25 or 587
static String smtpPort = "25";
We indicate that we are going to authenticate in the server
static String smtpAuth = "true";
You get a session with the properties previously that we have saving in -props-
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.auth", smtpAuth);
Session sesion = Session.getDefaultInstance(props, null);
We started creating the e-mail
Message menssage = new MimeMessage(sesion);
Fill in the necessary fields of an e-mail:
The subject
menssage.setSubject("Messages from Java with a Hotmail account");
Message sender
menssage.setFrom(new InternetAddress(UserIssuerMessage));
In this case we have one or several receivers and keep in main that our contacts can be of different services such as Hotmail, Gmail, etc
Address [] receptores = new Address []{
new InternetAddress ("aitor_@hotmail.es"),
new InternetAddress ("aitor_@hotmail.es")
};
We add the list of recipients.
menssage.addRecipients(Message.RecipientType.TO, receptores);
Here is the content of the message
menssage.setText("Body of our message e-mail");
Now let’s send the message
Transport t = sesion.getTransport("smtp");
But first you have to authenticate with a real account of Hotmail
t.connect(UserIssuerMessage, passwordIssuerMessage); t.sendMessage(menssage, menssage.getRecipients(Message.RecipientType.TO));
—
package javaemailsender;
import com.sun.mail.smtp.SMTPTransport;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
*
* @author Aitor Alcañiz
*/
public class JavaEmailSender{
static String UserIssuerMessage = "aitor_@hotmail.es";
static String passwordIssuerMessage = "password";
static String smtpHost = "Outlook.office365.com";
static String smtpPort = "25";
static String smtpAuth = "true";
static Properties props = new Properties();
public static void main(String[] args) {
JavaEmailSender.SendMessage();
System.out.println("Message sent!!!");
}
public static void SendMessage(){
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.auth", smtpAuth);
Session sesion = Session.getDefaultInstance(props, null);
try {
Message menssage = new MimeMessage(sesion);
menssage.setSubject("Messages from Java with a Hotmail account");
menssage.setFrom(new InternetAddress(UserIssuerMessage));
Address [] receptores = new Address []{
new InternetAddress ("aitor_@hotmail.es"),
new InternetAddress ("aitor_@hotmail.es")
};
menssage.addRecipients(Message.RecipientType.TO, receptores);
menssage.setText("Body of our message e-mail");
Transport t = sesion.getTransport("smtp");
t.connect(UserIssuerMessage, passwordIssuerMessage);
t.sendMessage(menssage, menssage.getRecipients(Message.RecipientType.TO));
}catch(MessagingException e) {
System.err.println(e.getMessage());
}
}
}



Publicar comentario