【干货】使用Java发送各种格式的邮件

来源:互联网 发布:安全防护眼镜知多少 编辑:程序博客网 时间:2024/06/06 01:08


          测试可用:

         有些重复代码没有给注释。类的方法作用自行查看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表示为multipartMimeMessageHelper 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表示为multipartMimeMessageHelper 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系列错误。

 




  

2 0