|
2 | 2 |
|
3 | 3 | ```groovy |
4 | 4 | implementation 'com.sun.mail:android-mail:1.6.0' |
5 | | - implementation 'com.sun.mail:android-activation:1.6.0' |
| 5 | + implementation 'com.sun.mail:android-activation:1.6.0' |
6 | 6 | ``` |
7 | 7 |
|
8 | 8 |
|
| 9 | +1 继承 AbstractProtocolSmtp 配置邮箱Smtp服务信息 |
| 10 | +```java |
| 11 | +public class NeteaseProtocolSmtp extends AbstractProtocolSmtp { |
| 12 | + private static final String MAIL_HOST = "smtp.163.com"; |
| 13 | + private static final int MAIL_HOST_PORT = 25; |
| 14 | + private static final int MAIL_HOST_PORT_SSL = 465;// 465 / 994 |
| 15 | + |
| 16 | + public NeteaseProtocolSmtp(EmailService emailService) { |
| 17 | + super(emailService); |
| 18 | + } |
| 19 | + |
| 20 | + @Override |
| 21 | + public EmailProtocol setupEmailProtocol() { |
| 22 | + return EmailProtocol.create(MAIL_HOST, MAIL_HOST_PORT, MAIL_HOST_PORT_SSL); |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
9 | 26 |
|
| 27 | +2 实现 IEmailFactory 工厂 配置账户信息 |
10 | 28 | ```java |
11 | | - //腾讯 QQ 邮箱发 email |
12 | | - IEmailFactory tencentEmailFactory = new TencentEmailFactory(); |
13 | | - try { |
14 | | - tencentEmailFactory.getProtocolSmtp().sendHtml("test_qq_email", "test_qq_email 内容", new Address[]{new InternetAddress(toEmail)}); |
| 29 | +public class NeteaseEmailFactory implements IEmailFactory { |
| 30 | + private static final String USER_NAME = "bsoft_app@163.com"; |
| 31 | + private static final String AUTH_CODE = "xxx";//163 的授权码 |
| 32 | + //发送方的邮箱 |
| 33 | + private static final String FROM_EMAIL = "bsoft_app@163.com"; |
| 34 | + //发送方姓名 |
| 35 | + private static final String FROM_NAME = "louisgeek_netease"; |
| 36 | + |
| 37 | + @Override |
| 38 | + public AbstractProtocolSmtp getProtocolSmtp() { |
| 39 | + return new NeteaseProtocolSmtp(EmailService.create(USER_NAME, AUTH_CODE, FROM_EMAIL, FROM_NAME)); |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
15 | 43 |
|
16 | | - } catch (AddressException e) { |
17 | | - MyLog.e(e.getMessage()); |
18 | | - } |
19 | | - //网易 163 邮箱发 email |
20 | | - File filePath = new File(getFilesDir() + "temp" + File.separator); |
| 44 | + |
| 45 | +3 发送邮件 |
| 46 | +```java |
| 47 | +// 普通 |
| 48 | + IEmailFactory neteaseEmailFactory = new NeteaseEmailFactory(); |
| 49 | + try { |
| 50 | + EmailMessage emailMessage = EmailMessage.newBuilder() |
| 51 | + .setTitle("杭船业软件有限公司") |
| 52 | + .setText("杭船业软件有限公司1") |
| 53 | + .setContent("杭船业软件有限公司2") |
| 54 | + .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) |
| 55 | + .build(); |
| 56 | + |
| 57 | + neteaseEmailFactory.getProtocolSmtp().send(emailMessage); |
| 58 | + |
| 59 | +// |
| 60 | + File imagePath = new File(Environment.getExternalStorageDirectory() + File.separator + "temp" + File.separator + "zfq.jpg"); |
| 61 | + // |
| 62 | + File filePath = new File(getFilesDir() + File.separator + "temp" + File.separator); |
21 | 63 | if (!filePath.exists()) { |
22 | 64 | filePath.mkdirs(); |
23 | 65 | } |
|
30 | 72 | fileOutputStream.write("test_email content 中文".getBytes("utf-8")); |
31 | 73 | fileOutputStream.close(); |
32 | 74 | // |
33 | | - IEmailFactory neteaseEmailFactory = new NeteaseEmailFactory(); |
34 | | - //带附件 |
35 | | - neteaseEmailFactory.getProtocolSmtp().sendHtmlWithFile("test_163_email", "test_163_email 内容", new File[]{file}, new Address[]{new InternetAddress(toEmail)}); |
36 | | - // 图文 带附件 //neteaseEmailFactory.getProtocolSmtp().sendHtmlWithImageAndFile("test_163_email", "test_163_email 内容",new File[]{imageFile}, new File[]{file}, new Address[]{new InternetAddress(toEmail)}); |
37 | | - } catch (IOException e) { |
38 | | - MyLog.e(e.getMessage()); |
39 | | - } catch (AddressException e) { |
40 | | - MyLog.e(e.getMessage()); |
41 | | - } |
42 | | - |
43 | | - |
44 | | - } |
45 | | - }); |
| 75 | + EmailMessage emailMessageWithFile = EmailMessage.newBuilder() |
| 76 | + .setTitle("test_163_email") |
| 77 | + .setText("test_163_email text") |
| 78 | + // .setContent("test_163_email 带附件") |
| 79 | + // .setFiles(new File[]{file}) |
| 80 | + .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) |
| 81 | + .build(); |
| 82 | + |
| 83 | + //带附件 |
| 84 | + neteaseEmailFactory.getProtocolSmtp().send(emailMessageWithFile); |
| 85 | + |
| 86 | +// |
| 87 | + |
| 88 | + EmailMessage emailMessageWithImage = EmailMessage.newBuilder() |
| 89 | + .setTitle("test_163_email") |
| 90 | + .setText("test_163_email text") |
| 91 | + .setContent("test_163_email 图文 <img src='cid:" + imagePath.getName() + "'/>") |
| 92 | + .setImageFiles(new File[]{imagePath}) |
| 93 | + .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) |
| 94 | + .build(); |
| 95 | + |
| 96 | + // 图文 |
| 97 | + neteaseEmailFactory.getProtocolSmtp().send(emailMessageWithImage); |
| 98 | +// |
| 99 | + EmailMessage emailMessageWithImageAndFile = EmailMessage.newBuilder() |
| 100 | + .setTitle("test_163_email") |
| 101 | + .setText("test_163_email text") |
| 102 | + .setContent("test_163_email 图文 带附件") |
| 103 | + .setImageFiles(new File[]{imagePath}) |
| 104 | + .setFiles(new File[]{file}) |
| 105 | + .setTOAddresses(new Address[]{new InternetAddress(toEmail)}) |
| 106 | + .build(); |
| 107 | + |
| 108 | + // 图文 带附件 |
| 109 | + neteaseEmailFactory.getProtocolSmtp().send(emailMessageWithImageAndFile); |
46 | 110 |
|
47 | 111 | ``` |
48 | 112 |
|
|
0 commit comments