javax mail 收发机制

 

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
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;
/**
 * @author Administrator
 * @date Dec 31, 2011
 */

public class SendMail {
 public SendMail() {
 }

 public static void main(String[] args) {
  try {
   Authenticator auth = new PopupAuthenticator();
   Properties mailProps = new Properties();
   mailProps.put("mail.smtp.host", "smtp.yeah.net");
   mailProps.put("mail.smtp.auth", "true");
   mailProps.put("username", "ruanqiangbeyond");
   mailProps.put("password", "1050110085.");
   Session mailSession = Session.getInstance(mailProps, auth);
   MimeMessage message = new MimeMessage(mailSession);
   message.setFrom(new InternetAddress("ruanqiangbeyond@yeah.net"));
   //发送
   message.setRecipient(Message.RecipientType.TO, new InternetAddress(
     "ruanqiangbeyond@yeah.net"));
   //抄送
   message.setRecipient(Message.RecipientType.CC, new InternetAddress(
     "490667462@qq.com"));
   //密送
   message.setRecipient(Message.RecipientType.BCC, new InternetAddress(
   "490667462@qq.com"));
   message.setSubject("Mail Test");
   MimeMultipart multi = new MimeMultipart();
   BodyPart textBodyPart = new MimeBodyPart();
   textBodyPart.setText("Hello World!");
   multi.addBodyPart(textBodyPart);
   message.setContent(multi);
//   message.saveChanges();
   Transport transport = mailSession.getTransport("smtp");
   transport.connect("smtp.yeah.net", "ruanqiangbeyond@yeah.net", "1050110085.");

   Transport.send(message);
   transport.close();
  } catch (Exception ex) {
   System.err.println("邮件发送失败的原因是:" + ex.getMessage());
   System.err.println("具体错误原因:");
   ex.printStackTrace(System.err);
  }
 }
}

class PopupAuthenticator extends Authenticator {
 public PasswordAuthentication getPasswordAuthentication() {
  String username = "ruanqiangbeyond"; // 163邮箱登录帐号
  String pwd = "1050110085."; // 登录密码
  return new PasswordAuthentication(username, pwd);
 }
}

 

 

import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.AuthenticationFailedException;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
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;


/**
 * @author Administrator
 * @date Jan 3, 2012
 */

public class Mail {

 // 发送人地址
 private String mailFrom = null;
 // 收件人地址(如多人发送则用逗号隔开)
 private String mailTo = null;
 // 抄送人地址、密送人地址
 private String mailccTo = null;
 private String mailbccTo = null;

 // SMTP主机地址
 private String smtpHost = null;
 // 是否采用调试方式
 private boolean debug = false;
 // 登陆SMTP服务器的用户名和密码
 private String smtpUser = "ruanqiangbeyond@yeah.net";
 private String smtpPassword = "1050110085.";

 private String messageBasePath = null;
 // Mail主题
 private String subject;
 // Mail内容
 private String msgContent;

 private Vector attachedFileList;
 private String messageContentMimeType = "text/html; charset=gb2312";


 public Mail() {
 }

 private void fillMail(MimeMessage msg) throws Exception {

  String fileName = null;

  if (mailFrom != null) {
   msg.setFrom(new InternetAddress(mailFrom));
  } else {
   throw new Exception("Addresser was not maintained!");
  }

  if (mailTo != null) {
   InternetAddress[] address = InternetAddress.parse(mailTo);
   msg.setRecipients(Message.RecipientType.TO, address);
  } else {
   throw new Exception("Addresser was not maintained!");
  }

  if (mailccTo != null) {
   InternetAddress[] ccaddress = InternetAddress.parse(mailccTo);
   msg.setRecipients(Message.RecipientType.CC, ccaddress);
  }
  if (mailbccTo != null) {
   InternetAddress[] bccaddress = InternetAddress.parse(mailbccTo);
   msg.setRecipients(Message.RecipientType.BCC, bccaddress);
  }

  // 加入主题
  msg.setSubject(subject);

  // 加入内容
  Multipart mPart = new MimeMultipart();

  MimeBodyPart mBodyContent = new MimeBodyPart();
  if (msgContent != null)
   mBodyContent.setContent(msgContent, messageContentMimeType);
  else
   mBodyContent.setContent("", messageContentMimeType);
  mPart.addBodyPart(mBodyContent);

  // 加入附件
  if (attachedFileList != null) {
   for (Enumeration fileList = attachedFileList.elements(); fileList
     .hasMoreElements();) {
    fileName = (String) fileList.nextElement();
    MimeBodyPart mBodyPart = new MimeBodyPart();

    FileDataSource fds = new FileDataSource(messageBasePath
      + fileName);
    mBodyPart.setDataHandler(new DataHandler(fds));
    mBodyPart.setFileName(fileName);
    mPart.addBodyPart(mBodyPart);
   }
  }

  msg.setContent(mPart);
 }

