javamail

来源:互联网 发布:同济大学网络教育招生 编辑:程序博客网 时间:2024/06/07 02:04
import java.util.Date;
import java.util.Properties;


import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


import org.apache.log4j.Logger;


import com.sun.mail.smtp.SMTPMessage;


public class SendMail {


private static final Logger log = Logger.getLogger(SendMail.class);


private static final String HOST_NAME_PROPERTY = "mail.smtp.host";


private static final String HOST_NAME = "smtp.gmail.com";
private static final String username = "";
private static final String password = "";


private static final String PROTOCOL = "smtp";


private String from = "";
private String to = "";
private String cc = "";
private String bcc = "";
private String subject = "";
private String message = "";
private MimeMultipart multipart;


public static Properties props = null;


static {
props = System.getProperties();
props.put(HOST_NAME_PROPERTY, HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.user", username);
props.put("mail.smtp.password", password);
props.setProperty("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
}


public SendMail() {
}


public boolean sendMail(String from, String to, String cc, String bcc,
String subject, String message) {


this.from = from;
this.to = to;
this.cc = cc;
this.bcc = bcc;
this.subject = subject;
this.message = message;


return sendMail();


}


public boolean sendHTMLMail(MimeMultipart multipart, String subject,
String from, String to) {
this.from = from;
this.to = to;
this.subject = subject;
this.multipart = multipart;


return sendHTMLMail();
}


public boolean send(String body, String subject, String from, String to,
String accountID, String accountPassword) {
this.message = body;
this.subject = subject;
this.from = from;
this.to = to;


return sendMail(accountID, accountPassword);
}


public boolean send(String body, String subject, String from, String to) {
this.message = body;
this.subject = subject;
this.from = from;
this.to = to;


return sendMail();
}

public boolean send(String body, String subject, String to) {
this.message = body;
this.subject = subject;
this.from = username;
this.to = to;


return sendMail();
}



public boolean sendAttachments(String body, String subject, String from,
String to, String[] filePaths) {
this.message = body;
this.subject = subject;
this.from = from;
this.to = to;


return sendAttach(filePaths);
}


private boolean sendMail(String accountID, String accountPassword) {
Session session = Session.getDefaultInstance(props, null);


URLName url = new URLName(PROTOCOL, HOST_NAME, -1, null, accountID,
accountPassword);
session.setPasswordAuthentication(url, new PasswordAuthentication(
accountID, accountPassword));


Message msg = new SMTPMessage(session);


try {


msg.setFrom(new InternetAddress(from));


msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
to, false));
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(
cc, false));
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(
bcc, false));


msg.setSubject(subject);
msg.setText(message);
msg.setSentDate(new Date());


// Transport.send(msg);
Transport smtpTransport = session.getTransport("smtp");
smtpTransport.connect(HOST_NAME, username, password);
smtpTransport.sendMessage(msg, msg.getAllRecipients());
smtpTransport.close();


return true;


} catch (MessagingException e) {
log.error(e);
return false;
}
}


private boolean sendMail() {
Session session = Session.getDefaultInstance(props, null);


URLName url = new URLName(PROTOCOL, HOST_NAME, -1, null, username,
password);
session.setPasswordAuthentication(url, new PasswordAuthentication(
username, password));


Message msg = new SMTPMessage(session);


try {


msg.setFrom(new InternetAddress(from));


msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
to, false));
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(
cc, false));
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(
bcc, false));


msg.setSubject(subject);
msg.setText(message);
msg.setSentDate(new Date());


// Transport.send(msg);
Transport smtpTransport = session.getTransport("smtp");
smtpTransport.connect(HOST_NAME, username, password);
smtpTransport.sendMessage(msg, msg.getAllRecipients());
smtpTransport.close();


return true;


} catch (MessagingException e) {
e.printStackTrace();
log.error(e);
return false;
}


}


private boolean sendHTMLMail() {
Session session = Session.getDefaultInstance(props, null);


URLName url = new URLName(PROTOCOL, HOST_NAME, -1, null, username,
password);
session.setPasswordAuthentication(url, new PasswordAuthentication(
username, password));


Message msg = new MimeMessage(session);


try {


msg.setFrom(new InternetAddress(from));


msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
to, false));
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(
cc, false));
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(
bcc, false));


msg.setSubject(subject);
msg.setContent(multipart);
msg.setSentDate(new Date());


// Transport.send(msg);
Transport smtpTransport = session.getTransport("smtp");
smtpTransport.connect(HOST_NAME, username, password);
smtpTransport.sendMessage(msg, msg.getAllRecipients());
smtpTransport.close();


return true;


} catch (MessagingException e) {
log.error(e);
return false;
}


}


private boolean sendAttach(String[] filePaths) {


// --- Password changed here and on the server. - AS:20041203.
String sUsername = username;
String sPassword = password;


Session session = Session.getDefaultInstance(props, null);


URLName url = new URLName(PROTOCOL, HOST_NAME, -1, null, sUsername,
sPassword);
session.setPasswordAuthentication(url, new PasswordAuthentication(
sUsername, sPassword));


// System.out.println( "User: " + url.getUsername() + ", Pass: " +
// url.getPassword() );


try {
// Define message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to));


// String sTemp = msg.getRecipients( Message.RecipientType.TO
// )[0].toString();
// System.out.println( "Recip: " + sTemp );


msg.setRecipients(Message.RecipientType.CC, InternetAddress
.parse(cc)); // --- AS:20041126.
msg.setSubject(subject);


// create and fill the first message part
MimeBodyPart mbpMessage = new MimeBodyPart();
mbpMessage.setText(message);


// create the Multipart and add its parts to it
Multipart mp = new MimeMultipart(); // --- Moved up here -
// AS:20041126.
mp.addBodyPart(mbpMessage); // --- Moved up here - AS:20041126.


// attach files to the message
for (int i = 0; i < filePaths.length; i++) {


// System.out.println( "Processing attachment: " + filePaths[i]
// );


FileDataSource fds = new FileDataSource(filePaths[i]);


// create the second message part
MimeBodyPart mbpAttach = new MimeBodyPart(); // --- move inside
// the loop -
// AS:20041126.


mbpAttach.setDataHandler(new DataHandler(fds));
mbpAttach.setFileName(fds.getName());


mp.addBodyPart(mbpAttach);


// add the Multipart to the message
msg.setContent(mp);
}


// set the Date: header
msg.setSentDate(new Date());


// System.out.println( "About to call Transport..." );


// send the message
// Transport.send(msg);
Transport smtpTransport = session.getTransport("smtp");
smtpTransport.connect(HOST_NAME, username, password);
smtpTransport.sendMessage(msg, msg.getAllRecipients());
smtpTransport.close();


return true;


} catch (MessagingException e) {
e.printStackTrace();
log.error(e);
return false;
}
}


public void setFrom(String from) {
this.from = from;
}


public void setTo(String to) {
this.to = to;
}


public void setCc(String cc) {
this.cc = cc;
}


public void setBcc(String bcc) {
this.bcc = bcc;
}


public void setSubject(String subject) {
this.subject = subject;
}


public void setMessage(String message) {
this.message = message;
}

public static void main(String args[]) {
SendMail sendMail = new SendMail();


sendMail.send("", "", "",
"");


System.out.println("OK");
}


}
原创粉丝点击