java 发送 mail 纯文本发送和html格式发送

来源:互联网 发布:网络发现强制关闭 编辑:程序博客网 时间:2024/06/05 18:35

定义消息实体类

public class MailInfo {    //邮箱服务器 如smtp.163.com    private String host ;    //用户邮箱 如**@163    private String formName ;    //用户授权码 不是用户名密码 可以自行查看相关邮件服务器怎么查看    private String formPassword ;    //消息回复邮箱    private String replayAddress ;    //发送地址    private String toAddress ;    //发送主题    private String subject ;    //发送内容    private String content ;    public String getHost() {        return host;    }    public void setHost(String host) {        this.host = host;    }    public String getFormName() {        return formName;    }    public void setFormName(String formName) {        this.formName = formName;    }    public String getFormPassword() {        return formPassword;    }    public void setFormPassword(String formPassword) {        this.formPassword = formPassword;    }    public String getReplayAddress() {        return replayAddress;    }    public void setReplayAddress(String replayAddress) {        this.replayAddress = replayAddress;    }    public String getToAddress() {        return toAddress;    }    public void setToAddress(String toAddress) {        this.toAddress = toAddress;    }    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;    }}



首先你需要获取你的授权码,以@163.com为例

sendTextMail是纯文本发送
sendHtmlMail是HTML格式标签发送 这种方式可以识别html标签

public class MailSendUtil {    private final static String host = "smtp.163.com"; //163的服务器    private final static String formName = "YYYXXX@163.com";//你的邮箱    private final static String password = "***********"; //授权码    private final static String replayAddress = "YYYXXX@163.com"; //你的邮箱    public static void sendHtmlMail(MailInfo info)throws Exception{        info.setHost(host);        info.setFormName(formName);        info.setFormPassword(password);   //网易邮箱的授权码~不一定是密码        info.setReplayAddress(replayAddress);        Message message = getMessage(info);        //设置html内容message.setContent(info.getContent(),"text/html;charset=utf-8");        Transport.send(message);    }    public static void sendTextMail(MailInfo info) throws Exception {        info.setHost(host);        info.setFormName(formName);        info.setFormPassword(password);   //网易邮箱的授权码~不一定是密码        info.setReplayAddress(replayAddress);        Message message = getMessage(info);        //消息发送的内容        message.setText(info.getContent());        Transport.send(message);    }    private static Message getMessage(MailInfo info) throws Exception{        final Properties p = System.getProperties() ;        p.setProperty("mail.smtp.host", info.getHost());        p.setProperty("mail.smtp.auth", "true");        p.setProperty("mail.smtp.user", info.getFormName());        p.setProperty("mail.smtp.pass", info.getFormPassword());        // 根据邮件会话属性和密码验证器构造一个发送邮件的session        Session session = Session.getInstance(p, new Authenticator(){            protected PasswordAuthentication getPasswordAuthentication() {                return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass"));            }        });        session.setDebug(true);        Message message = new MimeMessage(session);        //消息发送的主题        message.setSubject(info.getSubject());        //接受消息的人        message.setReplyTo(InternetAddress.parse(info.getReplayAddress()));        //接收消息的人message.setRecipient(RecipientType.TO, new InternetAddress(info.getToAddress()));        // 创建邮件的接收者地址,并设置到邮件消息中        message.setRecipient(Message.RecipientType.TO,new InternetAddress(info.getToAddress()));        // 消息发送的时间        message.setSentDate(new Date());        return message ;    }}


原创粉丝点击