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 ; }}
阅读全文
0 0
- java 发送 mail 纯文本发送和html格式发送
- java mail纯文本发送邮件
- java mail发送复杂邮件代码(包括附件,html消息,纯文本消息)
- php 发送html格式文本
- 用Java Mail发送带有图片附件的html格式邮件
- mail发送oracle输出html格式内容
- mail命令发送html格式的电子邮件
- mail命令发送html格式的电子邮件
- Java Mail发送文本邮件换行
- 简单的纯文本MAIL内网发送
- C# 发送纯文本的电子邮件(e-mail)
- java发送mail
- java发送mail
- java 发送mail
- Java Mail 发送邮件
- JAVA MAIL发送邮件
- java 发送mail
- 发送电子邮件 java mail
- MVC的衍生物、MVP
- angularjs
- 前端开发环境搭建
- 数据结构上机测试2-2:单链表操作B
- Tecplot.360EX+Chorus.2017.2.0.79771.Win64.&.Linux64.&.MacOSX64 3CD
- java 发送 mail 纯文本发送和html格式发送
- SLAM学习--三维空间刚体运动
- 如何让外部主机访问到docker中的数据库
- RecyclerView嵌套在NestedScrollView中的注意事项
- ImagerLoder
- Maven 入门篇
- caffe.proto详解
- Centos7 配置SVN服务器
- 由于图片异步加载,造成的被scrollView包裹的listview显示不全