001.
import
javax.activation.DataHandler;
002.
import
javax.activation.DataSource;
003.
import
javax.mail.Message;
004.
import
javax.mail.PasswordAuthentication;
005.
import
javax.mail.Session;
006.
import
javax.mail.Transport;
007.
import
javax.mail.internet.InternetAddress;
008.
import
javax.mail.internet.MimeMessage;
009.
import
java.io.ByteArrayInputStream;
010.
import
java.io.IOException;
011.
import
java.io.InputStream;
012.
import
java.io.OutputStream;
013.
import
java.security.Security;
014.
import
java.util.Properties;
015.
016.
public
class
GMailSender
extends
javax.mail.Authenticator {
017.
private
String mailhost =
"smtp.gmail.com"
;
018.
private
String user;
019.
private
String password;
020.
private
Session session;
021.
022.
static
{
023.
Security.addProvider(
new
com.provider.JSSEProvider());
024.
}
025.
026.
public
GMailSender(String user, String password) {
027.
this
.user = user;
028.
this
.password = password;
029.
030.
Properties props =
new
Properties();
031.
props.setProperty(
"mail.transport.protocol"
,
"smtp"
);
032.
props.setProperty(
"mail.host"
, mailhost);
033.
props.put(
"mail.smtp.auth"
,
"true"
);
034.
props.put(
"mail.smtp.port"
,
"465"
);
035.
props.put(
"mail.smtp.socketFactory.port"
,
"465"
);
036.
props.put(
"mail.smtp.socketFactory.class"
,
037.
"javax.net.ssl.SSLSocketFactory"
);
038.
props.put(
"mail.smtp.socketFactory.fallback"
,
"false"
);
039.
props.setProperty(
"mail.smtp.quitwait"
,
"false"
);
040.
041.
session = Session.getDefaultInstance(props,
this
);
042.
}
043.
044.
protected
PasswordAuthentication getPasswordAuthentication() {
045.
return
new
PasswordAuthentication(user, password);
046.
}
047.
048.
public
synchronized
void
sendMail(String subject, String body, String sender, String recipients)
throws
Exception {
049.
try
{
050.
MimeMessage message =
new
MimeMessage(session);
051.
DataHandler handler =
new
DataHandler(
new
ByteArrayDataSource(body.getBytes(),
"text/plain"
));
052.
message.setSender(
new
InternetAddress(sender));
053.
message.setSubject(subject);
054.
message.setDataHandler(handler);
055.
if
(recipients.indexOf(
','
) >
0
)
056.
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
057.
else
058.
message.setRecipient(Message.RecipientType.TO,
new
InternetAddress(recipients));
059.
Transport.send(message);
060.
}
catch
(Exception e){
061.
062.
}
063.
}
064.
065.
public
class
ByteArrayDataSource
implements
DataSource {
066.
private
byte
[] data;
067.
private
String type;
068.
069.
public
ByteArrayDataSource(
byte
[] data, String type) {
070.
super
();
071.
this
.data = data;
072.
this
.type = type;
073.
}
074.
075.
public
ByteArrayDataSource(
byte
[] data) {
076.
super
();
077.
this
.data = data;
078.
}
079.
080.
public
void
setType(String type) {
081.
this
.type = type;
082.
}
083.
084.
public
String getContentType() {
085.
if
(type ==
null
)
086.
return
"application/octet-stream"
;
087.
else
088.
return
type;
089.
}
090.
091.
public
InputStream getInputStream()
throws
IOException {
092.
return
new
ByteArrayInputStream(data);
093.
}
094.
095.
public
String getName() {
096.
return
"ByteArrayDataSource"
;
097.
}
098.
099.
public
OutputStream getOutputStream()
throws
IOException {
100.
throw
new
IOException(
"Not Supported"
);
101.
}
102.
}
103.
}