java之发送邮件

在日常开发中发送邮件是很正常的,现贴出发送邮件代码

 1.实例类,只贴出所拥有的属性,持久化数据库的配置不再列出

public class Mail{
/**
	 * @description 属性说明
	 */
	private static final long serialVersionUID = 1797738641950018355L;

	/**
	 * 主键
	 */
	private String mailId;
	/**
	 * SMTP服务器
	 */
	private String smtpSer;
	/**
	 * 邮件接收端口
	 */
	private int mailPort;
	/**
	 * 验证用户名
	 */
	private String username;
	/**
	 * 验证密码
	 */
	private String password;
	/**
	 * 是否验证
	 */
	private int isVal;
	/**
	 * 发件人信箱
	 */
	private String sendBox;
	/**
	 * 默认主题
	 */
	private String deTheme;
	/**
	 * 默认接收者
	 */
	private String deReceiver;
}

 2.发送邮件

public boolean sendEmail(String[] to,String[] cc,Mail mail , String subject, String content,
			String[] attachements) {
		MimeMessage mimeMsg;// MIME邮件对象
		MimeMultipart mp;// Multipart对象,邮件内容,标题,附件等内容均添加到其中之后再生成//MimeMessage对象
		Session session;// 邮件会话对象
		Properties props = new Properties();// 系统属性
		try {
			props.put("mail.smtp.host", mail.getSmtpSer());// 设置SMTP主机
			props.put("mail.smtp.port", mail.getMailPort());
			MyAuthenticator authenticator = null;   
			if (mail.getIsVal() == 1) {
				props.put("mail.smtp.auth", "true");
				authenticator = new MyAuthenticator(mail.getUsername(),mail.getPassword());
			} else {
				props.put("mail.smtp.auth", "false");
				System.out.println("发送邮件出错,必须验证stmp!");
				return false;
			}

			session = Session.getDefaultInstance(props, authenticator);// 获得邮件会话对象
			mimeMsg = new MimeMessage(session);
			mp = new MimeMultipart();
			
			mimeMsg.setFrom(new InternetAddress(mail.getSendBox()));// 设置发信人
			
			// 设置收信人
			Address toaddress[] = new Address[to.length];
		       for(int i=0;i<to.length;i++){
		    	   toaddress[i]=InternetAddress.parse(to[i].toString())[0];
		       }
			mimeMsg.setRecipients(Message.RecipientType.TO, toaddress);
			
			// 设置抄送人
			Address ccaddress[] = new Address[cc.length];
		       for(int i=0;i<cc.length;i++){
		    	   ccaddress[i]=InternetAddress.parse(cc[i].toString())[0];
		       }
			mimeMsg.setRecipients(Message.RecipientType.CC, ccaddress);
			
			// 设置邮件体格式
			mimeMsg.setSubject(mail.getDeTheme(), "GB2312");
			BodyPart bp = new MimeBodyPart();
			bp.setContent(
					"<meta http-equiv=Content-Type content=text/html; charset=GB2312>"
							+ content, "text/html;charset=GB2312");
			mp.addBodyPart(bp);
			// 添加附件,超过10M不可以发送。
			if (attachements != null && attachements.length > 0) {
				for (int i = 0; i < attachements.length; i++) {
					if((new File(attachements[i])).exists()){
						FileInputStream fis = new FileInputStream(new File(attachements[i]));
						if((double)fis.available()/1048576 <10.0){
							MimeBodyPart mbp = new MimeBodyPart();
							FileDataSource fileds = new FileDataSource(attachements[i]);
							DataHandler dh = new DataHandler(fileds);
							mbp.setDisposition(Part.ATTACHMENT);
							mbp.setFileName(fileds.getName());
							mbp.setDataHandler(dh);
							mp.addBodyPart(mbp);
						}
					}
				}
			}
			mimeMsg.setContent(mp);
			mimeMsg.saveChanges();
			// 正式发送邮件
			Session mailSession = Session.getInstance(props, null);
			Transport transport = mailSession.getTransport("smtp");
			transport.connect((String) props.get("mail.smtp.host"),
					mail.getUsername(), mail.getPassword());
			transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
			transport.close();
			System.out.println("Mail Successfully Sended!");
			return true;
		} catch (Exception e) {
			logger.debug("发送邮件出错,详见类:UniresponseApiImpl。错误信息为:"
					+ e.getMessage());
			return false;
		}
	}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值