-- 作者:wcdxyl
-- 发布时间:3/16/2006 10:12:00 AM
-- javamail发送邮件[原创]
import java.util.Enumeration; import java.util.Properties; import java.util.Vector; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; 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; import java.io.FileReader; import java.io.FileInputStream; import java.io.IOException; import javax.activation.DataHandler; import javax.activation.FileDataSource; /** * Title:SendMail * Description: 完整邮件发送工具 * Copyright: Copyright (c) 2005 * Company: ZZNODE * @author C.Wang * @version 1.0 */ public class MailTest { //邮件体 Multipart mp = new MimeMultipart(); //定义字符串 //邮件服务器地址 String mailserver = ""; //用户名 String user = ""; //密码 String password = ""; //主题 String subject = ""; //发件人地址 String fromAddr = ""; //收件人地址 String toAddr = ""; //邮件正文引入文件路径 String msgfilepath = ""; //抄送人地址 String ccAddr = ""; //密送人地址 String bccAddr = ""; //附件路径 String withfilepath = ""; public MailTest() { } public static void main(String[] args) throws IOException { //发送邮件 new MailTest().sendMail(); } /** * 获取文本文件内容 * @param path String * @throws IOException * @return String */ public String getFile(String path) throws IOException { //读取文件内容 char[] chrBuffer = new char[10];//缓冲十个字符 int intLength; String s = "";//文件内容字符串 FileReader fis = new FileReader(path); while ( (intLength = fis.read(chrBuffer)) != -1) { String temp = String.valueOf(chrBuffer);//转换字符串 s = s + temp;//累加字符串 } return s; } public void sendMail() throws IOException { //读取配置文件 Properties setupfile = new Properties(); FileInputStream fis = new FileInputStream("setup.properites"); setupfile.load(fis); mailserver = setupfile.getProperty("mailserver"); user = setupfile.getProperty("user"); password = setupfile.getProperty("password"); subject = setupfile.getProperty("subject"); fromAddr = setupfile.getProperty("fromAddr"); toAddr = setupfile.getProperty("toAddr"); ccAddr = setupfile.getProperty("ccAddr"); bccAddr = setupfile.getProperty("bccAddr"); withfilepath = setupfile.getProperty("withfilepath"); //信件内容文件路径 msgfilepath = setupfile.getProperty("msgfilepath"); //获取信件内容 String message = getFile(msgfilepath); try { Properties props = new Properties(); props.put("mail.smtp.host", mailserver); //例如:202.108.44.206 smtp.163.com props.put("mail.smtp.auth", "true"); //认证是否设置 //建立会话并且认证 Session session = Session.getDefaultInstance(props, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } } ); Message msg = new MimeMessage(session); //发送源地址 msg.setFrom(new InternetAddress(fromAddr)); //发送目的地址 InternetAddress[] tos = InternetAddress.parse(toAddr); msg.setRecipients(Message.RecipientType.TO, tos); //抄送目的地址 InternetAddress[] toscc = InternetAddress.parse(ccAddr); msg.setRecipients(Message.RecipientType.CC, toscc); //密送目的地址 InternetAddress[] tosbcc = InternetAddress.parse(bccAddr); msg.setRecipients(Message.RecipientType.BCC, tosbcc); //主题 msg.setSubject(subject); //邮件内容 //msg.setText(message);//发送不带附件时用,简单信件。 //把信件内容放入mp内,和附件一样的放发,作为一个整体发送。 MimeBodyPart mbps = new MimeBodyPart(); mbps.setText(message); mp.addBodyPart(mbps); //附件 Vector file = new Vector(); file.addElement(withfilepath); //附件名集合枚举 Enumeration efile = file.elements(); while (efile.hasMoreElements()) { MimeBodyPart mbp = new MimeBodyPart(); //选择出每一个附件名 String filename = efile.nextElement().toString(); //得到数据源 FileDataSource fds = new FileDataSource(filename); //得到附件本身并至入BodyPart mbp.setDataHandler(new DataHandler(fds)); //得到文件名同样至入BodyPart mbp.setFileName(fds.getName()); mp.addBodyPart(mbp); } //移走集合中的所有元素 file.removeAllElements(); //Multipart加入到信件 msg.setContent(mp); //发送 Transport.send(msg); System.out.println("Mail is Sent"); } catch (Exception e) { System.out.println(e); } } }
|