import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
/**
* usage: java SendingMail
* with proxy: java -DsocksProxyHost=socks-server.ibm.com -DsocksProxyPort=1080 SendingMail
*
**/
public class SendingMail {
private static final String SMTP_HOST_NAME = "smtp.qq.com";
private static final String SMTP_AUTH_USER = "8888@qq.com";
private static final String SMTP_AUTH_PWD = "6666";
public void send(String from, String to, String subject, String message) {
try {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session mailConnection = Session.getDefaultInstance(props, auth);
Message msg = new MimeMessage(mailConnection);
Address fromT = new InternetAddress(from, "Mr. Jwu");
Address toT = new InternetAddress(to);
msg.setContent(message, "text/plain");
msg.setFrom(fromT);
msg.setRecipient(Message.RecipientType.TO, toT);
msg.setSubject(subject);
Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String from = SMTP_AUTH_USER;
String to = "5230@gmail.com";
String subject = "a test message.";
String message = "A test message for java mail.";
SendingMail sender = new SendingMail();
sender.send(from, to, subject, message);
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}