1. 程式人生 > >mail.jar 發送郵件

mail.jar 發送郵件

sage ans 格式 rac 郵件 tco smtp 郵件內容 設置

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("[email protected]");
        MailUtil.init().setHost("cd.com.cn");
        MailUtil.init().setUser("user");
        MailUtil.init().setPassword("passwd");
        MailUtil.init().setRemindNum(5);
        try {
            MailUtil.sendMail(new String[]{"[email protected]","[email protected]"}, "測試", "hello yoyo");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

2.util配置參數+util發送郵件

mail.jar 發送郵件