莫大人

mail.jar 发送邮件

1.spring参数注入+util 发送邮件

2.util配置参数+util发送邮件

 

 

1.spring参数注入+util 发送邮件

    <bean id="mailSender" class="com.midea.ftms.util.MailSender">
        <property name="host" value="${mail.smtp.host}"></property>
        <property name="auth" value="${mail.smtp.auth}"></property>
        <property name="user" value="${mail.user}"></property>
        <property name="password" value="${mail.passwd}"></property>
        <property name="from" value="${mail.from}"></property>
        <property name="remindNum" value="${mail.remindnum}"></property>
        <property name="debugModel" value="${mail.debug}"></property>
    </bean>

 

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailSender {

    private String host;
    private String auth;
    private String user;
    private String password;
    private String from;
    private Integer remindNum;
    private Boolean debugModel;

    public MailSender() {

    }

    /**
     * @see 发送邮件基础方法,请遵循使用规则 MailUtil.sendMail
     * @param to 邮件接收地址
     * @param subject 邮件主题
     * @param content 邮件内容
     * @throws Exception 调用者处理异常
     */
    public void send(String[] to, String subject, String content)
            throws Exception {
        Properties props = new Properties();
        // 指定SMTP服务器
        props.put("mail.smtp.host", host);
        // 指定是否需要SMTP验证
        props.put("mail.smtp.auth", auth);
        Session mailSession = Session.getDefaultInstance(props);
        // 是否在控制台显示debug信息
        mailSession.setDebug(debugModel);
        Message message = new MimeMessage(mailSession);
        // 发件人
        message.setFrom(new InternetAddress(from));
        // 收件人
        InternetAddress[] addresses = new InternetAddress[to.length];
        for (int i = 0; i < to.length; i++) {
            addresses[i] = new InternetAddress(to[i]);
        }
        message.setRecipients(Message.RecipientType.TO, addresses);
        // 邮件主题
        message.setSubject("subject:"+subject);
        // 邮件内容(HTML格式)
        message.setContent(content, "text/html;charset=GBK");
        // 保存设置,让设置生效
        message.saveChanges();
        // 发送
        Transport transport = mailSession.getTransport("smtp");
        transport.connect(host, user, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getAuth() {
        return auth;
    }

    public void setAuth(String auth) {
        this.auth = auth;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFrom() {
        return from;
    }

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

    public Integer getRemindNum() {
        return remindNum;
    }

    public void setRemindNum(Integer remindNum) {
        this.remindNum = remindNum;
    }

    public Boolean getDebugModel() {
        return debugModel;
    }

    public void setDebugModel(Boolean debugModel) {
        this.debugModel = debugModel;
    }
    
}
public class MailUtil {
    
    private static volatile MailSender mailSender;
    
    private MailUtil() {
        
    }
    
    public static MailSender init() {
        if (mailSender == null) {
            synchronized (MailSender.class) {
                if (mailSender == null) {
//                    mailSender = new MailSender();
                    mailSender = (MailSender)ContextUtil.getContext().getBean("mailSender");
                }
            }
        }
        return mailSender;
    }
    
    
    public static void sendMail(String[] to, String subject, String content)
            throws Exception {
        MailUtil.init().send(to, subject, content);
    }
    
    public static void main(String[] args) {
        MailUtil.init().setAuth("true");
        MailUtil.init().setDebugModel(true);
        MailUtil.init().setFrom("a@b.com");
        MailUtil.init().setHost("cd.com.cn");
        MailUtil.init().setUser("user");
        MailUtil.init().setPassword("passwd");
        MailUtil.init().setRemindNum(5);
        try {
            MailUtil.sendMail(new String[]{"asdfa@qq.com","134324323@qq.com"}, "测试", "hello yoyo");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 

2.util配置参数+util发送邮件

 

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailSendUtil {
    
    private String host;
    private String auth;
    private String user;
    private String password;
    private String from;
    private Integer remindNum;
    private Boolean debugModel;
    
    private volatile static MailSendUtil mailSendUtil;
    
    private MailSendUtil(){
        init();
    }
    
    private void init() {
        host = PropertiesUtil.getProperty("mail.smtp.host");
        auth = PropertiesUtil.getProperty("mail.smtp.auth");
        user = PropertiesUtil.getProperty("mail.user");
        password = PropertiesUtil.getProperty("mail.passwd");
        from = PropertiesUtil.getProperty("mail.from");
        remindNum = Integer.parseInt(PropertiesUtil.getProperty("mail.remindnum"));
        debugModel = Boolean.valueOf(PropertiesUtil.getProperty("mail.debug"));
    }

    public static MailSendUtil getInstance() {
        if (mailSendUtil == null) {
            synchronized (MailSendUtil.class) {
                if (mailSendUtil == null) {
                    return new MailSendUtil();
                }
            }
        }
        return mailSendUtil;
    }
    
    public static void sendMail(String[] to, String subject, String content)
            throws Exception {
        MailSendUtil.getInstance().send(to, subject, content);
    }
    
    /**
     * @see 发送邮件基础方法,请遵循使用规则 MailUtil.sendMail
     * @param to 邮件接收地址
     * @param subject 邮件主题
     * @param content 邮件内容
     * @throws Exception 调用者处理异常
     */
    public void send(String[] to, String subject, String content)
            throws Exception {
        Properties props = new Properties();
        // 指定SMTP服务器
        props.put("mail.smtp.host", host);
        // 指定是否需要SMTP验证
        props.put("mail.smtp.auth", auth);
        Session mailSession = Session.getDefaultInstance(props);
        // 是否在控制台显示debug信息
        mailSession.setDebug(debugModel);
        Message message = new MimeMessage(mailSession);
        // 发件人
        message.setFrom(new InternetAddress(from));
        // 收件人
        InternetAddress[] addresses = new InternetAddress[to.length];
        for (int i = 0; i < to.length; i++) {
            addresses[i] = new InternetAddress(to[i]);
        }
        message.setRecipients(Message.RecipientType.TO, addresses);
        // 邮件主题
        message.setSubject("subject:"+subject);
        // 邮件内容(HTML格式)
        message.setContent(content, "text/html;charset=GBK");
        // 保存设置,让设置生效
        message.saveChanges();
        // 发送
        Transport transport = mailSession.getTransport("smtp");
        transport.connect(host, user, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    
}

3.遇到发送邮件正常,但是没有主题,也没有收件人的情况,请删除 geronimo-javamail_1.4_spec-1.2.jar

posted on 2017-07-15 16:06  莫大人  阅读(273)  评论(0编辑  收藏  举报

导航