美文网首页
java 发送邮件

java 发送邮件

作者: wanggs | 来源:发表于2018-08-20 15:06 被阅读0次

    实例

    package com.toltech.phatent.commons.utils;
    
    import org.apache.commons.mail.DefaultAuthenticator;
    import org.apache.commons.mail.Email;
    import org.apache.commons.mail.EmailAttachment;
    import org.apache.commons.mail.EmailException;
    import org.apache.commons.mail.MultiPartEmail;
    import org.apache.commons.mail.SimpleEmail;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    /**
     * EmailUtils
     * @author qiuyp
     * @version 1.0
     * @since 2017年11月17日下午4:40:42
     */
    public class EmailUtils {
        private static Logger logger = LoggerFactory.getLogger(EmailUtils.class);
    
        public static void sendEmail(final String title, final String content, final String toEmail) {
            new Thread(new Runnable() {
                
                @Override
                public void run() {
                    try {
                        Email email = new SimpleEmail();
                        email.setHostName("smtp.exmail.qq.com");
                        email.setSmtpPort(465);
                        email.setAuthenticator(new DefaultAuthenticator("qiuyp@toltech.cn", "qazwsx12"));
                        email.setSSLOnConnect(true);
                        email.setFrom("qiuyp@toltech.cn", "大知桌面系统");
                        email.setSubject(title);
                        email.setMsg(content);
                        email.addTo(toEmail);
                        email.send();
                    } catch (EmailException e) {
                        logger.error("send email error.", e);
                    }
                }
            }).start();
            
        }
        public static void sendAttachment() throws EmailException {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try{
                        EmailAttachment ea = new EmailAttachment();
                        ea.setPath("D:\\1.txt");
                        MultiPartEmail email = new MultiPartEmail();
                        email.setHostName("smtp.exmail.qq.com");
                        email.setAuthenticator(new DefaultAuthenticator("wanggs@toltech.cn", "Qazwsx12"));
                        email.setCharset("UTF-8");
                        email.setTLS(true);
                        email.setFrom("wanggs@toltech.cn", "大知桌面系统");
                        email.setSubject("commons email");
                        email.setMsg("这是利用commons包发出的电子邮件");
                        email.addTo("2313920848@qq.com");
                        email.attach(ea);
                        email.send();
                    } catch (EmailException e) {
                        logger.error("send email error.", e);
                    }
    
                }
            });
            thread.start();
        }
    
        public static void main(String[] args) throws EmailException {
            sendAttachment();
        }
    }
    
    

    相关文章

      网友评论

          本文标题:java 发送邮件

          本文链接:https://www.haomeiwen.com/subject/yjlliftx.html