🎶 Sym - 一款用 Java 实现的现代化社区(论坛/BBS/社交网络/博客)平台

📕 思源笔记 - 一款桌面端笔记应用,支持 Windows、Mac 和 Linux

🎸 Solo - B3log 分布式社区的博客端节点,欢迎加入下一代社区网络

♏ Vditor - 一款浏览器端的 Markdown 编辑器

java 发送邮件的简单例子

package com.jinfonet.KB;

import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;

import com.sun.mail.smtp.SMTPMessage;

public class MailSender {

    protected ParametersManager pm = ParametersManager.getInstance();

    public void sendToUser(String email, String content) {
        Properties mailProp = new Properties();
        try {
            mailProp
                    .setProperty("mail.smtp.host", pm.getItem("mail.user.host"));
            mailProp
                    .setProperty("mail.smtp.port", pm.getItem("mail.user.port"));

            Session session = Session.getDefaultInstance(mailProp);

            SMTPMessage mail = new SMTPMessage(session);
            InternetAddress from = new InternetAddress(pm
                    .getItem("mail.user.sender"));
            mail.setFrom(from);

            InternetAddress[] receiver = new InternetAddress[1];
            receiver[0] = new InternetAddress(email);
            mail.setRecipients(javax.mail.Message.RecipientType.TO, receiver);

            mail.setSubject(pm.getItem("mail.user.subject"));
            mail.setContent(content, "text/html;charset=UTF-8");

            Transport.send(mail);

        } catch (MessagingException me) {
            System.out.println(me.toString());
            me.printStackTrace();
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }

    public void sendToProductOwner(String content, String sender) {

        Properties mailProp = new Properties();
        try {
            mailProp.setProperty("mail.smtp.host", pm
                    .getItem("mail.owner.host"));
            mailProp.setProperty("mail.smtp.port", pm
                    .getItem("mail.owner.port"));

            Session session = Session.getDefaultInstance(mailProp);

            SMTPMessage mail = new SMTPMessage(session);
            InternetAddress from = null;
            if (sender == null) {
                from = new InternetAddress(pm.getItem("mail.owner.sender"));
            } else {
                from = new InternetAddress(sender);
            }

            String[] rece = pm.getItem("mail.owner.receiver").split(",");
            InternetAddress[] to = new InternetAddress[rece.length];
            for (int i = 0; i < rece.length; i++) {
                to[i] = new InternetAddress((String) rece[i]);
            }

            mail.setFrom(from);

            mail.setRecipients(javax.mail.Message.RecipientType.TO, to);

            mail.setSubject(pm.getItem("mail.owner.subject"));
            mail.setContent(content, "text/html;charset=UTF-8");

            Transport.send(mail);
        } catch (MessagingException me) {
            System.out.println(me.toString());
            me.printStackTrace();
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }

    public static void main(String s[]) {
        MailSender ms = new MailSender();
        ms.sendToProductOwner("hello", null);
        ms = null;
    }

}



package com.jinfonet.KB;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Properties;

public class ParametersManager {
    private static String PFILE = null;

    private File file = null;

    private long lastModifiedTime = 0;

    private Properties props = null;

    private static ParametersManager instance = new ParametersManager();

    private ParametersManager() {

        URL url = this.getClass().getResource("config.properties");

        if (url != null) {
            try {
                String path = URLDecoder.decode(url.getPath(), "UTF-8");
                /*
                 * For Unix FileSystem is file:/path For Windows FileSystem is
                 * file:/X:/path or file:/d:/path
                 */
                if ((path.length() >= 3) && (path.charAt(0) == '/')
                        && (path.charAt(2) == ':')) {
                    path = path.substring(1);
                }
                PFILE = path.replace('/', File.separatorChar);
            } catch (UnsupportedEncodingException ex) {
                System.out.println(ex.toString());
            }
        }
        file = new File(PFILE);

        lastModifiedTime = file.lastModified();
        if (lastModifiedTime == 0) {
            System.out.println(PFILE + " File does not exist!");
        }
        props = new Properties();
        try {
            props.load(new FileInputStream(PFILE));
        } catch (FileNotFoundException ex) {
            System.out.println(ex.toString());
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }

    /* return signal Instance */
    synchronized public static ParametersManager getInstance() {
        return instance;
    }

    public String getItem(String key) {
        long newTime = file.lastModified();
        if (newTime > lastModifiedTime) {
            props.clear();
            try {
                props.load(new FileInputStream(PFILE));
            } catch (IOException e) {
                System.out.println(e.toString());
            }
        }
        lastModifiedTime = newTime;
        return props.getProperty(key);
    }

    public void setItem(String key, String value) {
        String Header = "paramers of jinfonet customers self-service center";
        try {
            props.setProperty(key, value);
            props.store(new FileOutputStream(PFILE), Header);
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }
}




config.properties

mail.owner.port=25
mail.owner.sender=JReportCustomerCarePortal
[email protected]
mail.owner.host=192.168.128.4
mail.owner.subject=Jinfonet Technical Support


欢迎注册黑客派社区,开启你的博客之旅。让学习和分享成为一种习惯!

留下你的脚步