 /**
  * 发送邮件
  *
  * @return true表成功,false为失败
  * @throws Exception
  */
 public boolean sendMail() {
  Properties props = System.getProperties();
  props.put("mail.smtp.host", smtpHost);
  props.put("mail.smtp.auth", "true");

  MailAuthenticator auth = new MailAuthenticator();
  MailAuthenticator.MAIL_USER = smtpUser;
  MailAuthenticator.MAIL_PASSWORD = smtpPassword;
  Session session = Session.getInstance(props, auth);
  session.setDebug(debug);

  MimeMessage msg = new MimeMessage(session);
  Transport trans = null;
  try {

   fillMail(msg);
   trans = session.getTransport("smtp");
   try {
    trans.connect(smtpHost, smtpUser, smtpPassword);
   } catch (AuthenticationFailedException e) {
    throw e;
   } catch (MessagingException e) {
    throw e;
   }

   Transport.send(msg);
   return true;

  } catch (MessagingException mex) {
   Exception ex = null;
   if ((ex = mex.getNextException()) != null) {
   }
   return false;
  } catch (Exception ex) {
   return false;
  } finally {
   try {
    if (trans != null && trans.isConnected())
     trans.close();
   } catch (Exception e) {    
   }
                                                                                                                                                                                                                         }
 }

 public void setAttachedFileList(java.util.Vector filelist) {
  attachedFileList = filelist;
 }

 public void setDebug(boolean debugFlag) {
  debug = debugFlag;
 }

 public void setMailAccount(String strAccount) {
 }

 public void setMailbccTo(String bccto) {
  mailbccTo = bccto;
 }

 public void setMailccTo(String ccto) {
  mailccTo = ccto;
 }

 public void setMailFrom(String from) {
  mailFrom = from;
 }

 public void setMailPass(String strMailPass) {
 }

 public void setMailTo(String to) {
  mailTo = to;
 }

 public void setMessageBasePath(String basePath) {
  messageBasePath = basePath;
 }

 public void setMessageContentMimeType(String mimeType) {
  messageContentMimeType = mimeType;
 }

 public void setMsgContent(String content) {
  msgContent = content;
 }

 public void setSMTPHost(String host) {
  smtpHost = host;
 }

 public void setSubject(String sub) {
  subject = sub;
 }

 public void setSmtpPassword(String smtpPassword) {
  this.smtpPassword = smtpPassword;
 }

 public void setSmtpUser(String smtpUser) {
  this.smtpUser = smtpUser;
 }
 
 public static void main(String[] argv) throws Exception {
  Mail sm = new Mail();
  sm.setSMTPHost("smtp.yeah.net");
  sm.setMailFrom("ruanqiangbeyond@yeah.net");
  sm.setMailTo("775746615@qq.com,490667462@qq.com");
  sm.setMailccTo("490667462@qq.com");
  sm.setMsgContent("自动邮件内容");
  sm.setSubject("标题");
  sm.sendMail();
 }
 

}

class MailAuthenticator extends Authenticator {
 // ******************************
 // 由于发送邮件的地方比较多,
 // 下面统一定义用户名,口令.
 // ******************************
 public static String MAIL_USER;
 public static String MAIL_PASSWORD;

 public MailAuthenticator() {
 }

 protected PasswordAuthentication getPasswordAuthentication() {
  return new PasswordAuthentication(MAIL_USER, MAIL_PASSWORD);
 }

}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值