import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;
/*
class PopupAuthenticator extends Authenticator{
private String username, password;
public PopupAuthenticator(String user,String pass){
username = user;
password = pass;
}
public PasswordAuthentication getPasswordAuthentication(){
String result = JOptionPane.showInputDialog("Enter 'username,password'");
StringTokenizer st = new StringTokenizer(result, ",");
username = st.nextToken();
password = st.nextToken();
return new PasswordAuthentication(username, password);
}
}
*/
public class SMTP {
private Properties props = System.getProperties();
private Session session = null;
private Transport transport = null;
//private Message msg = null;
private MimeMessage msg = null;
private MimeMultipart mm = new MimeMultipart();
private MimeBodyPart mbp = new MimeBodyPart();
private String auth = "false";
private final static String mailer = "netkiller";
private String username = null;
private String password = null;
private String host = null;
private String from = null;
private String to = null;
private String cc = "";
private String bcc = "";
private String subject = "";
private String text = "";
private String footer = "";
private boolean debug = false;
public SMTP() {
}
public void createSession() throws Exception {
session = Session.getInstance(props, null);
session.setDebug(debug);
transport = session.getTransport("smtp");
transport.connect(host, username, password);
msg = new MimeMessage(session);
}
public void setDebug(boolean debug) throws Exception {
this.debug = debug;
}
public void setSmtpHost(String str) throws Exception {
this.host = str;
props.put("mail.smtp.host", host);
}
public void setAuth(boolean auth){
if(auth){
props.put("mail.smtp.auth", "true");
}
}
public void setUsername(String user){
this.username = user;
props.put("mail.smtp.user",username);
}
public void setPassword(String pass){
this.password = pass;
props.put("mail.smtp.password",password);
}
public String getUsername(){
return this.username;
}
public String getPassword(){
return this.password;
}
public void setFrom(String str) throws Exception {
this.from = str;
msg.setFrom(new InternetAddress(from));
}
public void setTo(String str) throws Exception{
this.to = str;
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
}
public void setCC(String str) throws Exception{
this.cc = str;
msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse(cc, false));
}
public void setBCC(String str) throws Exception{
this.bcc = str;
msg.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(bcc, false));
}
public void setSubject(String str) throws Exception{
this.subject = str;
msg.setSubject(subject);
}
public void setText(String str) throws Exception{
this.text = str;
this.text += this.footer;
// msg.setText(this.text);
BodyPart bp = new MimeBodyPart();
bp.setContent(text,"text/plain; charset=gb2312");
mm.addBodyPart(bp);
msg.setContent(mm);
}
public void setFooter(String str){
this.footer = str;
}
public void setHtml(String str) throws Exception{
StringBuffer sb = new StringBuffer();
sb.append("<HTML>\n");
sb.append("<HEAD>\n");
sb.append("<TITLE>\n");
sb.append(subject + "\n");
sb.append("</TITLE>\n");
sb.append("</HEAD>\n");
sb.append("<BODY>\n");
sb.append("<H1>" + subject + "</H1>" + "\n");
sb.append(str);
sb.append(this.footer);
sb.append("\n");
sb.append("</BODY>\n");
sb.append("</HTML>\n");
this.text = sb.toString();
BodyPart bp = new MimeBodyPart();
bp.setContent(text,"text/html; charset=gb2312");
mm.addBodyPart(bp);
msg.setContent(mm);
}
public boolean addFileAffix(String filename) {
boolean bool = false;
try{
BodyPart bp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fds));
//bp.setFileName(fds.getName());
bp.setFileName(MimeUtility.encodeWord(fds.getName(), "GB2312",null));
bp.setHeader("Content-ID",fds.getName());
mm.addBodyPart(bp);
//msg.setContent(mm);
bool = true;
}
catch(Exception e){
System.err.println("增加邮件附件:"+filename+"发生错误!"+e);
}
System.out.println("增加邮件附件:"+filename);
return bool;
}
public void send(){
try{
msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new Date());
transport.sendMessage(msg, msg.getAllRecipients());
System.out.println("Mail was recorded successfully.");
} catch (MessagingException e) {
//e.printStackTrace();
System.out.println(e.toString());
}
}
public static void main(String[] args) {
try{
SMTP am = new SMTP();
am.setDebug(true);
am.setSmtpHost("mail.9812.net");
am.setAuth(true);
am.setUsername("netkiller@9812.net");
am.setPassword("*******");
am.createSession();
am.setFrom("netkiller@9812.net");
am.setTo("netkiller@9812.net");
am.setCC("xnetkiller@netscape.net");
am.setBCC("netkillers@sohu.com");
am.setSubject("hi netkiller");
am.setText("<a href=xxx><img src='cid:product_price_chart.png'>ddddddddddddddddd</a>");
am.setHtml("陈景峰<br>"+"<a href=xxx><img src='cid:product_price_chart.png'></a>");
//HTML <img src='file.jpg'> 在邮件内容里要改为 cid:file.jpg
am.setFooter("\n<br>====================\n<br>netkiller");
am.addFileAffix("c:/product_price_chart.png");
am.addFileAffix("c:/gomine.properties");
am.send();
}
catch(Exception e){
System.err.println(e.toString());
//e.printStackTrace();
}
}
}