shichongfu
2023-04-25 ce0b49552668d3331055e2b1a1447a743dc54939
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.product.admin.util.email;
 
 
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.product.admin.config.SystemCode;
import com.product.core.config.Global;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
 
 
/**
 * mailgun邮件发送工具类
* Copyright  LX-BASE
* @Title: LX-BASE-
* @Project: MailgunSendEmail
* @Date:  2020-09-29 16:36   
* @Author: 杜洪波
* @Description:
 */
public class MailgunSendEmail {
    
//    private static Logger logger = LoggerFactory.getLogger(MailgunSendEmail.class);
    
    //正式账号
    
//    private static final String DOMAIN  = "mail.purtato.com";
//    private static final String API_URI = "https://api.mailgun.net/v3/"+DOMAIN+"/messages";
//    private static final String API_USER = "brianho@mail.purtato.com";
    
//    private static final String DOMAIN  = "mail.essencehr.io";
//    private static final String API_USER = "accountservices@mail.essencehr.io";
//    private static final String API_KEY = "8d52b0a748b20c61331587fa9a3fe6fc-cb3791c4-8cf158ef";
    
    
//    private static final String DOMAIN = "test-mail.essencehr.io";
//    private static final String API_USER = "accountservices@test-mail.essencehr.io";    
//    private static final String API_KEY = "8d52b0a748b20c61331587fa9a3fe6fc-cb3791c4-8cf158ef";
    
/*
    public static void sendMail() {
        HttpResponse<JsonNode> request=null;
        int finalCode=200;
        try {
            request = Unirest.post("https://api.mailgun.net/v3/"+DOMAIN+"/messages")
                 .basicAuth("api", API_KEY)
                 .field("from", API_USER)
                 .field("to", "1074825718@qq.com")
                 .field("subject", "gf")
                 .field("text", "sdfafs")
                 .asJson();
            finalCode=request.getStatus();
        } catch (UnirestException e) {
            e.printStackTrace();
            finalCode=500;
        }
    }
    
    
    public static void main(String[] args) {
        sendMail();
    }
*/
    
    /**
     *     发送html和text邮件
     * @param fseMailInfo    邮件主体信息
     * @param attachment    附件
     * @throws BaseException
     */
    public static void sendMail(FieldSetEntity fseMailInfo) throws BaseException {
        
        String mailType="text";
        String mailContent=fseMailInfo.getString("mail_content");
        if (mailContent.indexOf("</html>")>-1) {
            mailType="html";
        }
        
        int finalCode=200;
        
        HttpResponse<JsonNode> request=null;
        try {
            request = Unirest.post("https://api.mailgun.net/v3/"+Global.getSystemConfig("mailgun.mail.domain", "")+"/messages")
                 .basicAuth("api", Global.getSystemConfig("mailgun.mail.key", ""))
                 .field("from", Global.getSystemConfig("mailgun.mail.user", ""))
                 .field("to", fseMailInfo.getString("addressee"))
                 .field("subject", fseMailInfo.getString("mail_title"))
                 .field(mailType, mailContent)
                 .asJson();
            finalCode=request.getStatus();
        } catch (UnirestException e) {
            e.printStackTrace();
            finalCode=500;
        } finally {
            resultJudge(finalCode,fseMailInfo);
        }
    }
    
    
    /**
     *     发送状态判断
     * @param finalCode 发送结果
     * @param fseMailInfo    邮件信息
     * @throws BaseException
     */
    public static void resultJudge(int finalCode,FieldSetEntity fseMailInfo) throws BaseException {
        
        fseMailInfo.setValue("smtp_username", Global.getSystemConfig("mailgun.mail.user", ""));
        fseMailInfo.setValue("smtp_license_code", Global.getSystemConfig("mailgun.mail.key", ""));
        
        int status=2;
        String resultInfo=null;
        
        switch (finalCode) {
        case 200:
            status=1;
            resultInfo=SystemCode.SYSTEM_MAILGUN_RESULT_ONE.getText();
            break;
        case 400:
            resultInfo=SystemCode.SYSTEM_MAILGUN_RESULT_TWO.getText();
            break;
        case 401:
            resultInfo=SystemCode.SYSTEM_MAILGUN_RESULT_THREE.getText();
            break;
        case 402:
            resultInfo=SystemCode.SYSTEM_MAILGUN_RESULT_FOUR.getText();
            break;    
        case 404:
            resultInfo=SystemCode.SYSTEM_MAILGUN_RESULT_FIVE.getText();
            break;
        case 413:
            resultInfo=SystemCode.SYSTEM_MAILGUN_RESULT_SIX.getText();
            break;
        default:
            resultInfo=SystemCode.SYSTEM_MAILGUN_RESULT_SEVEN.getText();
            break;
        }
        fseMailInfo.setValue("send_stat", status);
        fseMailInfo.setValue("failure_reason", resultInfo);
    }
}