XMTT 8 gadi atpakaļ
vecāks
revīzija
3a17c56199

+ 56 - 15
src/main/java/com/ekexiu/console/system/service/EmailService.java

1
package com.ekexiu.console.system.service;
1
package com.ekexiu.console.system.service;
2
2
3
import com.ekexiu.console.system.mail.MailService;
3
import com.ekexiu.console.system.mail.MailService;
4
import org.apache.log4j.Logger;
5
import org.jfw.apt.annotation.Autowrie;
4
import org.jfw.apt.annotation.Autowrie;
6
import org.jfw.apt.annotation.Nullable;
5
import org.jfw.apt.annotation.Nullable;
7
import org.jfw.apt.web.annotation.Path;
6
import org.jfw.apt.web.annotation.Path;
8
import org.jfw.apt.web.annotation.operate.Post;
7
import org.jfw.apt.web.annotation.operate.Post;
9
import org.jfw.apt.web.annotation.param.JdbcConn;
10
8
11
import javax.mail.MessagingException;
9
import javax.mail.MessagingException;
12
import java.sql.Connection;
13
import java.sql.SQLException;
10
import java.util.ArrayList;
11
import java.util.List;
14
12
15
/**
13
/**
16
 * Created by TT on 2017/5/11.
14
 * Created by TT on 2017/5/11.
17
 */
15
 */
18
@Path
16
@Path
19
public class EmailService {
17
public class EmailService {
20
    private Logger logger = Logger.getLogger(EmailService.class);
21
18
22
    @Autowrie
19
    @Autowrie
23
    private MailService mailservice;
20
    private MailService mailservice;
25
    private String phoneContentTemplate;
22
    private String phoneContentTemplate;
26
23
27
    private String content = "contentKey";
24
    private String content = "contentKey";
28
    private String contentDefault = "科袖网是一个为科研工作者服务的平台。我们将为您免费提供一个专属主页,您可以对外展示您的科研经历、成果以及资源,我们的平台将面向全国的企业与科研机构进行推广,您将得到更多与企业的合作、与同行交流的机会。";
25
    private String contentDefault = "";
29
26
30
    private String appellation = "appellationKey";
27
    private String appellation = "appellationKey";
31
    private String appellationDefault = "您好";
28
    private String appellationDefault = "";
32
29
33
    private String phoneReplaceKey = "mobileCode";
30
    private String phoneReplaceKey = "mobileCode";
34
    private String inviteReplacePhone = "phoneKey";
31
    private String inviteReplacePhone = "phoneKey";
136
    /**
133
    /**
137
     * 给指定邮箱发送邀请邮件
134
     * 给指定邮箱发送邀请邮件
138
     *
135
     *
139
     * @param con
140
     * @param mobilePhones 手机号数组
136
     * @param mobilePhones 手机号数组
141
     * @param emails       邮箱
137
     * @param emails       邮箱
142
     * @param inviteCodes  邀请码
138
     * @param inviteCodes  邀请码
143
     * @throws SQLException
139
144
     * @throws MessagingException
140
     * @throws MessagingException
145
     */
141
     */
146
    @Post
142
    @Post
147
    @Path("/sendmail")
143
    @Path("/sendmail")
148
    public int sendmail(@Nullable String content,@Nullable String appellation, @Nullable String[] mobilePhones, String[] emails, String[] inviteCodes)
149
            throws SQLException, MessagingException {
144
    public EmailInfo sendmail(@Nullable String content, @Nullable String appellation, @Nullable String[] mobilePhones, String[] emails, String[] inviteCodes, String[] names)
145
            throws MessagingException {
146
        EmailInfo emailInfo = new EmailInfo();
147
        List<String> email = new ArrayList<>();
148
        List<String> name = new ArrayList<>();
149
        int count = 0;
150
        for (int i = 0; i < emails.length; i++) {
150
        for (int i = 0; i < emails.length; i++) {
151
            if (mobilePhones[i] == "") {
151
            if (mobilePhones[i] == "") {
152
                mobilePhones[i] = null;
152
                mobilePhones[i] = null;
157
            if (appellation == null) {
157
            if (appellation == null) {
158
                appellation = this.appellationDefault;
158
                appellation = this.appellationDefault;
159
            }
159
            }
160
            this.sendInviteMail(content, appellation,emails[i], mobilePhones[i], inviteCodes[i]);
161
            this.logger.info("成功发送邀请邮件:" + emails[i]);
160
            try {
161
                this.sendInviteMail(content, appellation, emails[i], mobilePhones[i], inviteCodes[i]);
162
            } catch (MessagingException e) {
163
                count++;
164
                email.add(emails[i]);
165
                name.add(names[i]);
166
                continue;
167
            }
162
        }
168
        }
163
        return emails.length;
169
        emailInfo.setCount(emails.length-count);
170
        emailInfo.setEmails(email);
171
        emailInfo.setNames(name);
172
        return emailInfo;
164
    }
173
    }
165
174
166
    public void sendInviteMail(String content,String appellation,String email, @Nullable String mobilePhone, String inviteCode)
175
    public void sendInviteMail(String content, String appellation, String email, @Nullable String mobilePhone, String inviteCode)
167
            throws MessagingException {
176
            throws MessagingException {
168
        String mailContent = this.inviteMailContentTempalte;
177
        String mailContent = this.inviteMailContentTempalte;
169
        mailContent = mailContent.replaceAll(this.inviteReplaceEmail, email);
178
        mailContent = mailContent.replaceAll(this.inviteReplaceEmail, email);
180
        this.mailservice.sendSimpleMail(email, mailContent, null, this.inviteMailSubject);
189
        this.mailservice.sendSimpleMail(email, mailContent, null, this.inviteMailSubject);
181
    }
190
    }
182
191
192
    public static class EmailInfo {
193
194
        private int count;
195
        private List<String> emails;
196
        private List<String> names;
197
198
        public List<String> getNames() {
199
            return names;
200
        }
201
202
        public void setNames(List<String> names) {
203
            this.names = names;
204
        }
205
206
        public int getCount() {
207
            return count;
208
        }
209
210
        public void setCount(int count) {
211
            this.count = count;
212
        }
213
214
        public List<String> getEmails() {
215
            return emails;
216
        }
217
218
        public void setEmails(List<String> emails) {
219
            this.emails = emails;
220
        }
221
222
    }
223
183
}
224
}