import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
/**
* @author Administrator
*
* TODO 要更改此生成的類型注釋的模板,請(qǐng)轉(zhuǎn)至
* 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板
*/
public class mymail {
public static String username = "***";//用戶名
public static String password = "***";//用戶密碼
public static String smtpaddress = "smtp.163.com";//郵件服務(wù)起地址
public static String from = "***@163.com";//發(fā)郵件的郵箱
public static String to = "****@***";//收郵件的郵箱
public static String title = "標(biāo)題";//文件標(biāo)題
public static String content = "neirong";//文件neirong
public static void main(String[] args) throws Exception {
String[] b = { mymail.to, mymail.title, mymail.content };
Properties props = new Properties();
props.put("mail.smtp.host", mymail.smtpaddress); //設(shè)置smtp主機(jī)
props.put("mail.smtp.auth", "true");//使用smtp身份驗(yàn)證
Session session = Session.getDefaultInstance(props,
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mymail.username,
mymail.password);
}
});
session.setDebug(true);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(
mymail.from);
msg.setFrom(addressFrom);
Address addressTo = new InternetAddress(b[0]);
msg.setRecipient(Message.RecipientType.TO, addressTo);
msg.setSubject(b[1]);//標(biāo)題
msg.setContent(b[2], "text/plain");//內(nèi)容
①
Transport transport = session.getTransport("smtp");
System.out.println("連接郵件服務(wù)器成功");
transport.send(msg);
}
}
***************************8
第二種寫法
/*
* 創(chuàng)建日期 2005-6-8
*
* TODO 要更改此生成的文件的模板,請(qǐng)轉(zhuǎn)至
* 窗口 - 首選項(xiàng) - Java - 代碼樣式 - 代碼模板
*/
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class sendmail {
public static String username = "***";//用戶名
public static String password = "***";//用戶密碼
public static String smtpaddress = "smtp.163.com";//郵件服務(wù)器
public static String from = "***";//發(fā)郵件的郵箱
public static String to = "***";//收郵件的郵箱
public static String title = "標(biāo)題";//文件標(biāo)題
public static String content = "neirong";//文件neirong
public static void main(String[] args) throws Exception {
String[] b = { sendmail.to, sendmail.title, sendmail.content };
Properties props = new Properties();
props.put("mail.smtp.host", sendmail.smtpaddress); //設(shè)置smtp主機(jī)
props.put("mail.smtp.auth", "true");//使用smtp身份驗(yàn)證
Session session = Session.getDefaultInstance(props,
null);
session.setDebug(true);
Message msg = new MimeMessage(session);
InternetAddress addressFrom = new InternetAddress(
sendmail.from);
msg.setFrom(addressFrom);
Address addressTo = new InternetAddress(b[0]);
msg.setRecipient(Message.RecipientType.TO, addressTo);
msg.setSubject(b[1]);//標(biāo)題
msg.setContent(b[2], "text/plain");//內(nèi)容
①
Transport transport = session.getTransport("smtp");
transport.connect(smtpaddress,username,password);
System.out.println("連接郵件服務(wù)器成功");
//transport.send(msg);
transport.sendMessage(msg,msg.getAllRecipients());
}
}
*****************************
如果要添加附件 參考下面程序片段 把下面的程序片段添加到①
String attachment = "c:\1.txt";//附件
if (!attachment.equals("")) {
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(new String(content.getBytes("ISO8859_1"),
"GBK")); //
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart); //
messageBodyPart = new MimeBodyPart();//
DataSource source = new FileDataSource(attachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachment);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
}
最后要記得在tomcat的lib目錄下添加mail.jar,j2ee.jar,activation.jar。