Since I was involved in MA tools, I researched email delivery. I made a simple mail delivery (html mail) program using google's smtp server.
・ Java -Mail.jar: download link -Activation.jar: download link ・ Google smtp server
As a rough processing flow
smtp authentication → Delivery content, destination, sender settings → Send
It's a simple or normal flow.
import
import process
import javax.mail.Address;
import javax.mail.AuthenticationFailedException;
import javax.mail.Authenticator;
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;
import java.util.Properties;
import java.io.UnsupportedEncodingException;
import java.util.Date;
First of all, you need to import them as a premise.
python
Properties props = new Properties();
//SMTP server settings. Set google's SMTP server here
props.setProperty("mail.smtp.host","smtp.gmail.com");
//Change port number for SSL
props.setProperty("mail.smtp.port", "465");
//Time-out setting
props.setProperty("mail.smtp.connectiontimeout", "60000");
props.setProperty("mail.smtp.timeout", "60000");
//Authentication
props.setProperty("mail.smtp.auth", "true");
//This setting is required when using SSL
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port","465");
//Create session using the information set in props
final Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
//Set your gmail account and password here
return new PasswordAuthentication("[email protected]","your password");
}
});
In this way, set the credentials in Properties and create a session Assign authentication information to Session type variable, session.
python
//Set the variable session
MimeMessage contentMessage = new MimeMessage(session);
//Email content information
String mailContents = "<html><body><h1>hello</h1></body></html>"
try {
//Set the source address, display name, and character code
Address addFrom = new InternetAddress("[email protected]", "Mr. A", "UTF-8");
contentMessage.setFrom(addFrom);
//Set the destination address, display name, and character code
Address addTo = new InternetAddress("[email protected]","Mr. B","UTF-8");
contentMessage.addRecipient(Message.RecipientType.TO, addTo);
//Set subject
contentMessage.setSubject("Hello! !!","UTF-8");
//You have specified the content type of the email. In this case it will be an HTML email
contentMessage.setHeader("Content-Type", "text/html; charset=UTF-8");
//Setting the content of the email
contentMessage.setContent(mailContents, "text/html; charset=UTF-8");
//Setting of date etc.
contentMessage.setSentDate(new Date());
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Set the delivery content, destination, and source Assign it to a variable called contentMessage of type MimeMessage. By the way, if you set the content type to text, you can deliver text mail.
python
//send e-mail
try {
//Set the contentMessage earlier
Transport.send(contentMessage);
} catch (AuthenticationFailedException e) {
//Authentication failure
e.printStackTrace();
} catch (MessagingException e) {
//Connection failure to smtp server
e.printStackTrace();
}
This will send the email.
It seems that the basic mail delivery in Java is basically like this. To make this even faster, use threads, insert characters into the content, etc. It's interesting because you can do various things. The actual code can be found on github below, if you like.
Source: github
Recommended Posts