java发送邮件mail.jar[通俗易懂]

2022-08-28 11:14:37 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

这里短信发送是基于对mail.jar 封装的jar包开发的,但是基本参数是一样的,可以参考

代码语言:javascript复制
package com.dw;

import com.dareway.framework.exception.AppException;
import com.dareway.framework.mail.MailInfo;
import com.dareway.framework.mail.MailUtil;
import com.dareway.framework.util.DataObject;

/**
 * 需要连接互联网的工具类
 * @author hq
 * @version 1.0
 */
public class NetworkUtil {

	//发邮件
	public static DataObject sendMail(DataObject para) throws AppException {
		DataObject result = new DataObject();
		System.out.println("开始发邮件");

		try {
			if(GAUNames.EmailSmtp==null || "".equals(GAUNames.EmailSmtp)) {//邮件协议:smtp.163.com
				System.out.println("GAUNames.EmailSmtp"   GAUNames.EmailSmtp);
				throw new Exception("系统参数EmailSmtp未配置,请联系管理员");
			}
			if(GAUNames.EmailUser==null || "".equals(GAUNames.EmailUser)) {//用户名:
				System.out.println("GAUNames.EmailUser"   GAUNames.EmailUser);
				throw new Exception("系统参数EmailUser未配置,请联系管理员");
			}
			if(GAUNames.EmailPassword==null || "".equals(GAUNames.EmailPassword)) {//密码,新申请邮箱为授权码:
				System.out.println("GAUNames.EmailPassword"   GAUNames.EmailPassword);
				throw new Exception("系统参数EmailPassword未配置,请联系管理员");
			}
			if(GAUNames.EmailFrom==null || "".equals(GAUNames.EmailFrom)) {//发送邮箱
				System.out.println("GAUNames.EmailFrom"   GAUNames.EmailSmtp);
				throw new Exception("系统参数EmailFrom未配置,请联系管理员");
			}
			
			String mailTitle = para.getString("mailTitle");
			String autoMsg = para.getString("autoMsg");
			String email = para.getString("email");
			String[] toMail = { email };
			MailInfo mailinfo = new MailInfo();
			mailinfo.setSmtp(GAUNames.EmailSmtp);
			mailinfo.setSmtpUser(GAUNames.EmailUser);
			mailinfo.setSmtpPassword(GAUNames.EmailPassword);
			mailinfo.setFrom(GAUNames.EmailFrom);
			mailinfo.setSubject(mailTitle);// 设置邮件主题
			mailinfo.setContent(autoMsg);// 设置邮件内容
			mailinfo.setTo(toMail);// 设置收件人
			// 要用公共邮箱给上面那些人发邮件,这个是公共邮箱的密码
			new MailUtil(mailinfo).send();
			result.put("result", true);
			System.out.println("完成发邮件");
		} catch (Exception e) {
			result.put("errmsg", "发送邮件失败!异常信息:"   e.getMessage());
			result.put("result", false);
		}
		return result;
	}
	
//	public static void main(String[]arg) throws AppException {
//		DataObject pdo = new DataObject();
//		pdo.put("mailTitle", "dddd");
//		pdo.put("autoMsg", "ssss");
//		//pdo.put("email", "smartga_dyetdz@163.com");
//		pdo.put("email", "xubenxismile@163.com");
//		sendMail(pdo);
//	}
	
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/146398.html原文链接:https://javaforall.cn

0 人点赞