java邮件发送
1 需要的jar包
?activation.jar,mail.jar,mailapi.jar
2?SendMailObject.java
package com.mc.common.javasendmail;/*** @author Zhangkun aistill@msn.com* @version 1.0*/import java.util.*; import javax.mail.*; import javax.mail.internet.*;import java.util.Date; import javax.activation.*; import java.io.*;public class SendMailObject {// ���巢���?��?��?������private String to = "";private String from = "";private String cc = "";private String bcc = "";private String bounceback = "";private String subject = "";private String TextContent = "";private String SMTPServer = "";private String filename = "";private String sasluser = "";private String saslpass = "";private boolean isHtmlMail = true;// ���?��淢�?������?���?���Vector file = new Vector();public SendMailObject(String SMTPServer) {this.SMTPServer = SMTPServer;}public SendMailObject(String SMTPServer, String SASLUser, String SASLPass) {// the mail server need to saslauth,initialize tthe sasl parameterthis.SMTPServer = SMTPServer;this.sasluser = SASLUser;this.saslpass = SASLPass;}public void setMailInfo(String from, String to, String cc, String bcc,String bounceback, String subject, boolean isHtmlMail,String TextContent) {// ��?�������?��?��?������this.from = from;this.to = to;this.cc = cc;this.bcc = bcc;this.bounceback = bounceback;this.subject = subject;this.isHtmlMail = isHtmlMail;this.TextContent = TextContent;}// �÷��������?�������public void attachfile(String fname) {file.addElement(fname);}// ��?�����?��?���public boolean startSendMail() {try {// ����properties����Properties props = System.getProperties();// �����?�������props.put("mail.smtp.host", SMTPServer);// �����?������� ����?��?��? ���?������?�?props.put("mail.smtp.from", bounceback);// set the smtp auth option of the mail serverif ("".equalsIgnoreCase(sasluser.trim())) {props.put("mail.smtp.auth", "false");System.out.println("set smtp sasl authentication mail.smtp.auth = false");} else {props.put("mail.smtp.auth", "true");System.out.println("set smtp sasl authentication mail.smtp.auth = true");}// �?�?�???�����Session session = Session.getDefaultInstance(props, null);// ����?����?������?������?�?���?��MimeMessage msg = new MimeMessage(session);// �����?�?�?�������msg.setSentDate(new Date());// ���?�?msg.setFrom(new InternetAddress(from));// ���?�?msg.setRecipients(Message.RecipientType.TO, to);// ���?�?if (!"".equalsIgnoreCase(cc.trim())) { // if the cc is not null ,set// the cc field of the mailmsg.setRecipients(Message.RecipientType.CC, cc);}// �����?�?if (!"".equalsIgnoreCase(bcc.trim())) { // if the cc is not null// ,set the cc field of the// mailmsg.setRecipients(Message.RecipientType.BCC, bcc);}// ����msg.setSubject(subject);// �����bodypart�����?�?�������multipart��Multipart mp = new MimeMultipart();// add attachment , ����?��������?����Enumeration efile = file.elements();// ����������?��и��?���while (efile.hasMoreElements()) {MimeBodyPart mbp = new MimeBodyPart();// ?���??��������filename = efile.nextElement().toString();// �?����?FileDataSource fds = new FileDataSource(filename);// �?��������?����bodypartmbp.setDataHandler(new DataHandler(fds));// �?��?���?������bodypartmbp.setFileName(fds.getName());mp.addBodyPart(mbp);}// ���?����е�����?��file.removeAllElements();// add mail text bodyMimeBodyPart mbpc = new MimeBodyPart();if (isHtmlMail == true) {mbpc.setContent("<meta http-equiv='Content-Type' content='text/html; charset=gbk' />"+ TextContent, "text/html;charset=GB2312");} else {mbpc.setText(TextContent);}mp.addBodyPart(mbpc);// multipart���?�?�msg.setContent(mp);msg.saveChanges();System.out.println(new Date() + " sending a mail by Mail Server:"+ SMTPServer + " .....");// �����?�Transport transport = session.getTransport("smtp");transport.connect((String) props.get("mail.smtp.host"), sasluser,saslpass);if ("".equalsIgnoreCase(sasluser.trim())) { //transport.send(msg);} else {transport.sendMessage(msg, InternetAddress.parse(to));if (!"".equalsIgnoreCase(cc.trim())) { // if the cc is not null// ,sent mailtransport.sendMessage(msg, InternetAddress.parse(cc));}if (!"".equalsIgnoreCase(bcc.trim())) { // if the cc is not null// ,sent mailtransport.sendMessage(msg, InternetAddress.parse(bcc));}}System.out.println("Send mail completed!");} catch (MessagingException mex) {mex.printStackTrace();Exception ex = null;if ((ex = mex.getNextException()) != null) {ex.printStackTrace();}return false;}return true;}}3 EmailTest.java?
總結(jié)
- 上一篇: PBE加密
- 下一篇: 自己写的socket 多线程 通讯