java中使用自己的邮箱进行多人邮件发送

来源:互联网 发布:淘宝网店开店考试 编辑:程序博客网 时间:2024/06/13 07:33
email.properties文件如下:
host=smtp.qq.com  //这是qq邮箱,如果是其他邮箱 服务要配置成相应的hostuser=自己邮箱名pwd=邮箱密码subject=主题1:167359230@qq.com  //发送人2:17655858721@qq.com

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.Date;import java.util.Enumeration;import java.util.HashMap;import java.util.Map;import java.util.Properties;import java.util.Vector;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.PasswordAuthentication;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;import org.apache.commons.lang.StringUtils;import com.pzoom.dsa.common.util.Log;import com.pzoom.dsa.nerd.model.Area;public class SendMail {private static Log log = Log.getLogger(SendMail.class);    public static String toChinese(String text) {        try {            text = MimeUtility.encodeText(new String(text.getBytes(), "GB2312"), "GB2312", "B");        } catch (Exception e) {            e.printStackTrace();        }        return text;    }        static StringBuffer buffer = new StringBuffer();     static Map<String,String> map = new HashMap<String,String>();    static{try {InputStream input=Area.class.getClassLoader().getResourceAsStream("email.properties");BufferedReader br=new BufferedReader(new InputStreamReader(input));String line=br.readLine();while(line!=null){if(line.indexOf(":")>=0){String[] val = line.split(":");buffer.append(val[1]+",");}else{String[] val=line.split("=");map.put(val[0], val[1]);}line=br.readLine();}} catch (IOException e) {log.error(e);}    }    /**     * 邮件发送     * @param mb     * @return     */    public static boolean sendMail(MailBean mb) {            String from = map.get("user");//邮件发送人                String subject = map.get("subject");//邮件主题        String content = mb.getContent();        String fileName = mb.getFilename();        Vector<String> file = mb.getFile();        String type = mb.getType();        String nick = mb.getNick();                Properties props = System.getProperties();        props.put("mail.smtp.host", map.get("host"));                // 设置SMTP的主机        props.put("mail.smtp.auth", "true");            // 需要经过验证        if(map.get("host").contains("gmail")){        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");         props.setProperty("mail.smtp.socketFactory.fallback", "false");         props.setProperty("mail.smtp.port", "465");         props.setProperty("mail.smtp.socketFactory.port", "465");         }                Session session = Session.getInstance(props, new Authenticator() {            public PasswordAuthentication getPasswordAuthentication() {                return new PasswordAuthentication(map.get("user"),  map.get("pwd"));            }        });        try {            MimeMessage msg = new MimeMessage(session);            if(StringUtils.isNotBlank(nick)){            nick = MimeUtility.encodeText(nick);            from = nick+"<"+from+">";            }            msg.setFrom(new InternetAddress(from));            new InternetAddress();InternetAddress[] address = InternetAddress.parse(buffer.toString());            msg.setRecipients(Message.RecipientType.TO, address);//            msg.setSubject(toChinese(subject));            msg.setSubject(subject);            Multipart mp = new MimeMultipart();            MimeBodyPart mbpContent = new MimeBodyPart();            if("html".equals(type)){            mbpContent.setContent(content, "text/html;charset=gb2312");            }else {            mbpContent.setText(content);}            mp.addBodyPart(mbpContent);            /*    往邮件中添加附件    */            if(null!=file){            Enumeration<String> efile = file.elements();            while (efile.hasMoreElements()) {            MimeBodyPart mbpFile = new MimeBodyPart();            fileName = efile.nextElement().toString();            FileDataSource fds = new FileDataSource(fileName);            mbpFile.setDataHandler(new DataHandler(fds));            mbpFile.setFileName(toChinese(fds.getName()));            mp.addBodyPart(mbpFile);            }            }            msg.setContent(mp);            msg.setSentDate(new Date());            Transport.send(msg);                    } catch (MessagingException me) {            me.printStackTrace();            return false;        } catch (UnsupportedEncodingException e) {e.printStackTrace();return false;}        return true;    }    public static class MailBean{     private String to;                                // 收件人        private String from;                            // 发件人        private String host;                            // SMTP主机        private String username;                        // 发件人的用户名        private String password;                        // 发件人的密码        private String subject;                            // 邮件主题        private String content;                            // 邮件正文        Vector<String> file;                            // 多个附件        private String filename;                        // 附件的文件名        private String type;//type text html类别        private String nick;//昵称                public String getTo() {            return to;        }        public void setTo(String to) {            this.to = to;        }        public String getFrom() {            return from;        }        public void setFrom(String from) {            this.from = from;        }        public String getHost() {            return host;        }        public void setHost(String host) {            this.host = host;        }        public String getUsername() {            return username;        }        public void setUsername(String username) {            this.username = username;        }        public String getPassword() {            return password;        }        public void setPassword(String password) {            this.password = password;        }        public String getSubject() {            return subject;        }        public void setSubject(String subject) {            this.subject = subject;        }        public String getContent() {            return content;        }        public void setContent(String content) {            this.content = content;        }        public String getFilename() {            return filename;        }        public void setFilename(String filename) {            this.filename = filename;        }        public Vector<String> getFile(){            return file;        }                public void attachFile(String fileName) {            if(file == null)                file = new Vector<String>();            file.addElement(fileName);        }    public String getType() {    return type;    }    public void setType(String type) {    this.type = type;    }    public String getNick() {    return nick;    }    public void setNick(String nick) {    this.nick = nick;    }    }}


0 0
原创粉丝点击