java实现Email发送

Java实现Email发送

首先导入依赖‘’

		<dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-emailartifactId>
            <version>1.4version>
        dependency>

密码=授权码
QQ邮箱授权码官网教程

普通邮件发送:

public static void main(String[] args) throws Exception{

        // 创建简单文本对象
        Email email = new SimpleEmail();

        // QQ邮箱的 SMTP 服务器地址为: smtp.qq.com
        email.setHostName("smtp.qq.com");

        // 端口号
        email.setSmtpPort(465);

        // 设置默认 发送邮件 账号、发件人邮箱密码(授权码)
        //在开启SMTP服务时会获取到一个授权码,把授权码填在这里
        email.setAuthenticator(new DefaultAuthenticator("[email protected]", "XXXXXX"));
        // 开启SSL安全连接
        email.setSSLOnConnect(true);
        // 发件人
        email.setFrom("[email protected]");
        // 邮件主题(标题)
        email.setSubject("TestMail");
        // 邮件内容
        email.setMsg("This is a test mail ... :-)");
        // 收件人
        email.addTo("[email protected]");
        // 发送
        email.send();
        System.out.println("发送成功!");
    }

发送带附件的邮件:

public static void main(String[] args) throws Exception {
        /* 发送带附件的邮件 */
        // 创建Email附件对象
        EmailAttachment attachment = new EmailAttachment();
        // 附件地址(路径)
        attachment.setPath("E:\\test\\早安.jpg");
        attachment.setDisposition(EmailAttachment.ATTACHMENT);
        // 设置附件描述
        attachment.setDescription("Picture of John");
        // 设置附件名字
        attachment.setName("zhaoan.jpg");
        /***********************************************************/
     
        // 创建电子邮件消息对象
        MultiPartEmail email = new MultiPartEmail();
        // QQ邮箱的 SMTP 服务器地址为: smtp.qq.com
        email.setHostName("smtp.qq.com");
        // 端口号
        email.setSmtpPort(465);
        // 设置默认 发送邮件 账号、发件人邮箱密码(授权码)
        //在开启SMTP服务时会获取到一个授权码,把授权码填在这里
        email.setAuthenticator(new DefaultAuthenticator("[email protected]", "XXXXXX"));
        // 开启SSL安全连接
        email.setSSLOnConnect(true);
        // 收件人
        email.addTo("[email protected]");
        // 发件人
        email.setFrom("[email protected]");
        // 邮件主题(标题)
        email.setSubject("早安!");
        // 邮件内容
        email.setMsg("我们要学会珍惜我们生活的每一天。因为,这每一天的开始,都将是我们余下生命之中的第一天。早安!");

        // Email附加上附件
        email.attach(attachment);

        // send the email
        email.send();
        System.out.println("发送成功!");
    }

发送HTML格式的电子邮件

// Create the email message
  HtmlEmail email = new HtmlEmail();
  email.setHostName("mail.myserver.com");
  email.addTo("[email protected]", "John Doe");
  email.setFrom("[email protected]", "Me");
  email.setSubject("Test email with inline image");
  
  // embed the image and get the content id
  URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
  String cid = email.embed(url, "Apache logo");
  
  // set the html message
  email.setHtmlMsg("The apache logo - +cid+"\">");

  // set the alternative message
  email.setTextMsg("Your email client does not support HTML messages");

  // send the email
  email.send();

其他有关Email请参照:Apache Commons Email官网教程

你可能感兴趣的:(java实现Email发送)