⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 postoffice.java

📁 采用JAVA开发
💻 JAVA
字号:
package com.gctech.sms.voice.common;import java.io.*;import java.util.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;import java.io.FileNotFoundException;
import com.gctech.sms.voice.*;public class PostOffice{  public String SMTP;  private String user;  private String pwd;  private MailAuthenticator math = null;  public PostOffice()  {  }  public String getSMTP()  {    return this.SMTP;  }  public void setAuth(String user,String pwd)  {    this.user = user;    this.pwd = pwd;    math = new MailAuthenticator(user,pwd);  }  public void setSMTP(String SMTP)  {    this.SMTP = SMTP;  }  /**   * 发送邮件   * param m :邮件信息载体   * @return 如发送成功,返回true   */  public boolean send(MailMessage m)  {    try    {      // Get system properties      Properties props = System.getProperties();      // Setup mail server      props.put("mail.smtp.host",this.SMTP);      if(math!=null) props.put("mail.smtp.auth","true");      // Get session      Session session = Session.getInstance(props,math);      // Define message      Message message = new MimeMessage(session);      message.setFrom(new InternetAddress(m.getFrom()));      String[] to = m.getTo();      for(int i = 0;i < to.length;i++)      {        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to[i]));      }      //设置抄送人列表,暗送人列表      String[] bcc = m.getBcc();      if(null != bcc)      {        for(int j = 0;j < bcc.length;j++)        {          message.addRecipient(Message.RecipientType.BCC,                               new InternetAddress(bcc[j]));        }      }      String[] cc = m.getCc();      if(null != cc)      {        for(int k = 0;k < cc.length;k++)        {          message.setRecipient(Message.RecipientType.CC,                               new InternetAddress(cc[k]));        }      }      message.setSubject(m.getSubject());      // Create the message part      BodyPart messageBodyPart = new MimeBodyPart();      // Fill the message      messageBodyPart.setText(m.getMessage());      // Create a Multipart      Multipart multipart = new MimeMultipart();      // Add part one      multipart.addBodyPart(messageBodyPart);      // Create second body part      if(!m.isFilesEmpty()) //发送多个附件      {        Iterator files = m.allFiles();        while(files.hasNext())        {          String file = (String) files.next();          messageBodyPart = new MimeBodyPart();          // Get the attachment          DataSource source = new FileDataSource((file));          // Set the data handler to the attachment          messageBodyPart.setDataHandler(new DataHandler(source));          // Set the filename          messageBodyPart.setFileName(file);          // Add part          multipart.addBodyPart(messageBodyPart);        }      } //end if      // Put parts in message      message.setContent(multipart);      // Send the message      Transport.send(message);    } //end try    catch(MessagingException me)    {      System.out.println(me.toString());      return false;    }    return true;  } //end send}class MailAuthenticator    extends Authenticator{  private String username = null;  private String userpasswd = null;  public MailAuthenticator()  {}  public MailAuthenticator(String username,String userpasswd)  {    this.username = username;    this.userpasswd = userpasswd;  }  public void setUserName(String username)  {    this.username = username;  }  public void setPassword(String password)  {    this.userpasswd = password;  }  public PasswordAuthentication getPasswordAuthentication()  {    return new PasswordAuthentication(username,userpasswd);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -