send Mail No Auth - Java Network

Java examples for Network:EMail

Description

send Mail No Auth

Demo Code


//package com.java2s;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
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.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;
import javax.mail.internet.MimeUtility;

public class Main {

    private static final String DEFAULT_CHARSET = "euc-kr";

    public static void sendMailNoAuth(String smtpHost, String smtpPort,
            String subject, String content, String toEmail,
            String fromEmail, String fromName)
            throws UnsupportedEncodingException, MessagingException {
        sendMailNoAuth(smtpHost, smtpPort, subject, content, toEmail,
                fromEmail, fromName, DEFAULT_CHARSET, null);
    }/* ww w. j  a v  a  2 s  . c om*/

    public static void sendMailNoAuth(String smtpHost, String smtpPort,
            String subject, String content, String toEmail,
            String fromEmail, String fromName, String charset)
            throws UnsupportedEncodingException, MessagingException {
        sendMailNoAuth(smtpHost, smtpPort, subject, content, toEmail,
                fromEmail, fromName, charset, null);
    }

    public static void sendMailNoAuth(String smtpHost, String smtpPort,
            String subject, String content, String toEmail,
            String fromEmail, String fromName, String charset,
            File[] attachFiles) throws UnsupportedEncodingException,
            MessagingException {
        Properties props = new Properties();
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.port", smtpPort);

        Session session = Session.getDefaultInstance(props, null);
        sendMail(subject, content, toEmail, fromEmail, fromName, charset,
                attachFiles, session);
    }

    private static void sendMail(String subject, String content,
            String toEmail, String fromEmail, String fromName,
            String charset, File[] attachFiles, Session session)
            throws UnsupportedEncodingException, MessagingException {
        MimeMessage message = new MimeMessage(session);
        InternetAddress addr = new InternetAddress(fromEmail, fromName,
                charset);
        message.setFrom(addr);
        message.setSubject(subject);
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(
                toEmail));

        if (attachFiles == null) {
            message.setContent(content, "text/html; charset=" + charset);
        } else {
            Multipart multipart = new MimeMultipart();
            BodyPart messageBodyPart = new MimeBodyPart();

            messageBodyPart.setText(content);
            multipart.addBodyPart(messageBodyPart);

            for (File f : attachFiles) {
                BodyPart fileBodyPart = new MimeBodyPart();
                FileDataSource fds = new FileDataSource(f);
                fileBodyPart.setDataHandler(new DataHandler(fds));
                fileBodyPart.setFileName(MimeUtility.encodeText(
                        f.getName(), charset, "B"));
                multipart.addBodyPart(fileBodyPart);
            }
            message.setContent(multipart);
        }
        Transport.send(message);
    }
}

Related Tutorials