[测评系统]--测评系统核心代码库
吴祝攀
2024-06-14 499b1d27da156ddd25c7adb58a5601805d8149fe
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package com.ots.project.tool.email;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.*;
 
@Slf4j
public class SimpleMailSender {
    
    public static void sendEmail(String SMTP, String PORT, String EMAIL, String PAW, String toEMAIL, String TITLE, String CONTENT, String TYPE) throws Exception {
        
        MailSenderInfo mailInfo = new MailSenderInfo();
        mailInfo.setMailServerHost(SMTP);
        mailInfo.setMailServerPort(PORT);
        mailInfo.setValidate(true);
        mailInfo.setUserName(EMAIL);
        mailInfo.setPassword(PAW);
        mailInfo.setFromAddress(EMAIL);
        mailInfo.setToAddress(toEMAIL);
        mailInfo.setSubject(TITLE);
        mailInfo.setContent(CONTENT);
        
        SimpleMailSender sms = new SimpleMailSender();
        if ("1".equals(TYPE)) {
            sms.sendTextMail(mailInfo);
        } else {
            sms.sendHtmlMail(mailInfo);
        }
    }
    
    public static void main(String[] args) throws Exception {
        
        MailSenderInfo mailInfo = new MailSenderInfo();
        mailInfo.setMailServerHost("smtp.mxhichina.com");
        mailInfo.setMailServerPort("465");
        mailInfo.setValidate(true);
        mailInfo.setUserName("test@tai-online.com");
        mailInfo.setPassword("Wlz@2020");
        mailInfo.setFromAddress("test@tai-online.com");
        mailInfo.setToAddress("");
        mailInfo.setSubject("测试发送邮件");
        mailInfo.setContent("评测系统测试邀请链接http://139.199.11.114/exam-stu/#/demography/ed860660-95a7-4feb-aab9-f56b1eff1a3c/12/index");
        String[] attachFileNames = {"D:\\ots\\uploadPath\\upload\\94a3847d-901a-4063-9d2d-ef3a345f46d0.png", "D:\\ots\\uploadPath\\upload\\94a3847d-901a-4063-9d2d-ef3a345f46d0.png"};
        mailInfo.setAttachFileNames(attachFileNames);
        
        SimpleMailSender sms = new SimpleMailSender();
        sms.sendTextMail(mailInfo);
 
 
    }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    
    public RushMailResult sendPushMail(RushMailInfo into) {
        RestTemplate infoRestTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        if (Objects.nonNull(into.getAttachFileNames())) {
            String[] attachFileNames = into.getAttachFileNames();
            for (int i = 0; i < attachFileNames.length; i++) {
                File file = new File(attachFileNames[i]);
                FileSystemResource resource = new FileSystemResource(file);
                param.add(file.getName(), resource);
            }
        }
        param.add("account", into.getAccount());
        param.add("receivename", "receivename");
        param.add("toemail", into.getEmails());
        param.add("title", into.getTitle());
        param.add("content", into.getContext());
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(param, headers);
        RushMailResult rushMailResult = new RushMailResult();
        try {
            ResponseEntity<JSONObject> response = infoRestTemplate.exchange(into.getUrl(), HttpMethod.POST, entity, JSONObject.class);
            JSONObject responseBody = response.getBody();
            rushMailResult = JSONObject.toJavaObject(responseBody, RushMailResult.class);
        } catch (Exception e) {
            rushMailResult.setResult(false);
            rushMailResult.setMessage(e.getMessage());
            log.error("sendPushMail:{}",e.getMessage(), e);
        }
        return rushMailResult;
    }
    
    public RushMailResultCheck mailResult (String url, String account, String date) {
        RestTemplate infoRestTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("account", account);
        param.add("date", date);
        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(param, headers);
        ResponseEntity<JSONObject> response = null;
        RushMailResultCheck check = new RushMailResultCheck();
        try {
            response = infoRestTemplate.exchange(url, HttpMethod.POST, entity, JSONObject.class);
            JSONObject responseBody = response.getBody();
            System.out.println(responseBody);
            check = JSONObject.toJavaObject(responseBody, RushMailResultCheck.class);
        } catch (Exception e) {
 
            check.setResult(false);
            check.setMessage(e.getMessage());
        }
        return check;
    }
    
