熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java Mail 深入淺出(2)

2022-06-13   來源: Java核心技術 

  /**

  * SendMailService 構造子注解

  */

  public SendMail() {

  super();

  }

  private void fillMail(Session sessionMimeMessage msg) throws IOException MessagingException{

  String fileName = null;

  Multipart mPart = new MimeMultipart();

  if (mailFrom != null) {

  msgsetFrom(new InternetAddress(mailFrom));

  Systemoutprintln(發送人Mail地址+mailFrom);

  } else {

  Systemoutprintln(沒有指定發送人郵件地址!);

  return;

  }

  if (mailTo != null) {

  InternetAddress[] address = InternetAddressparse(mailTo);

  msgsetRecipients(MessageRecipientTypeTO address);

  Systemoutprintln(收件人Mail地址+mailTo);

  } else {

  Systemoutprintln(沒有指定收件人郵件地址!);

  return;

  }

  if (mailccTo != null) {

  InternetAddress[] ccaddress = InternetAddressparse(mailccTo);

  Systemoutprintln(CCMail地址+mailccTo);

  msgsetRecipients(MessageRecipientTypeCC ccaddress);

  }

  if (mailbccTo != null) {

  InternetAddress[] bccaddress = InternetAddressparse(mailbccTo);

  Systemoutprintln(BCCMail地址+mailbccTo);

  msgsetRecipients(MessageRecipientTypeBCC bccaddress);

  }

  msgsetSubject(subject);

  InternetAddress[] replyAddress = { new InternetAddress(mailFrom)};

  msgsetReplyTo(replyAddress);

  // create and fill the first message part

  MimeBodyPart mBodyContent = new MimeBodyPart();

  if (msgContent != null)

  mBodyContentsetContent(msgContent messageContentMimeType);

  else

  mBodyContentsetContent( messageContentMimeType);

  mPartaddBodyPart(mBodyContent);

  // attach the file to the message

  if (attachedFileList != null) {

  for (Enumeration fileList = attachedFileListelements(); fileListhasMoreElements();) {

  fileName = (String) fileListnextElement();

  MimeBodyPart mBodyPart = new MimeBodyPart();

  // attach the file to the message

  FileDataSource fds = new FileDataSource(messageBasePath + fileName);

  Systemoutprintln(Mail發送的附件為+messageBasePath + fileName);

  mBodyPartsetDataHandler(new DataHandler(fds));

  mBodyPartsetFileName(fileName);

  mPartaddBodyPart(mBodyPart);

  }

  }

  msgsetContent(mPart);

  msgsetSentDate(new Date());

  }

  /**

  * 此處插入方法說明

  */

  public void init()

  {

  }

  /**

  * 發送e_mail返回類型為int

  * 當返回值為說明郵件發送成功

  * 當返回值為說明郵件發送失敗

  */

  public int sendMail() throws IOException MessagingException {

  int loopCount;

  Properties props = SystemgetProperties();

  propsput(mailsmtphost smtpHost);

  propsput(mailsmtpauth true);

  MailAuthenticator auth = new MailAuthenticator();

  Session session = SessiongetInstance(props auth);

  sessionsetDebug(debug);

  MimeMessage msg = new MimeMessage(session);

  Transport trans = null;

  try {

  fillMail(sessionmsg);

  // send the message

  trans = sessiongetTransport(smtp);

  try {

  nnect(smtpHost MailAuthenticatorHUAWEI_MAIL_USER MailAuthenticatorHUAWEI_MAIL_PASSWORD);// HUAWEI_MAIL_PASSWORD);

  } catch (AuthenticationFailedException e) {

  eprintStackTrace();

  Systemoutprintln(連接郵件服務器錯誤);

  return ;

  } catch (MessagingException e) {

  Systemoutprintln(連接郵件服務器錯誤);

  return ;

  }

  transsend(msg);

  transclose();

  } catch (MessagingException mex) {

  Systemoutprintln(發送郵件失敗);

  mexprintStackTrace();

  Exception ex = null;

  if ((ex = mexgetNextException()) != null) {

  Systemoutprintln(extoString());

  exprintStackTrace();

  }

  return ;

  } finally {

  try {

  if (trans != null && transisConnected())

  transclose();

  } catch (Exception e) {

  Systemoutprintln(etoString());

  }

  }

  Systemoutprintln(發送郵件成功!);

  return ;

  }

  public void setAttachedFileList(javautilVector 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 = ;i<;i++) {

  SendMail sm = new SendMail();

  smsetSMTPHost(SMTP地址);

  smsetMailFrom(發送地址);

  smsetMailTo(目標地址);

  smsetMsgContent(內容);

  smsetSubject(標題);

  smsendMail();

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26252.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.