All Downloads are FREE. Search and download functionalities are using the official Maven repository.

anlavn.net.Email Maven / Gradle / Ivy

The newest version!
package anlavn.net;
// Make By Bình An || AnLaVN || KatoVN

import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**The Email class supports create a SMTP to send email.
 * @author AnLaVN - https://github.com/AnLaVN
 */
public class Email {
    private String HostEmail = null, Title = "Email title", Content = "

Email content.

"; private Session session = null; private Set setTO = new HashSet<>(), setCC = new HashSet<>(), setBCC = new HashSet<>(), setFile = new HashSet<>(); private InternetAddress[] getAddress(Set set) throws AddressException{ return InternetAddress.parse(String.join(",", set)); } private void setHost(String hostEmail, final String hostPass, String hostServer, int hostPort){ this.HostEmail = hostEmail; Properties p = new Properties(); p.put("mail.smtp.auth", "true"); p.put("mail.smtp.starttls.enable", "true"); p.put("mail.smtp.host", hostServer); p.put("mail.smtp.port", hostPort); this.session = Session.getInstance(p, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(HostEmail, hostPass); } }); } private Email() {} /**Create a host SMTP with full information. * @param hostEmail is your SMTP email address.
* @param hostPass is your SMTP server passwords.
*/ public Email(String hostEmail, String hostPass) { setHost(hostEmail, hostPass, "smtp.gmail.com", 587); } /**Create a host SMTP with full information. * @param hostEmail is your SMTP email address.
* @param hostPass is your SMTP server passwords.
* @param hostServer is your SMTP server address. Default is 'smtp.gmail.com'.
*/ public Email(String hostEmail, String hostPass, String hostServer) { setHost(hostEmail, hostPass, hostServer, 587); } /**Create a host SMTP with full information. * @param hostEmail is your SMTP email address.
* @param hostPass is your SMTP server passwords.
* @param hostServer is your SMTP server address. Default is 'smtp.gmail.com'.
* @param hostPort is port of your SMTP server. Default is '587'.
*/ public Email(String hostEmail, String hostPass, String hostServer, int hostPort) { setHost(hostEmail, hostPass, hostServer, hostPort); } /** Use this method to setup email content. * @param title is Email Title.
* @param content is Email Content.
*/ public final void setEmail(String title, String content){ this.Title = title; this.Content = content; } /** Use this method to add recipients with type is TO. * @param setTO is email address of recipients you want send to.
* @return true if this set changed.
*/ public final boolean addSetTO(String... setTO){ return Collections.addAll(this.setTO, setTO); } /** Use this method to add recipients with type is TO. * @param setTO is email address of recipients you want send to.
* @return true if this set changed.
*/ public final boolean addSetTO(Set setTO){ return this.setTO.addAll(setTO); } /** Use this method to get set of recipients with type is TO. * @return set of address recipients with type is TO. */ public final Set getSetTO(){ return this.setTO; } /** Use this method to clear set of recipients with type is TO. */ public final void clearSetTO(){ this.setTO.clear(); } /** Use this method to add recipients with type is CC. * @param setCC is email address of recipients you want send carbon copy.
* @return true if this set changed.
*/ public final boolean addSetCC(String... setCC){ return Collections.addAll(this.setCC, setCC); } /** Use this method to add recipients with type is CC. * @param setCC is email address of recipients you want send carbon copy.
* @return true if this set changed.
*/ public final boolean addSetCC(Set setCC){ return this.setCC.addAll(setCC); } /** Use this method to get set of recipients with type is CC. * @return set of recipients with type is CC. */ public final Set getSetCC(){ return this.setCC; } /** Use this method to clear set of recipients with type is CC. */ public final void clearSetCC(){ this.setCC.clear(); } /** Use this method to add recipients with type is BCC. * @param setBCC is email address of recipients you want send blind carbon copy.
* @return true if this set changed.
*/ public final boolean addSetBCC(String... setBCC){ return Collections.addAll(this.setBCC, setBCC); } /** Use this method to add recipients with type is BCC. * @param setBCC is email address of recipients you want send blind carbon copy.
* @return true if this set changed.
*/ public final boolean addSetBCC(Set setBCC){ return this.setBCC.addAll(setBCC); } /** Use this method to get set of recipients with type is BCC. * @return set of recipients with type is BCC. */ public final Set getSetBCC(){ return this.setBCC; } /** Use this method to clear set of recipients with type is BCC. */ public final void clearSetBCC(){ this.setBCC.clear(); } /** Use this method to add attachments files to email. * @param setFile is attachments files you want send.
* @return true if this set changed.
*/ public final boolean addSetAttachments(String... setFile){ return Collections.addAll(this.setFile, setFile); } /** Use this method to add attachments files to email. * @param setFile is attachments files you want send.
* @return true if this set changed.
*/ public final boolean addSetAttachments(Set setFile){ return this.setFile.addAll(setFile); } /** Use this method to get set of attachments files. * @return set of attachments files. */ public final Set getSetAttachments(){ return this.setFile; } /** Use this method to clear set of attachments files. */ public final void clearSetAttachments(){ this.setFile.clear(); } /** Use this method to clear content email include title, content email, TO, CC, BCC, Attachments. */ public final void clear(){ this.Title = "Email title"; this.Content = "

Email content.

"; clearSetTO(); clearSetCC(); clearSetBCC(); clearSetAttachments(); } /**Use this method to send email to recipients. * @throws javax.mail.MessagingException throw an exception if there is no internet connection or can't send email to the recipient. */ public final void sendEmail() throws MessagingException { Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(HostEmail)); if(setTO != null) msg.addRecipients(Message.RecipientType.TO, getAddress(setTO)); if(setCC != null) msg.addRecipients(Message.RecipientType.CC, getAddress(setCC)); if(setBCC!= null) msg.addRecipients(Message.RecipientType.BCC,getAddress(setBCC)); msg.setSubject(Title); if(setFile == null) msg.setContent(Content, "text/html; charset=UTF-8"); else{ BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(Content, "text/html; charset=UTF-8"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); for(String file : setFile){ messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(file))); messageBodyPart.setFileName(new File(file).getName()); multipart.addBodyPart(messageBodyPart); } msg.setContent(multipart); } Transport.send(msg); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy