zoukankan      html  css  js  c++  java
  • email集成

    email集成

    mvn依赖包

    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.10</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-email</artifactId>
        <version>1.5</version>
    </dependency>
    
    

    配置文件

    mail.host=smtp.163.com
    mail.username=jack_liujj@163.com
    mail.password=test1234
    mail.title="测试结果"
    mail.touser=1300940785@qq.com,jack_liujj@163.com
    

    代码实列

    package apitest.email;
    
    import java.io.File;
    import java.net.URL;
    
    import org.apache.commons.mail.DataSourceResolver;
    import org.apache.commons.mail.EmailAttachment;
    import org.apache.commons.mail.EmailException;
    import org.apache.commons.mail.HtmlEmail;
    import org.apache.commons.mail.ImageHtmlEmail;
    import org.apache.commons.mail.resolver.DataSourceCompositeResolver;
    import org.apache.commons.mail.resolver.DataSourceFileResolver;
    import org.apache.commons.mail.resolver.DataSourceUrlResolver;
    
    public class EmailUtils {
    
    	public static void main(String[] args) {
    		try {
    //			sendMsg();
    			 String path= "E:\id_rsa_2048.pub";
    			 String path1="E:\GM_refreshpkg_p358.zip";
    			sendEmailsWithAttachments("测试结果","请查收",path,path1);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	public static void sendMsg() throws Exception {
    		// HtmlEmail mail = new HtmlEmail();
    		ImageHtmlEmail mail = new ImageHtmlEmail();
    
    		String htmlEmailTemplate = "这是一张用于测试的图片,请查收。 <img src="test.png"> ";
    //				+ " <img src="http://commons.apache.org/proper/commons-email/images/commons-logo.png">";
    		
    //		String htmlEmailTemplate = "这是一张用于测试的图片,请查收 "
    //				+ " <img src="http://commons.apache.org/proper/commons-email/images/commons-logo.png">";
    
    		// 解析本地图片和网络图片都有的html文件重点就是下面这两行;
    		// ImageHtmlEmail通过setDataSourceResolver来识别并嵌入图片
    		// 查看DataSourceResolver的继承结构发现有几个好用的子类
    		DataSourceResolver[] dataSourceResolvers = new DataSourceResolver[] { new DataSourceFileResolver(), // 添加DataSourceFileResolver用于解析本地图片
    				new DataSourceUrlResolver(new URL("http://")) };// 添加DataSourceUrlResolver用于解析网络图片,注意:new URL("http://")
    		// DataSourceCompositeResolver类可以加入多个DataSourceResolver,
    		// 把需要的DataSourceResolver放到一个数组里传进去就可以了;
    		mail.setDataSourceResolver(new DataSourceCompositeResolver(dataSourceResolvers));
    
    		String hostname = PropertiesUtils.getString("mail.host");
    		String password = PropertiesUtils.getString("mail.password");
    		String username = PropertiesUtils.getString("mail.username");
    		String[] toList = PropertiesUtils.getStringArray("mail.touser");
    		String subject = PropertiesUtils.getString("mail.title");
    		mail.setHostName(hostname); // 邮件服务器域名
    		mail.setAuthentication(username, password); // 邮箱账户
    		mail.setCharset("UTF-8"); // 邮件的字符集
    		mail.setFrom(username); // 发件人地址
    		for (String to : toList) {
    			mail.addTo(to); // 收件人地址,可以设置多个
    		}
    
    //		mail.setSubject(subject); // 邮件主题
    		mail.setSubject("测试"); // 邮件主题
    		mail.setHtmlMsg(htmlEmailTemplate); // 邮件正文
    		String rString = mail.send(); // 发送邮件
    		System.out.println(rString);
    	}
    
    	public static void sendEmailsWithAttachments(String title, String context, String... filepath)
    			throws EmailException {
    
    		String hostname = PropertiesUtils.getString("mail.host");
    		String password = PropertiesUtils.getString("mail.password");
    		String username = PropertiesUtils.getString("mail.username");
    		String[] toList = PropertiesUtils.getStringArray("mail.touser");
    
    		// Create the email message
    		HtmlEmail email = new HtmlEmail();
    		email.setHostName(hostname); // 邮件服务器域名
    		email.setAuthentication(username, password); // 邮箱账户
    		email.setCharset("UTF-8"); // 邮件的字符集
    
    		// mail.setSSLOnConnect(true); // 是否启用SSL
    		// mail.setSslSmtpPort(sslSmtpPort); // 若启用,设置smtp协议的SSL端口号
    		email.setSubject(title);//邮件标题
    		email.setFrom(username); // 发件人地址
    		email.setHtmlMsg(context); //邮件内容
    
    		for (String to : toList) {
    			email.addTo(to); // 收件人地址,可以设置多个
    		}
    		// add the attachment
    		for (String fp : filepath) {
    			EmailAttachment attachment = new EmailAttachment();
    			attachment.setPath(fp);
    			attachment.setDisposition(EmailAttachment.ATTACHMENT);
    			attachment.setDescription("测试结果");
    			File f = new File(fp);
    			attachment.setName(f.getName());
    			email.attach(attachment);
    		}
    		// send the email
    		email.send();
    	}
    
    }
    
    

    作者:我是刘先生
    地址:https://www.cnblogs.com/cekaigongchengshi/
    文章转载请标明出处,如果,您认为阅读这篇博客让您有些收获,不妨点击一下推荐按钮,据说喜欢分享的,后来都成了大神

    欢迎扫码关注微信公众号
  • 相关阅读:
    kotlin的三目运算
    oracle存储过程相关整理
    LInux系统部署Java项目
    Idea集成vue
    java执行查询存储过程
    访问分享文件路径方法
    java计算程序运行时间
    IntelliJ IDEA 最新版 2019.1 安装与激活
    oracel中将子表中一个字段多条数据合并到主表的sql
    oracel根据条件不同,统计数据sql
  • 原文地址:https://www.cnblogs.com/cekaigongchengshi/p/14072147.html
Copyright © 2011-2022 走看看