1.简单电子邮件例子之二:

Email email = new SimpleEmail();email.setHostName("smtp.qq.com");//qqemail.setSmtpPort(465);//iportemail.setAuthenticator(new DefaultAuthenticator("fromUserName", "fromUserPwd"));email.setSSLOnConnect(true);try {email.setFrom("fromUserName@**.com");email.setSubject("TestMail");email.setMsg("This is a test mail ... :-)");email.addTo("toUserName@**.com");email.send();} catch (EmailException e) {e.printStackTrace();}

//第二个

Email email = new SimpleEmail();email.setHostName("smtp.qq.com");email.setAuthentication("fromUserName", "fromUserPwd");email.setCharset("UTF-8"); email.setTLS(true); try {email.setFrom("fromUserName@**.com");email.setSubject("TestMail");email.setMsg("This is a test mail ... :-)");email.addTo("toUserName@**.com");email.send();} catch (EmailException e) {e.printStackTrace();}

2.添加附件,附件都是MIME进行编码

以下这种方式,添加的附件是失败的,附件的类型错误,但是内容正确
EmailAttachment attachment = new EmailAttachment();attachment.setPath("g:/promise.jpg");attachment.setDisposition(EmailAttachment.ATTACHMENT);attachment.setDescription("to me");attachment.setName("to me ");// Create the email messageMultiPartEmail email = new MultiPartEmail();email.setHostName("smtp.qq.com");
//        email.setSmtpPort(465);//iport
//        email.setAuthenticator(new DefaultAuthenticator("fromUserName", "fromUserPwd"));
//        email.setSSLOnConnect(true);email.setAuthentication("fromUserName", "fromUserPwd");email.setCharset("UTF-8"); email.setTLS(true); try {email.addTo("toUserName@**.com");email.setFrom("fromUserName@**.com", "Me");email.setSubject("The picture");email.setMsg("Here is the picture you wanted");// add the attachmentemail.attach(attachment);// send the emailemail.send();} catch (EmailException e) {e.printStackTrace();}

第二个,发送特定url下的资源,作为附件

