Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mailMessage;
- 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.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.*;
- public class EmailMessage {
- private String from;
- //private LinkedList<String> to = new LinkedList<String>();
- private String to;
- private String subject;
- private String content;
- private String mimeType;
- private LinkedList<String> cc;
- private LinkedList<String> bcc;
- //private at least, in order to prevent running constructor from out of class
- private EmailMessage(){};
- private EmailMessage(Builder bilder){
- if (bilder == null)
- return;
- else{
- from = bilder.from;
- to = bilder.to;
- subject = bilder.subject;
- content = bilder.content;
- mimeType = bilder.mimeType;
- cc = bilder.cc;
- bcc = bilder.bcc;
- }
- }
- public void sendMail(EmailMessage ms){
- final String username = ms.getFrom();
- final String password = "bigheart01";
- Properties props = new Properties();
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.port", "587");
- Session session = Session.getInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- try{
- MimeMessage message = new MimeMessage(session);
- message.setFrom(new InternetAddress(ms.getFrom()));
- message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(ms.getTo()));
- message.setSubject(ms.getSubject());
- message.setText(ms.getContent());
- if (ms.getFrom() == "Invalid email address" || ms.getTo() == "Invalid email address")
- System.out.println("Could not send. Check email address.");
- else{
- Transport.send(message);
- System.out.println("Sent succesfully.");
- }
- }catch (MessagingException mex) {mex.printStackTrace();}
- }
- public String getFrom(){
- return from;
- }
- public String getTo(){
- return to;
- }
- public String getSubject(){
- return subject;
- }
- public String getContent(){
- return content;
- }
- public String getmimeType(){
- return mimeType;
- }
- public LinkedList<String> getCC(){
- return cc;
- }
- public LinkedList<String> getBCC(){
- return bcc;
- }
- public static class Builder{
- private String from;
- private String to;
- private String subject;
- private String content;
- private String mimeType;
- private LinkedList<String> cc;
- private LinkedList<String> bcc;
- protected Builder(){}
- public Builder fromWho(String from){
- Pattern p = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
- + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
- Matcher matcher = p.matcher(from);
- if (!matcher.matches()){
- System.out.println("Invalid email address");
- this.from = "Invalid email address";
- return this;
- }else{
- this.from = from;
- return this;
- }
- }
- public Builder toWho(String who){
- // for (String address : who){
- // to.add(address);
- // }
- Pattern p = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
- + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
- Matcher matcher = p.matcher(who);
- if (!matcher.matches()){
- System.out.println("Invalid email address");
- this.to = "Invalid email address";
- return this;
- }else{
- this.to = who;
- return this;
- }
- }
- public Builder whatSubject(String subject){
- this.subject = subject;
- return this;
- }
- public Builder whatContent(String content){
- this.content = content;
- return this;
- }
- public Builder whatMimeType(String mimeType){
- this.mimeType = mimeType;
- return this;
- }
- public Builder WhatCC(LinkedList<String> cc){
- this.cc = cc;
- return this;
- }
- public Builder WhatBCC(LinkedList<String> bcc){
- this.bcc = bcc;
- return this;
- }
- public EmailMessage build(){
- return new EmailMessage(this);
- }
- }
- }
- //second file
- public class MainMessage {
- public static void main(String[] argv){
- EmailMessage mail = new EmailMessage.Builder()
- .fromWho("belabot01@gmail.com")
- .toWho("belabot01@gmail.com")
- .whatSubject("heehe działa")
- .whatContent("pozdrawiam")
- .build();
- mail.sendMail(mail);
- // System.out.println(" MAIL:");
- // System.out.println("FROM: " + mail.getFrom());
- // System.out.println("TO: " + mail.getTo());
- // System.out.println("SUBJECT: " + mail.getSubject());
- // System.out.println("CONTENT: " + mail.getContent());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement