java实现内网系统通过跳板机往外网发送邮件和短信

内网系统通过跳板机往外网发送邮件和短信,内网系统实现业务数据的处理和httpclient传递参数,跳板机上部署程序实现参数的接收。

1、使用httpclient传递数据,HttpClientUtil 工具类为:


import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.sk.webapp.skims.common.vo.HttpClientTransInfo;

public class HttpClientUtil {

	/**
	 * 定时任务执行接口参数传递
	 */
	public static void doPost2(Map<String, String> auditMap) {

		// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();

		// 创建Post请求
		HttpPost httpPost = new HttpPost("http://localhost:8080/sendmail/controller/getFixedTimeInfoInterface4Audit");

		String jsonString = JSON.toJSONString(auditMap);

		StringEntity entity = new StringEntity(jsonString, "UTF-8");

		// post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
		httpPost.setEntity(entity);

		httpPost.setHeader("Content-Type", "application/json;charset=utf8");

		// 响应模型
		CloseableHttpResponse response = null;
		try {
			// 由客户端执行(发送)Post请求
			response = httpClient.execute(httpPost);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();

			System.out.println("响应状态为:" + response.getStatusLine());
			if (responseEntity != null) {
				System.out.println("响应内容长度为:" + responseEntity.getContentLength());
				System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

2、跳板机程序接口类(步骤1中直接调用的action类方法)


import java.io.IOException;
import java.util.Map.Entry;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sk.common.exception.BizException;
import com.sk.common.util.CommonUtils;
import com.sk.common.util.EmailHelper;

@Controller
public class SendMailBB {
	
	@RequestMapping(value = "/getFixedTimeInfoInterface4Audit", method = RequestMethod.POST)
	@ResponseBody
	public void getFixedTimeInfoInterface4Audit(@RequestBody String jsonStr) throws BizException {	
		
		HttpClient client = new HttpClient();  
		PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn"); 
		
		System.out.println(jsonStr);
		
		JSONObject jsonObject = JSONObject.parseObject(jsonStr);
		
		//发送邮件
		EmailHelper.sendEmail("邮箱账号", "标题", "内容");
		
		//发送短信
        post = new PostMethod("http://gbk.sms.webchinese.cn");  
        post.addRequestHeader("Content-Type",  
                "application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码  
        NameValuePair[] data = { new NameValuePair("Uid", "用户名"),// 注册的用户名  
                new NameValuePair("Key", "密钥"),// 注册成功后,登录网站后得到的密钥  
                new NameValuePair("smsMob", "13912345678"),// 手机号码  
                new NameValuePair("smsText", "短信内容") };// 短信内容  
        post.setRequestBody(data);  
        try {
			client.executeMethod(post);
			Header[] headers = post.getResponseHeaders();  
	        int statusCode = post.getStatusCode();  
	        System.out.println("statusCode:" + statusCode);  
	        for (Header h : headers) {  
	            System.out.println("---" + h.toString());  
	        }  
	        String result = new String(post.getResponseBodyAsString().getBytes("gbk"));  
	        System.out.println(result);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  	
		
    }
}


import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailHelper {
    
    public static void sendEmail(String someone, String subject, String content){
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.mxhichina.com");
        props.setProperty("mail.smtp.auth", "true");
        
        Authenticator authenticator = new Authenticator(){
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("邮箱账号", "密码");
            }
        };
        Session session = Session.getDefaultInstance(props, authenticator);
        session.setDebug(true);
        Message message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress("邮箱账号"));
            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(someone));
            //message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("测试的接收的邮件多个以逗号隔开"));
            try {
                message.setSubject(subject);
                message.setContent(content,"text/html;charset=UTF-8");
                Transport.send(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
    
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值