简易的tomcat的email发送

来源:互联网 发布:老徐外设店淘宝店 编辑:程序博客网 时间:2024/04/29 18:06

下面是我参考apache官网中的email组件的案例做的小案例。

功能:在自己写的小程序中向指定的邮箱发送邮件,下面是以qq邮箱为例子。

第一步:在项目中导入commons-email-1.4.jar 、activation.jar、amil.jar、mailapi.jar、smtp.jar

第二步:qq邮箱的设置,开启smtp服务,获取授权并记下来,下面有用到


再写测试代码

package com.czy.test;import org.apache.commons.mail.Email;import org.apache.commons.mail.EmailException;import org.apache.commons.mail.SimpleEmail;public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubEmail email = new SimpleEmail();email.setHostName("smtp.qq.com");email.setAuthentication("xxxxxxxxx@QQ.com", "xxxxxxxxx");//设置的邮箱为你开启smtp服务和授权码email.setSmtpPort(465);//设置访问smtp的端口email.setSSLOnConnect(true);//设置SSL链接email.setCharset("utf-8");try {email.setFrom("xxxxxxxxxxx@QQ.com");//这里的邮箱地址是你开启smtp服务的邮箱地址(必须一样)email.setSubject("TestMail");email.setMsg("This is a test mail ... :-)");email.addTo("*******@QQ.com");//这是目的的邮箱地址email.send();} catch (EmailException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}



0 0
原创粉丝点击