Java WebMail Kullanımı

19-08-2021
ConcreteMail mail = new ConcreteMail("host", "username", "password");
mail.setPort(25);
mail.setFrom("username@blabla.com");
mail.setSubject("Konu");
mail.setContent("Başlık");
mail.send(email);

 

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Transport;
import java.util.Properties;
/**
 * Created by myuce on 25.01.2016.
 */
public class ConcreteMail extends Mail {
    private String host;
    private String toError;
    public TelifMail(String host, String username, String password) {
        super(host, username, password);
        this.host = host;
    }


    @Override
    protected void extraProperties(Properties properties) {
        properties.put("mail.smtp.ssl.trust", host);
    }

    /**
     * When application error occurred, send mails to toError
     * @param toError
     */
    public void setToError(String toError) {
        this.toError = toError;
    }

    public String getToError() {
        return toError;
    }
}
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created by myuce on 25.01.2016.
 */
public abstract class Mail {
    private final String username;
    private final String password;
    private String host;
    private int port = 25;
    private String subject;
    private String content;
    private List<File> fileList = new ArrayList<File>();
    private String text;
    private String from;

    public Mail(String host, String username, String password) {
        this.host = host;
        this.username = username;
        this.password = password;
    }

    public void send(String to) throws MessagingException {
        send(from, to);
    }

    public void send(String from, String to) throws MessagingException {
        this.from = from;
        MimeMessage message = createMessage();
        message.setSubject(getSubject(), "utf-8");
        message.setContent(createMultipart());
        if (this.from == null)
            throw new IllegalArgumentException("Lutfen setFrom() metodunu cagirarak from field'i belirtiniz");
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));
        Transport.send(message);
    }

    protected MimeMessage createMessage() {
        return new MimeMessage(createSession(username, password));
    }

    private Session createSession(String username, String password) {
        return Session.getInstance(initDefaultProperties(),
                new CustomAuthenticator(username, password));
    }

    private Properties initDefaultProperties() {
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        extraProperties(props);
        return props;
    }

    protected abstract void extraProperties(Properties properties);

    private Multipart createMultipart() throws MessagingException {
        Multipart result = new MimeMultipart();
        if (content != null)
            result.addBodyPart(createHtmlBodyPart());
        if (text != null)
            result.addBodyPart(createTextBodyPart());
        for (File file : fileList)
            result.addBodyPart(createFileBodyPart(file));
        return result;
    }

    private MimeBodyPart createHtmlBodyPart() throws MessagingException {
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setContent(content, "text/html; charset=utf-8");
        return htmlPart;
    }

    private MimeBodyPart createTextBodyPart() throws MessagingException {
        MimeBodyPart textPart = new MimeBodyPart();
        textPart.setContent(text, "text/plain; charset=utf-8");
        return textPart;
    }

    private BodyPart createFileBodyPart(File file) throws MessagingException {
        DataSource source = new FileDataSource(file.getPath());
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(file.getName());
        return messageBodyPart;
    }

    public void addAttachment(File file) {
        fileList.add(file);
    }

    public void addAttachments(List<File> files) {
        fileList.addAll(files);
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getSubject() {
        return subject;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Object getContent() {
        return content;
    }

    public int getPort() {
        return port;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    private class CustomAuthenticator extends Authenticator {
        private String username;
        private String password;

        public CustomAuthenticator(String username, String password) {
            this.username = username;
            this.password = password;
        }

        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    }
}


© 2019 All rights reserved. Codesenior.COM