测试可用:
有些重复代码没有给注释。类的方法作用自行查看API了解,最后附上源码。
首先使用JavaMail的jar,官网可下载。
公共静态的常量:
- public final static String MAIL = "@sina.com"; // 邮箱格式
- public final static String SEND_HOST = "smtp.sina.com"; // 邮箱发送服务器
- public final static String ACCEPT_HOST = "pop.sina.com"; // 邮箱服务器
- public final static String SEND_USER = "xxxx"; // 用户名
- public final static String SEND_PWD = "xxxxx"; // 密码
- public final static String FROM_MAIL = SEND_USER + "@sina.com";// 发送方邮箱地址
- public final static String TO_MAIL = "xxxx@sina.com"; // 接收方邮箱地址
- public final static String CC_MAIL = SEND_USER + MAIL; // 抄送方邮箱地址
- public final static String BCC_MAIl = SEND_USER + MAIL; // 密送方邮箱地址
- public final static String ENCODE = "UTF-8";
- public static Date date = new Date();
使用Java自带的接口和类发送文本格式邮件
- Properties prop = new Properties();
- prop.setProperty("mail.host", SEND_HOST);
- prop.setProperty("mail.transport.protocol", "smtp");
- prop.setProperty("mail.smtps.ssl.enable", "true");
- // prop.setProperty("mail.smtp.port", "25");
- prop.setProperty("mail.smtp.auth", "true");
- Session session = Session.getInstance(prop); //创建应用会话
- Message message = new MimeMessage(session); //消息摘要,是邮件的主体
- message.setSubject("测试"); //设置主题
- message.setText("你好!"); //邮件内容
- message.setSentDate(new Date()); //发送日期
- message.setFrom(new InternetAddress(FROM_MAIL)); //发送方
- message.addRecipient(Message.RecipientType.TO, new InternetAddress(TO_MAIL)); //接受方
- message.saveChanges(); //保存邮件主体对象内容
- Transport transport = session.getTransport(); //传输对象
- transport.connect(SEND_HOST, FROM_MAIL, SEND_PWD); //连接服务器中的邮箱
- transport.sendMessage(message, message.getAllRecipients()); //发送
- transport.close(); //关闭传输
- System.out.println("Successfully send mail to all user");
感觉自带的接口方法比较麻烦,使用spring封装的javamail,记的导入spring相关包。
使用spring发送文本格式的邮件,代码如下:
- public static void sendTxtMail() throws MailException {
- JavaMailSenderImpl send = new JavaMailSenderImpl();
- Properties prop = new Properties();
- prop.setProperty("mail.transport.protocol", "smtp");
- prop.setProperty("mail.host", SEND_HOST);
- prop.setProperty("mail.smtps.ssl.enable", "true");
- prop.setProperty("mail.smtp.auth", "true");
- send.setUsername(SEND_USER);
- send.setPassword(SEND_PWD);
- send.setJavaMailProperties(prop);
- SimpleMailMessage msg = new SimpleMailMessage();
- msg.setFrom(FROM_MAIL);
- msg.setTo(TO_MAIL);
- msg.setReplyTo(FROM_MAIL);
- msg.setCc(CC_MAIL);
- msg.setBcc(BCC_MAIl);
- msg.setSentDate(date);
- msg.setSubject("发送的文本格式邮件");
- msg.setText("文本格式 测试成功!");
- send.send(msg);
- System.out.println("Successfully send mail to the user");
使用spring的封装方法发送Html格式邮件 ,代码如下:
- // 发送Html格式邮件
- public static void sendHtmlMail() throws Exception {
- JavaMailSenderImpl send = new JavaMailSenderImpl();
- Properties prop = new Properties();
- prop.setProperty("mail.transport.protocol", "smtp");
- prop.setProperty("mail.host", SEND_HOST);
- prop.setProperty("mail.smtps.ssl.enable", "true");
- prop.setProperty("mail.smtp.auth", "true");
- send.setUsername(SEND_USER);
- send.setPassword(SEND_PWD);
- send.setJavaMailProperties(prop);
- MimeMessage msg = send.createMimeMessage();
- MimeMessageHelper helper = new MimeMessageHelper(msg, ENCODE);
- helper.setFrom(FROM_MAIL);
- helper.setTo(TO_MAIL);
- helper.setReplyTo(FROM_MAIL);
- helper.setCc(CC_MAIL);
- helper.setBcc(BCC_MAIl);
- helper.setSentDate(date);
- helper.setSubject("发送的HTML格式邮件");
- String html = "<font size='5' color='red'>HTML格式测试成功!</font>";
- helper.setText(html, true); // 邮件内容,参数true表示是html代码
- send.send(msg);
- System.out.println("Successfully send mail to the user");
使用spring的封装方法发送带内嵌内容的Html格式邮件 ,代码如下:
- // 发送带内嵌文件的HTML格式邮件
- public static void sendInlineMail() throws Exception {
- // spring提供的邮件实现类
- JavaMailSenderImpl send = new JavaMailSenderImpl();
- Properties prop = new Properties();
- prop.setProperty("mail.transport.protocol", "smtp"); // 设置邮件发送协议
- prop.setProperty("mail.host", SEND_HOST); // 邮件服务器地址
- prop.setProperty("mail.smtps.ssl.enable", "true"); // 邮件ssl验证
- prop.setProperty("mail.smtp.auth", "true"); // 邮件服务身份验证
- send.setUsername(SEND_USER); // 设置用户名
- send.setPassword(SEND_PWD); // 设置密码
- send.setJavaMailProperties(prop);
- MimeMessage msg = send.createMimeMessage();
- // 指定HTML编码,参数true表示为multipart
- MimeMessageHelper helper = new MimeMessageHelper(msg, true, ENCODE);
- helper.setFrom(FROM_MAIL); // 发送者邮箱
- helper.setTo(TO_MAIL); // 接收者邮箱
- helper.setReplyTo(FROM_MAIL); // 回复邮箱
- helper.setCc(CC_MAIL); // 抄送邮箱
- helper.setBcc(BCC_MAIl); // 密送邮箱
- helper.setSentDate(date); // 发送日期
- helper.setSubject("发送的带有内部文件的HTML格式邮件");
- String html = "<font size='5' color='red'>HTML格式测试成功!</font>"
- + "<img src ='cid:demoimg'>"; // cid是一个固定前缀,demoimg是一个资源名称
- helper.setText(html, true); // 邮件内容,参数true表示是html代码
- ClassPathResource resource = new ClassPathResource("col.jpg"); // 加载项目路径下资源
- helper.addInline("demoimg", resource); // 将资源绑定到demoimg上
- send.send(msg); // 发送邮件
- System.out.println("Successfully send mail to the user");
- }
使用spring的封装方法发送带附件的邮件 ,代码如下:
- // 发送带附件的邮件
- public static void sendAttachmentMail() throws Exception {
- // spring提供的邮件实现类
- JavaMailSenderImpl send = new JavaMailSenderImpl();
- Properties prop = new Properties();
- prop.setProperty("mail.transport.protocol", "smtp"); // 设置邮件发送协议
- prop.setProperty("mail.host", SEND_HOST); // 邮件服务器地址
- prop.setProperty("mail.smtps.ssl.enable", "true"); // 邮件ssl验证
- prop.setProperty("mail.smtp.auth", "true"); // 邮件服务身份验证
- send.setUsername(SEND_USER); // 设置用户名
- send.setPassword(SEND_PWD); // 设置密码
- send.setJavaMailProperties(prop);
- MimeMessage msg = send.createMimeMessage();
- // 指定HTML编码,参数true表示为multipart
- MimeMessageHelper helper = new MimeMessageHelper(msg, true, ENCODE);
- helper.setFrom(FROM_MAIL); // 发送者邮箱
- helper.setTo(TO_MAIL); // 接收者邮箱
- helper.setReplyTo(FROM_MAIL); // 回复邮箱
- helper.setCc(CC_MAIL); // 抄送邮箱
- helper.setBcc(BCC_MAIl); // 密送邮箱
- helper.setSentDate(date); // 发送日期
- helper.setSubject("发送带有附件的邮件");
- String html = "<font size='5' color='red'>附件测试成功!</font>";
- helper.setText(html, true); // 邮件内容,参数true表示是html代码
- String demo = "demo.docx";
- ClassPathResource resource = new ClassPathResource(demo); // 加载项目路径下资源
- helper.addAttachment(MimeUtility.encodeWord(demo), resource); // 如果文件是中文名,需要转码。
- send.send(msg); // 发送邮件
- System.out.println("Successfully send mail to the user");
- }
在测试之前记得将邮箱smtp、pop设置上,否者会报验证错误或连接服务错误 即50X系列错误。