admin管理员组

文章数量:1559037

写一个企业邮箱发送的问题,每个用户都能发送邮件,这个还是比较好写的,百度一大推,但是我是在本地测试的,我把代码部署到阿里云上去之后,一直发送失败,查看日志文件,说连接腾讯邮箱服务器失败,端口使用的是25,后面经过资料查找,说什么阿里云不支持25端口,要使用ssl的465.后面经过测试果然ok.记录一下.

每个用户都能够发送邮件,为了密码安全,全部在腾讯企业邮箱设置客户端安全密码

@data
public class mailbean {
    private string[] to; // 收件人
    private string[] cc;//抄送人
    private string from; // 发件人
    private string host; // smtp主机
    private string username; // 发件人的用户名
    private string password; // 发件人的密码
    private string subject; // 邮件主题
    private string content; // 邮件正文
    private inputstream fileinputstream; // 文件流
    private string filename; // 附件的文件名
}
public boolean sendmail(mailbean mb) throws ioexception {
        string host = mb.gethost();
        final string password = mb.getpassword();
        final string from = mb.getfrom();
        string[] to = mb.getto();
        string[] cc=mb.getcc();
        string subject = mb.getsubject();
        string content = mb.getcontent();
        string filename = mb.getfilename();
        inputstream file = mb.getfileinputstream();
        properties prop = new properties();
        //协议
        prop.setproperty("mail.transport.protocol", "smtp");
        //服务器
        prop.setproperty("mail.smtp.host", "smtp.exmail.qq");
        //端口
        prop.setproperty("mail.smtp.port", "465");
        //使用smtp身份验证
        prop.setproperty("mail.smtp.auth", "true");
        mailsslsocketfactory sf = null;
        try {
            sf = new mailsslsocketfactory();
            sf.settrustallhosts(true);
        } catch (generalsecurityexception e1) {
            e1.printstacktrace();
        }
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketfactory", sf);
        session session = session.getdefaultinstance(prop, new authenticator() {
            public passwordauthentication getpasswordauthentication() {
                return new passwordauthentication(from, password);
            }
        });
        try {
            mimemessage msg = new mimemessage(session);
            msg.setfrom(new internetaddress(from));
            internetaddress[] toaddress = new internetaddress[to.length];//添加收件人
            for(int i=0;inew internetaddress(to[i]);
            }
            internetaddress[] ccaddress = new internetaddress[cc.length];//添加抄送人
            for(int i=0;inew internetaddress(cc[i]);
            }
            msg.setrecipients(message.recipienttype.to, toaddress);
            msg.setrecipients(message.recipienttype.cc, ccaddress);
            msg.setsubject(tochinese(subject));
            multipart mp = new mimemultipart();
            mimebodypart body = new mimebodypart();
            body.setcontent(content, "text/html; charset=utf-8");
            mp.addbodypart(body);
            /* 往邮件中添加附件 */
            if (file != null) {
                mimebodypart mbpfile = new mimebodypart();
                datasource datasource=new bytearraydatasource(file, "application/png");
                datahandler datahandler=new datahandler(datasource);;
                mbpfile.setdatahandler(datahandler);
                mbpfile.setfilename(filename);
                mp.addbodypart(mbpfile);
            }
            msg.setcontent(mp);
            msg.setsentdate(new date());
            transport.send(msg);
        } catch (messagingexception me) {
            me.printstacktrace();
            logger.error(me.getmessage());
            return false;
        }
        return true;
    }

记录一下!

本文标签: 腾讯企业邮箱