import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class mymail {
public static String username = "***";//用户名
public static String password = "***";//用户密码
public static String smtpaddress = "smtp.163.com";//邮件服务起地址
public static String from = "***@163.com";//发邮件的邮箱
public static String to = "****@***";//收邮件的邮箱
public static String title = "标题";//文件标题
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); //设置smtp主机
props.put("mail.smtp.auth", "true");//使用smtp身份验证
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]);//标题
msg.setContent(b[2], "text/plain");//内容
①
Transport transport = session.getTransport("smtp");
System.out.println("连接邮件服务器成功");
transport.send(msg);
}
}
***************************8
第二种写法
/*
* 创建日期 2005-6-8
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - 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";//邮件服务器
public static String from = "***";//发邮件的邮箱
public static String to = "***";//收邮件的邮箱
public static String title = "标题";//文件标题
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); //设置smtp主机
props.put("mail.smtp.auth", "true");//使用smtp身份验证
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]);//标题
msg.setContent(b[2], "text/plain");//内容
①
Transport transport = session.getTransport("smtp");
transport.connect(smtpaddress,username,password);
System.out.println("连接邮件服务器成功");
//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。