当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

JAVA发送Email邮件

发表于: 2011-06-23   作者:Aaron5   来源:转载   浏览次数:
摘要: 使用Apache的Commons Email发送邮件 首先在pom中添加依赖: <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.
使用Apache的Commons Email发送邮件

首先在pom中添加依赖:
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-email</artifactId>
    <version>1.2</version>
</dependency>


1、发送简单邮件:
Email email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setTLS(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();


2、发送带附件的邮件:
import org.apache.commons.mail.*;
...

// Create the attachment
  EmailAttachment attachment = new EmailAttachment();
  attachment.setPath("mypictures/john.jpg");
  attachment.setDisposition(EmailAttachment.ATTACHMENT);
  attachment.setDescription("Picture of John");
  attachment.setName("John");

  // Create the email message
  MultiPartEmail email = new MultiPartEmail();
  email.setHostName("mail.myserver.com");
  email.addTo("jdoe@somewhere.org", "John Doe");
  email.setFrom("me@apache.org", "Me");
  email.setSubject("The picture");
  email.setMsg("Here is the picture you wanted");

  // add the attachment
  email.attach(attachment);

  // send the email
  email.send();


3、发送HTML格式的邮件:
import org.apache.commons.mail.HtmlEmail;
...

  // Create the email message
  HtmlEmail email = new HtmlEmail();
  email.setHostName("mail.myserver.com");
  email.addTo("jdoe@somewhere.org", "John Doe");
  email.setFrom("me@apache.org", "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("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");

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

  // send the email
  email.send();


4、发送带图片的HTML邮件:
import org.apache.commons.mail.HtmlEmail;
...

  // load your HTML email template
  String htmlEmailTemplate = ....
  
  // Create the email message
  HtmlEmail email = new ImageHtmlEmail();
  email.setHostName("mail.myserver.com");
  email.addTo("jdoe@somewhere.org", "John Doe");
  email.setFrom("me@apache.org", "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(htmlEmailTemplate, new File("").toURI().toURL(), false);

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

  // send the email
  email.send();


引用
commons email发送邮件用户指南: http://commons.apache.org/email/userguide.html

JAVA发送Email邮件

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
项目源码下载:http://download.csdn.net/detail/liangrui1988/6720047 效果图: 相关代码: test1 pac
项目源码下载:http://download.csdn.net/detail/liangrui1988/6720047 效果图: 相关代码: test1 pac
1 jar包  email.jar包,网上下载 2 源代码 package zjr.amy.emil.test; import java.util.Date; im
package com.hzk.mail; import java.net.MalformedURLException; import java.net.URL; import java
关于Email的预备知识: 原贴地址:http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/23434
今天折腾了一天,有个业务需要写个简单的邮件发送,但是测试了。从Apache Commons Email 上取得的例
方法一:简单邮件发送 echo “hello world" | mail -s "Subject" yanggang_2050@163.com 效果截图
方法一:简单邮件发送 echo “hello world" | mail -s "Subject" yanggang_2050@163.com 效果截图
方法一:简单邮件发送 echo 'hello world' | mail -s "Subject" -t yanggang@ithomer.com,yanggang_
方法一:简单邮件发送 echo “hello world" | mail -s "Subject" yanggang_2050@163.com 效果截图
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号