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
| //Отправка на email сообщения
public boolean send_email(String email){
MailSenderClass sender = new MailSenderClass(user, password);
try {
sender.sendMail("blablabla", "12345", user, email);
return true;
} catch (Exception e) {
e.printStackTrace();
Toast toast = Toast.makeText(getApplicationContext(),
email, Toast.LENGTH_SHORT);
toast.show();
return false;
}
}
public final class JSSEProvider extends Provider {
public JSSEProvider() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");
put("KeyManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
return null;
}
});
}
}
public class MailSenderClass extends javax.mail.Authenticator {
private Session session;
private String user;
private String password;
Multipart _multipart;
{
Security.addProvider(new JSSEProvider());
}
public MailSenderClass(String user, String password) {
this.user=user;
this.password=password;
_multipart = new MimeMultipart();
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
MimeMessage message = new MimeMessage(session);
// Кто
message.setSender(new InternetAddress(sender));
// О чем
message.setSubject(subject);
// Кому
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(recipients));
// Хочет сказать
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
_multipart.addBodyPart(messageBodyPart);
message.setContent(_multipart);
Transport.send(message);
}
} |