- import javax.mail.*;
-
-
-
-
-
- public class MailAuthenticator extends Authenticator
-
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static String HUAWEI_MAIL_USER = "lisunchang678";
- public static String HUAWEI_MAIL_PASSWORD = "lsc6312217";
-
-
-
-
-
- public MailAuthenticator()
-
- {
-
- }
-
-
-
- protected PasswordAuthentication getPasswordAuthentication()
-
- {
-
- return new PasswordAuthentication(HUAWEI_MAIL_USER, HUAWEI_MAIL_PASSWORD);
-
- }
-
-
-
- }
-
- package net.csdn.blog.johnston.mail;
-
-
-
-
-
-
-
-
-
-
-
- import java.util.*;
-
- import java.io.*;
-
- import javax.mail.*;
-
- import javax.mail.internet.*;
-
- import javax.activation.*;
-
- public class SendMail {
-
-
-
- private String mailTo = null;
-
-
-
- private String mailFrom = null;
-
-
-
- private String smtpHost = null;
-
-
-
- private boolean debug = false;
-
- private String messageBasePath = null;
-
-
-
- private String subject;
-
-
-
- private String msgContent;
-
- private Vector attachedFileList;
-
- private String mailAccount = null;
-
- private String mailPass = null;
-
- private String messageContentMimeType = "text/html; charset=gb2312";
-
- private String mailbccTo = null;
-
- private String mailccTo = null;
-
-
-
-
-
-
-
- public SendMail() {
-
- super();
-
- }
-
- private void fillMail(Session session, MimeMessage msg) throws IOException,
- MessagingException {
-
- String fileName = null;
-
- Multipart mPart = new MimeMultipart();
-
- if (mailFrom != null) {
-
- msg.setFrom(new InternetAddress(mailFrom));
-
- System.out.println("發(fā)送人Mail地址:" + mailFrom);
-
- } else {
-
- System.out.println("沒有指定發(fā)送人郵件地址!");
-
- return;
-
- }
-
- if (mailTo != null) {
-
- InternetAddress[] address = InternetAddress.parse(mailTo);
-
- msg.setRecipients(Message.RecipientType.TO, address);
-
- System.out.println("收件人Mail地址:" + mailTo);
-
- } else {
-
- System.out.println("沒有指定收件人郵件地址,!");
-
- return;
-
- }
-
- if (mailccTo != null) {
-
- InternetAddress[] ccaddress = InternetAddress.parse(mailccTo);
-
- System.out.println("CCMail地址:" + mailccTo);
-
- msg.setRecipients(Message.RecipientType.CC, ccaddress);
-
- }
-
- if (mailbccTo != null) {
-
- InternetAddress[] bccaddress = InternetAddress.parse(mailbccTo);
-
- System.out.println("BCCMail地址:" + mailbccTo);
-
- msg.setRecipients(Message.RecipientType.BCC, bccaddress);
-
- }
-
- msg.setSubject(subject);
-
- InternetAddress[] replyAddress = { new InternetAddress(mailFrom) };
-
- msg.setReplyTo(replyAddress);
-
-
-
- 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);
-
- System.out.println("Mail發(fā)送的附件為:" + messageBasePath + fileName);
-
- mBodyPart.setDataHandler(new DataHandler(fds));
-
- mBodyPart.setFileName(fileName);
-
- mPart.addBodyPart(mBodyPart);
-
- }
-
- }
-
- msg.setContent(mPart);
-
- msg.setSentDate(new Date());
-
- }
-
-
-
-
-
-
-
- public void init()
-
- {
-
- }
-
-
-
-
-
-
-
-
-
-
-
- public int sendMail() throws IOException, MessagingException {
-
- int loopCount;
-
- Properties props = System.getProperties();
-
- props.put("mail.smtp.host", smtpHost);
-
- props.put("mail.smtp.auth", "true");
-
- MailAuthenticator auth = new MailAuthenticator();
-
- Session session = Session.getInstance(props, auth);
-
- session.setDebug(debug);
-
- MimeMessage msg = new MimeMessage(session);
-
- Transport trans = null;
-
- try {
-
- fillMail(session, msg);
-
-
-
- trans = session.getTransport("smtp");
-
- try {
-
- trans.connect(smtpHost, MailAuthenticator.HUAWEI_MAIL_USER,
- MailAuthenticator.HUAWEI_MAIL_PASSWORD);
-
-
- } catch (AuthenticationFailedException e) {
-
- e.printStackTrace();
-
- System.out.println("連接郵件服務(wù)器錯(cuò)誤1:");
-
- return 3;
-
- } catch (MessagingException e) {
- e.printStackTrace();
- System.out.println("連接郵件服務(wù)器錯(cuò)誤2:");
-
- return 3;
-
- }
-
- trans.send(msg);
-
- trans.close();
-
- } catch (MessagingException mex) {
-
- System.out.println("發(fā)送郵件失敗:");
-
- mex.printStackTrace();
-
- Exception ex = null;
-
- if ((ex = mex.getNextException()) != null) {
-
- System.out.println(ex.toString());
-
- ex.printStackTrace();
-
- }
-
- return 3;
-
- } finally {
-
- try {
-
- if (trans != null && trans.isConnected())
-
- trans.close();
-
- } catch (Exception e) {
-
- System.out.println(e.toString());
-
- }
-
- }
-
- System.out.println("發(fā)送郵件成功,!");
-
- return 0;
-
- }
-
- public void setAttachedFileList(java.util.Vector filelist)
-
- {
-
- attachedFileList = filelist;
-
- }
-
- public void setDebug(boolean debugFlag)
-
- {
-
- debug = debugFlag;
-
- }
-
- public void setMailAccount(String strAccount) {
-
- mailAccount = 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) {
-
- mailPass = 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 static void main(String[] argv) throws Exception
-
- {
-
- for (int i = 0; i < 10; i++) {
-
- SendMail sm = new SendMail();
-
-
-
-
-
-
- sm.setSMTPHost("smtp.163.com");
- sm.setMailFrom("[email protected]");
- sm.setMailTo("[email protected]");
-
-
-
-
-
- sm.setMsgContent("郵件測試程序" + i);
-
- sm.setSubject("郵件測試程序" + i);
-
- sm.sendMail();
-
- Thread t = new Thread();
- t.sleep(10000);
- System.out.println("count:" + i);
-
- }
-
- }
-
- }
|