EmailAttachment attachment = new EmailAttachment();try {//特定url出资源作为附件,发送,在接收的时候,会下载该资源,然后作为附件attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));} catch (MalformedURLException e1) {e1.printStackTrace();}//  attachment.setPath("g:\\promise.jpg");attachment.setDisposition(EmailAttachment.ATTACHMENT);attachment.setDescription("to me");attachment.setName("to me ");// Create the email messageMultiPartEmail email = new MultiPartEmail();email.setHostName("smtp.qq.com");
//        email.setSmtpPort(465);//iport
//        email.setAuthenticator(new DefaultAuthenticator("fromUserName", "fromUserPwd"));
//        email.setSSLOnConnect(true);email.setAuthentication("fromUserName", "fromUserPwd");email.setCharset("UTF-8"); email.setTLS(true); try {email.addTo("toUserName@**.com");email.setFrom("fromUserName@**.com", "Me");email.setSubject("The picture");email.setMsg("Here is the picture you wanted");// add the attachmentemail.attach(attachment);// send the emailemail.send();} catch (EmailException e) {e.printStackTrace();}

3.发送页面邮件

 // Create the email messageHtmlEmail email = new HtmlEmail();email.setHostName("smtp.qq.com");email.setAuthentication("fromUserName", "fromUserPwd");email.setCharset("UTF-8"); email.setTLS(true); try {email.addTo("toUserName@163.com", "John Doe");email.setFrom("fromUserName@qq.com", "Me");email.setSubject("Test email with inline image");// embed the image and get the content idURL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");String cid = email.embed(url, "Apache logo");// set the html messageemail.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");// set the alternative messageemail.setTextMsg("Your email client does not support HTML messages");// send the emailemail.send();} catch (MalformedURLException e) {e.printStackTrace();} catch (EmailException e) {// TODO Auto-generated catch blocke.printStackTrace();}

补充内容:

The JavaMail API supports a debugging option that will can be very useful if you run into problems. You can activate debugging on any of the mail classes by calling setDebug(true). The debugging output will be written to System.out.

Sometimes you want to experiment with various security setting or features of commons-email. A good starting point is the test class EmailLiveTest and EmailConfigurationwhich are used for testing commons-email with real SMTP servers.

Authentication

If you need to authenticate to your SMTP server, you can call the setAuthentication(userName,password) method before sending your email. This will create an instance ofDefaultAuthenticator which will be used by the JavaMail API when the email is sent. Your server must support RFC2554 in order for this to work.

You can perform a more complex authentication method such as displaying a dialog box to the user by creating a subclass of the javax.mail.Authenticator object. You will need to override the getPasswordAuthentication() method where you will handle collecting the user's information. To make use of your new Authenticator class, use theEmail.setAuthenticator method.

Security

Nowadays you should not use plain SMTP protocol when using public SMTP servers but there is a some confusion regarding the available options.

Two commons options are using

  • STARTTLS on port 25
  • SSL on port 465

The following definitions were taken from Wikipedia

  • STARTTLS is an extension to plain text communication protocols, which offers a way to upgrade a plain text connection to an encrypted (TLS or SSL) connection instead of using a separate port for encrypted communication.
  • Transport Layer Security (TLS) and its predecessor, Secure Sockets Layer (SSL), are cryptographic protocols that provide communication security over the Internet.TLS and SSL encrypt the segments of network connections above the Transport Layer, using asymmetric cryptography for key exchange, symmetric encryption for privacy, and message authentication codes for message integrity.

In addition you can force the following security checks (which are disabled by default)

  • When using a secured transport (STARTTLS or SSL) you can force validating the server's certificate by calling Email.setSSLCheckServerIdentity(true). Having said that this does not seem to work on any of my test servers (GMAIL, GMX).
  • Enforce using STARTTLS by calling Email.setStartTLSRequired(true)

Handling Bounced Messages

Normally, messages which cannot be delivered to a recipient are returned to the sender (specified with the from property). However, in some cases, you'll want these to be sent to a different address. To do this, simply call the setBounceAddress(emailAddressString) method before sending your email.

Technical notes: When SMTP servers cannot deliver mail, they do not pay any attention to the contents of the message to determine where the error notification should be sent. Rather, they refer to the SMTP "envelope sender" value. JavaMail sets this value according to the value of the mail.smtp.from property on the JavaMail Session. (Commons Email initializes the JavaMail Session using System.getProperties()) If this property has not been set, then JavaMail uses the "from" address. If your email bean has the bounceAddress property set, then Commons Email uses it to set the value of mail.smtp.from when the Session is initialized, overriding any other value which might have been set.

Note: This is the only way to control the handling of bounced email. Specifically, the "Errors-to:" SMTP header is deprecated and cannot be trusted to control how a bounced message will be handled. Also note that it is considered bad practice to send email with an untrusted "from" address unless you also set the bounce address. If your application allows users to enter an address which is used as the "from" address on an email, you should be sure to set the bounce address to a known good address.

参考资料:

Java Email相关推荐

  1. Java Email 发HTML邮件工具 采用 freemarker模板引擎渲染

    Java Email 发HTML邮件工具 采用 freemarker模板引擎 1.常用方式对比 Java发送邮件有很多的实现方式 第一种:Java 原生发邮件mail.jar和activation.j ...

  2. Java Email Server

    点击打开链接 Introduction Java Mail Server is a Java SMTP and POP3 e-mail server. This project was inspire ...

  3. java email怎么设置端口号_java mail 设置参数

    Session需要使用java.util.Properties来构造,常用的用来构造Session的属性: 属性名 含义 mail.smtp.user SMTP的缺省用户名. mail.smtp.ho ...

  4. java email bean_JavaWeb学习笔记-第四章JavaBean技术

    第四章 JavaBean技术 4.2.2 使用JavaBean的意义 如果使HTML代码与Java代码相分离,将Java代码单独封装成为一个处理某种业务逻辑的类,然后在JSP页面中调用此类,就可以降低 ...

  5. java email qq邮箱 与 阿里企业邮箱/个人邮箱

    贴一个例子仅供参考  QQ邮箱 import org.apache.commons.mail.DefaultAuthenticator; import org.apache.commons.mail. ...

  6. java email邮件发送465

    为什么使用465端口呢: 因为阿里云服务器以安全为由将25端口给封了,无法使用25号端口发送邮件,所以只有使用465端口啦. 实现java发送邮件的过程大体有以下几步: 1. 准备一个properti ...

  7. 基于JSP的JAVA email邮件系统研发

    欢迎添加微信互相交流学习哦! 项目源码:https://gitee.com/oklongmm/biye2 摘 要 由于网络中邮件管理系统繁多.业务需求多变,基于各邮件服务器的邮件管理系统的维护和业务变 ...

  8. 基于java Email的电子邮件发送

    Email 电子邮件,用java实现email的发送,首先我们需要了解一下email发送的大概过程.简单来说,发送email,需要从用户的电脑邮件软件(MUA),发送到邮件服务器(MTA)上,在通过若 ...

  9. 使用Java Email发送邮件

    前言Email就是电子邮件.电子邮件的应用已经有几十年的历史了,我们熟悉的邮箱地址比如abc@example.com,邮件软件比如Outlook.网易闪电邮.Foxmail都是用来收发邮件的.当然,使 ...

  10. 网络编程----基于Java Email的电子邮件发送

    前言 电子邮件(electronic mail,简称E-mail,标志:@)又称电子信箱.电子邮政,它是-种用电子手段提供信息交换的通信方式.电子邮件指用电子手段传送信件.单据.资料等信息的通信方法. ...

最新文章

  1. java servlet html文件_Servlet生成html页面
  2. [转]图片自动缩放 js图片缩放
  3. [已经验证通过]xp sp2 不支持WPA协议的解决办法
  4. cgroup代码浅析(2)
  5. optee中的thread_vector_table线程向量表
  6. 专注于网络安全的数据中心在莫斯科开通运营
  7. 文件上传函数(get post assert)
  8. vue事件委托传递节点防止向下传递穿透
  9. 联想重装系统去掉保护_如何去掉联想硬盘保护系统?
  10. Geronimo入门与进阶
  11. Unity3D 学习笔记 —— Tween对象的实现与动作管理
  12. 『交换机连交换机』交换机多种化连接方式
  13. JAVA北京时间转换为世界协调时
  14. 银达汇智 智慧综合管理平台 FileDownLoad.aspx 任意文件读取漏洞
  15. JS实现元素自动移动
  16. 基于VUE的后台管理系统
  17. 计算机组成原理实验 内容,计算机组成原理实验报告
  18. SpringCloud版本Hoxton SR5 --- 第一讲:认识
  19. 2022-2028年中国开源软件行业市场专项调研及竞争战略分析报告
  20. 真人演示——怎样练腹肌最快最有效听语音

热门文章

  1. python try命令_python try语句(try/except/else/finally) Assertions
  2. 银行排队系统 c语言,C++实现银行排队系统
  3. python中key什么意思_Python中字典的key都可以是什么
  4. 王道数据结构2.2.4——1、设计递归算法删除不带头结点的单链表L中所有值为x的结点
  5. 市场营销行业拓客的10个经典方法
  6. jQuery动画——animate()
  7. 复数与三角函数的转换
  8. 计算机学院年会,重庆大学计算机学院举行2019年迎新晚会
  9. 如何提升你的编程能力?年轻程序员听好了,我给你的这几条建议一定会对你有所帮助
  10. Kali实现ARP欺骗