javaMail 发送邮件
来源:互联网 发布:安卓手机 mac照片 编辑:程序博客网 时间:2024/05/20 13:16
import org.springframework.stereotype.Service;import javax.mail.*;import javax.mail.internet.AddressException;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 java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Properties;/** * 简单邮件(不带附件的邮件)发送器 */@Servicepublic class SimpleMailSenderService {private static String mailServerHost = "XXXX.163.com";//可以是163邮箱的地址private static String mailServerPort = "25"; private static String sendUserName = "***.163.com";//private static String sendUserPassword ="123456" ;//private static String sendMailProtocol = PropertiesUtil.sendMailProtocol ;////邮件发送协议 /** * 文本格式发送邮件 * * @param addArray * 目标地址数组 * @param subject * 主题 * @param content * 内容 * @return */public static boolean sendTextMail(Address[] addArray, String subject, String content) throws Exception {// 判断是否需要身份认证MyAuthenticator authenticator = null;// 如果需要身份认证,则创建一个密码验证器authenticator = new MyAuthenticator(sendUserName, sendUserPassword);Properties p = new Properties();p.put("mail.smtp.host", mailServerHost);p.put("mail.smtp.port", mailServerPort);p.put("mail.smtp.auth", true);// 根据邮件会话属性和密码验证器构造一个发送邮件的sessionSession sendMailSession = Session.getDefaultInstance(p, authenticator);try {// 根据session创建一个邮件消息Message mailMessage = new MimeMessage(sendMailSession);// 创建邮件发送者地址Address from = new InternetAddress(sendUserName);// 设置邮件消息的发送者mailMessage.setFrom(from);// 创建邮件的接收者地址,并设置到邮件消息中mailMessage.setRecipients(Message.RecipientType.TO, addArray);// 设置邮件消息的主题mailMessage.setSubject(subject);// 设置邮件消息发送的时间mailMessage.setSentDate(new Date());// 设置邮件消息的主要内容mailMessage.setText(content);// 发送邮件Transport.send(mailMessage);return true;} catch (MessagingException ex) {throw new Exception(ex.getMessage(), ex);}}/** * 以HTML格式发送邮件 * * @param mailInfo * 待发送的邮件信息 * @throws MessagingException * @throws UnsupportedEncodingException */public static void sendHtmlMail(String to, String subject, String messageText) throws MessagingException, UnsupportedEncodingException {// Step 1: Configure the mail sessionjava.util.Properties props = new java.util.Properties();props.setProperty("mail.smtp.auth", "true");// 指定是否需要SMTP验证props.setProperty("mail.smtp.host", mailServerHost);// 指定SMTP服务器props.put("mail.transport.protocol", sendMailProtocol);//邮件发送协议 Session mailSession = Session.getDefaultInstance(props);mailSession.setDebug(true);// 是否在控制台显示debug信息// Step 2: Construct the messageInternetAddress fromAddress = new InternetAddress(sendUserName);InternetAddress toAddress = new InternetAddress(to);MimeMessage testMessage = new MimeMessage(mailSession);testMessage.setFrom(fromAddress);testMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);testMessage.setSentDate(new java.util.Date());testMessage.setSubject(MimeUtility.encodeText(subject, "gb2312", "B"));testMessage.setContent(messageText, "text/html;charset=gb2312");// Step 3: Now send the messageTransport transport = mailSession.getTransport("smtp");transport.connect(mailServerHost, sendUserName, sendUserPassword);transport.sendMessage(testMessage, testMessage.getAllRecipients());transport.close();} /** * 以HTML格式发送多人邮件 * * @param mailInfo * 待发送的邮件信息 * @throws MessagingException * @throws UnsupportedEncodingException */public static void sendHtmlMailMore(List<InternetAddress> to, String subject, String messageText) throws MessagingException, UnsupportedEncodingException {// Step 1: Configure the mail sessionjava.util.Properties props = new java.util.Properties();props.setProperty("mail.smtp.auth", "true");// 指定是否需要SMTP验证props.setProperty("mail.smtp.host", mailServerHost);// 指定SMTP服务器props.put("mail.transport.protocol", sendMailProtocol);//邮件发送协议 Session mailSession = Session.getDefaultInstance(props);mailSession.setDebug(true);// 是否在控制台显示debug信息// Step 2: Construct the messageInternetAddress fromAddress = new InternetAddress(sendUserName);// 设置多个收件人地址 InternetAddress[] internetAddressTo = (InternetAddress[]) to.toArray(new InternetAddress[to.size()]); MimeMessage testMessage = new MimeMessage(mailSession);testMessage.setFrom(fromAddress);testMessage.setRecipients(javax.mail.Message.RecipientType.TO, internetAddressTo);testMessage.setSentDate(new java.util.Date());testMessage.setSubject(MimeUtility.encodeText(subject, "gb2312", "B"));testMessage.setContent(messageText, "text/html;charset=gb2312");// Step 3: Now send the messageTransport transport = mailSession.getTransport("smtp");transport.connect(mailServerHost, sendUserName, sendUserPassword);transport.sendMessage(testMessage, testMessage.getAllRecipients());transport.close();}public static void main(String[] args) throws AddressException { String to = "CCC@qq.com"; String subject = "html邮件测试"; //subject javamail自动转码 StringBuffer theMessage = new StringBuffer(); theMessage.append("<div style='margin-top:10px'>发送的内容</div>"); List<InternetAddress> list = new ArrayList<InternetAddress>();list.add(new InternetAddress("AAAA@qq.com"));list.add(new InternetAddress("BBBB@qq.com")); try {sendHtmlMailMore( list, subject, theMessage.toString());} catch (Exception e) {e.printStackTrace();}}}import javax.mail.Authenticator;import javax.mail.PasswordAuthentication;public class MyAuthenticator extends Authenticator {String userName = null;String password = null;public MyAuthenticator() {}public MyAuthenticator(String username, String password) {this.userName = username;this.password = password;}protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(userName, password);}}
阅读全文
0 0
- javamail发送邮件
- 用javamail 发送邮件
- javamail发送邮件--sendmail
- JAVAMAIL发送邮件方法
- JavaMail发送邮件
- JavaMail发送邮件
- JavaMail发送邮件例子
- 用javamail发送邮件
- 用Javamail发送邮件
- JavaMail直接发送邮件
- JavaMail发送邮件
- JavaMail 发送验证邮件
- 使用JavaMail发送邮件
- javaMail发送邮件实例
- 用javamail 发送邮件
- JavaMail 实现邮件发送
- javamail邮件发送问题
- javamail发送邮件
- error note: Failed to load nodelet [/kinect2_points_xyzrgb_sd] of type [depth_image_proc/point_cloud
- 友盟集成步骤
- 关于java中sort的一些总结
- 一步步教你使用MyEclipse搭建java Web项目开发环境
- Python3之Django Web框架URL匹配、反向解析及部分错误处理
- javaMail 发送邮件
- QmlTextEdit带滚动条
- 信息学奥赛一本通(C++版) 第一部分 C++语言 第三章 程序的控制结构
- mysql exists与in 具体区别
- 从dts中得到电压和电流赋值给opp table
- 就突然色泽上;幸福不能的更新
- fiddler 使用方法总结
- S5PV210开发 -- 前言
- JavaScript实现轮播图,鼠标移入暂停播放,鼠标移除开始播放