    public boolean sendTextMail (MailSenderInfo mailInfo) throws Exception {
        
        Message mailMessage = initMailMessage(mailInfo);
 
 
 
 
 
        return true;
    }
    
    public boolean sendHtmlMail(MailSenderInfo mailInfo) throws Exception {
        Message mailMessage = initMailMessage(mailInfo);
        
        Multipart mainPart = new MimeMultipart();
        
        BodyPart html = new MimeBodyPart();
        
        html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        
        mailMessage.setContent(mainPart);
        
        Transport.send(mailMessage);
        return true;
    }
    private Message initMailMessage(MailSenderInfo mailInfo) throws MessagingException {
        
        MyAuthenticator authenticator = null;
        Properties pro = mailInfo.getProperties();
        
        if (mailInfo.isValidate()) {
            authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
        }
        
        Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
        
        Message mailMessage = new MimeMessage(sendMailSession);
        
        Address from = new InternetAddress(mailInfo.getFromAddress());
        
        mailMessage.setFrom(from);
        
        Address to = new InternetAddress(mailInfo.getToAddress());
        
        mailMessage.setRecipient(Message.RecipientType.TO, to);
        
        mailMessage.setSubject(mailInfo.getSubject());
        
        mailMessage.setSentDate(new Date());
        
        String mailContent = mailInfo.getContent();
        MimeMultipart mm_text_image = new MimeMultipart();
        MimeBodyPart text = new MimeBodyPart();
        text.setContent(mailContent, "text/html;charset=UTF-8");
        mm_text_image.addBodyPart(text);
        mm_text_image.setSubType("related");    
        MimeBodyPart text_image = new MimeBodyPart();
        text_image.setContent(mm_text_image);
        
        MimeMultipart mm = new MimeMultipart();
        mm.addBodyPart(text_image);
        String[] attachFileNames = mailInfo.getAttachFileNames();
        if (Objects.nonNull(attachFileNames) && attachFileNames.length > 0) {
            for (int i = 0; i < attachFileNames.length; i++) {
                MimeBodyPart attachment = getMimeBodyPart(attachFileNames[i]);
                mm.addBodyPart(attachment);     
            }
            mm.setSubType("mixed");         
            
        }
        mailMessage.setContent(mm);
        Transport transport = sendMailSession.getTransport();
        transport.connect(mailInfo.getUserName(), mailInfo.getPassword());
        Address[] allRecipients = mailMessage.getAllRecipients();
        transport.sendMessage(mailMessage, allRecipients);
        transport.close();
        return mailMessage;
    }
    private MimeBodyPart getMimeBodyPart(String name) throws MessagingException {
        
        MimeBodyPart attachment = new MimeBodyPart();
        
        DataHandler dh2 = new DataHandler(new FileDataSource(name));
        
        attachment.setDataHandler(dh2);
        
        try {
            attachment.setFileName(MimeUtility.encodeText(dh2.getName()));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return attachment;
    }
    
    public boolean sendMail(String title, String content, String type, String tomail) throws Exception {
        
        MailSenderInfo mailInfo = new MailSenderInfo();
        mailInfo.setMailServerHost("smtp.qq.com");
        mailInfo.setMailServerPort("25");
        mailInfo.setValidate(true);
        mailInfo.setUserName("itfather@1b23.com");
        mailInfo.setPassword("tttt");
        mailInfo.setFromAddress("itfather@1b23.com");
        mailInfo.setToAddress(tomail);
        mailInfo.setSubject(title);
        mailInfo.setContent(content);
        
        SimpleMailSender sms = new SimpleMailSender();
        if ("1".equals(type)) {
            return sms.sendTextMail(mailInfo);
        } else if ("2".equals(type)) {
            return sms.sendHtmlMail(mailInfo);
        }
        return false;
    }
}