zoukankan      html  css  js  c++  java
  • Java发邮件:Java Mail与Apache Mail

    一、邮件简介

    一封邮件由很多信息构成,主要的信息如下,其他的暂时不考虑,例如抄送等:

    1、收件人:收件人的邮箱地址,例如xxx@xx.com

    2、收件人姓名:大部分的邮件显示时都会显示,

    3、发件人:发件人的邮箱地址

    4、发件人姓名:

    5、主题:邮件的标题

    6、内容及附件:邮件的主要内容

    二、使用Java发邮件的通用步骤

    一般的项目中没有单独的邮件服务器,一般情况下都是使用别人的服务器。

    1设置smtp服务器:不同的邮件服务器有不同的地址,例如:smtp.qq.com表示腾讯的smtp服务器。

    2授权:使用该服务器的帐号和密码登录该服务器。

    3创建邮件:创建一份包含所有信息的邮件,比如发件人、收件人、内容等。

    4设置邮件的属性:为邮件的属性添加数据。

    5发送邮件:因为封装不同,发送的方式不一致。

    三、Java Mail与Apache Mail

    Apache Mail是对Java Mail的封装,使用起来更加的简便,逻辑层次感更好。

    使用Java Mail只需要导入一个jar包:mail.jar

    使用Apache Mail的时候需要导入两个jar包:mail.jarcommons-email-1.3.1.jar

    四、使用Java Mail发送邮件

     1 public static void main(String[] args) throws Exception {
     2         final String user = "xxxx";
     3         final String password = "";
     4 
     5         String fromAddress = "xxx@qq.com";
     6         String toAddress = "xxx@163.com";
     7         String subject = "邮件测试主题";
     8         String content = "这是一个测试邮件<b>哈哈</b>";
     9 
    10         //配置参数
    11         Properties props = new Properties();
    12         props.setProperty("mail.smtp.auth", "true");
    13         props.setProperty("mail.transport.protocol", "smtp");
    14         props.setProperty("mail.host", "smtp.qq.com");
    15         // 方法一:使用transport对象发送邮件
    16         {
    17             //通过参数生成会话
    18             Session session = Session.getInstance(props);
    19             //启用调试模式
    20             session.setDebug(true);
    21             //创建一封邮件,并设置信息
    22             Message message = new MimeMessage(session);
    23             message.setFrom(new InternetAddress(fromAddress));
    24             message.setSubject(subject);
    25             message.setText(content);
    26             //创建传输
    27             Transport transport = session.getTransport();
    28             //连接smtp服务器
    29             transport.connect(user, password);
    30             //发送
    31             transport.sendMessage(message, new InternetAddress[] { new InternetAddress(toAddress) });
    32             transport.close();
    33         }
    34         
    35         
    36         // 方法二:使用Transport类静态方法发送邮件
    37         {
    38             //生成Session时以获取授权连接
    39             Session session = Session.getInstance(props, new Authenticator() {
    40                 @Override
    41                 protected PasswordAuthentication getPasswordAuthentication() {
    42                     return new PasswordAuthentication(user, password);
    43                 }
    44             });
    45             session.setDebug(true);
    46             //创建一封邮件,并设置信息
    47             Message message = new MimeMessage(session);
    48             message.setSubject(subject);
    49             message.setFrom(new InternetAddress(fromAddress));
    50             message.setRecipient(RecipientType.TO, new InternetAddress(toAddress));
    51             message.setContent(content, "text/html;charset=utf-8");
    52             
    53             //直接发送,message通过已经授权的Session生成
    54             Transport.send(message);
    55         }
    56     }

    五、使用Apache Mail发送邮件

     1 public class ApacheMailTest {
     2     // smtp服务器
     3     private String hostName = "smtp.qq.com";
     4     // 帐号与密码
     5     private String userName = "xxx";
     6     private String password = "这是个秘密";
     7     // 发件人
     8     private String fromAddress = "xxx@qq.com";
     9     // 发件人姓名
    10     private String fromName = "xxx";
    11 
    12     public static void main(String[] args) throws Exception {
    13         // 收件人与收件人名字
    14         String toAddress = "xxx@163.com";
    15         String toName = "xxx";
    16         ApacheMailTest test = new ApacheMailTest();
    17         // 所有的异常都为处理,方便浏览
    18 
    19         test.sendSimpleEmail(toAddress, toName);
    20         test.sendHtmlEmail(toAddress, toName);
    21         test.sendMultiPartEmail(toAddress, toName);
    22         System.out.println("发送完成");
    23     }
    24 
    25     // 发送简单邮件,类似一条信息
    26     public void sendSimpleEmail(String toAddress, String toName) throws Exception {
    27         SimpleEmail email = new SimpleEmail();
    28         email.setHostName(hostName);// 设置smtp服务器
    29         email.setAuthentication(userName, password);// 设置授权信息
    30         email.setCharset("utf-8");
    31         email.setFrom(fromAddress, fromName, "utf-8");// 设置发件人信息
    32         email.addTo(toAddress, toName, "utf-8");// 设置收件人信息
    33         email.setSubject("测试主题");// 设置主题
    34         email.setMsg("这是一个简单的测试!");// 设置邮件内容
    35         email.send();// 发送邮件
    36     }
    37 
    38     // 发送Html内容的邮件
    39     public void sendHtmlEmail(String toAddress, String toName) throws Exception {
    40         HtmlEmail email = new HtmlEmail();
    41         email.setHostName(hostName);
    42         email.setAuthentication(userName, password);
    43         email.setCharset("utf-8");
    44         email.addTo(toAddress, toName, "utf-8");
    45         email.setFrom(fromAddress, fromName, "utf-8");
    46         email.setSubject("这是一个html邮件");
    47         // 设置html内容,实际使用时可以从文本读入写好的html代码
    48         email.setHtmlMsg("<div style='100px;height:200px;'>a</div>");
    49         email.send();
    50 
    51     }
    52 
    53     // 发送复杂的邮件,包含附件等
    54     public void sendMultiPartEmail(String toAddress, String toName) throws Exception {
    55         MultiPartEmail email = null;
    56         email = new MultiPartEmail();
    57         email.setHostName(hostName);
    58         email.setAuthentication(userName, password);
    59         email.setCharset("utf-8");
    60         email.addTo(toAddress, toName, "utf-8");
    61         email.setFrom(fromAddress, fromName, "utf-8");
    62         email.setSubject("这是有附件的邮件");
    63         email.setMsg("<a href='#'>测试内容</a>");
    64 
    65         // 为邮件添加附加内容
    66         EmailAttachment attachment = new EmailAttachment();
    67         attachment.setPath("D:\邮件.txt");// 本地文件
    68         // attachment.setURL(new URL("http://xxx/a.gif"));//远程文件
    69         attachment.setDisposition(EmailAttachment.ATTACHMENT);
    70         attachment.setDescription("描述信息");
    71         // 设置附件显示名字,必须要编码,不然中文会乱码
    72         attachment.setName(MimeUtility.encodeText("邮件.txt"));
    73         // 将附件添加到邮件中
    74         email.attach(attachment);
    75         email.send();
    76     }
    77 }
  • 相关阅读:
    创建基于MailKit和MimeKit的.NET基础邮件服务
    MailKit---获取邮件
    C# 与JAVA 的RSA 加密解密交互,互通,C#使用BouncyCastle来实现私钥加密,公钥解密的方法
    .net 开源 FTP 组件 edtFTPnet
    Consul1-window安装consul
    通信传输利器Netty(Net is DotNetty)介绍
    工作中,如何衡量一个人的 JavaScript 编码水平?
    10个有趣又能编译为JavaScript的语言,你用过哪些?
    一定要你明白Java中的volatile
    面试总被问到HTTP缓存机制及原理?看完你就彻底明白了
  • 原文地址:https://www.cnblogs.com/vikde/p/3750943.html
Copyright © 2011-2022 走看看