91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java Web stmp發送帶附件郵件(附SSL版)

發布時間:2020-10-05 21:49:20 來源:腳本之家 閱讀:129 作者:luck-cheng 欄目:編程語言

本文實例為大家分享了Java Web stmp發送帶附件郵件的具體代碼,供大家參考,具體內容如下

public class MailFileSendUtils {

 private Properties props; //系統屬性
 private Session session; //郵件會話對象
 private MimeMessage mimeMsg; //MIME郵件對象
 private Multipart mp; //Multipart對象,郵件內容,標題,附件等內容均添加到其中后再生成MimeMessage對象

 /**
  * Constructor
  * @param
  */
 public MailFileSendUtils(){
  props = System.getProperties();
  props.put("mail.smtp.auth","false");
  session = Session.getDefaultInstance(props, null);
  session.setDebug(true);
  mimeMsg = new MimeMessage(session);
  mp = new MimeMultipart();
 }

 /**
  * Constructor
  * @param smtp 郵件發送服務器
  */
 public MailFileSendUtils(String smtp, String username, String password){
  props = System.getProperties();
  props.put("mail.smtp.auth","true");
  props.put("mail.smtp.host", smtp);
  props.put("username", username);
  props.put("password", password);
  session = Session.getDefaultInstance(props, null);
  session.setDebug(true);
  mimeMsg = new MimeMessage(session);
  mp = new MimeMultipart();
 }

 /**
  * 發送郵件
  */
 public boolean sendMail(String from, String[] to, String subject, String content, String filename) {
  try {
   //設置發信人
   mimeMsg.setFrom(new InternetAddress(from));
   //設置接收人
   for (int i = 0; i < to.length; i++) {
    mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to[i]));
   }
   //設置抄送人
//   for (int i = 0; i < copyto.length; i++) {
//    mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(copyto[i]));
//   }
   //設置主題
   mimeMsg.setSubject(subject);
   //設置正文
   BodyPart bp = new MimeBodyPart();
   bp.setContent(content, "text/html;charset=utf-8");
   mp.addBodyPart(bp);
   //設置附件
   bp = new MimeBodyPart();
   FileDataSource fileds = new FileDataSource(filename);
   bp.setDataHandler(new DataHandler(fileds));
   bp.setFileName(MimeUtility.encodeText(fileds.getName(),"UTF-8","B"));
   mp.addBodyPart(bp);
   mimeMsg.setContent(mp);
   mimeMsg.saveChanges();
   //發送郵件
   if(props.get("mail.smtp.auth").equals("true")){
    Transport transport = session.getTransport("smtp");
    transport.connect((String)props.get("mail.smtp.host"), (String)props.get("username"), (String)props.get("password"));
    transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
//    transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC));
    transport.close();
   }else{
    Transport.send(mimeMsg);
   }
   System.out.println("郵件發送成功");
  } catch (MessagingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return true;
 }

// public void toSendMail(SendMailParam sendMailParam){
//  MailFileSendUtils email = new MailFileSendUtils(sendMailParam.getSmtp(), sendMailParam.getUsername(), sendMailParam.getPassword());
//  email.sendMail(sendMailParam.getFrom(), sendMailParam.getTo(), sendMailParam.getSubject(), sendMailParam.getContent(), sendMailParam.getFilepath());
// }


 public static void main(String[] args) {
  String smtp = "smtp.exmail.qq.com";
  String username = "發送的郵箱賬號";
  String password = "發送的郵箱密碼";
  String from = "發送的郵箱";
  String[] to = {"接收郵件的郵箱"};
//  String[] copyto = {"抄送的郵箱"};
  String subject = "主題6";
  String content = "郵件內容6";
  String filename = "附件的文件";
  MailFileSendUtils email = new MailFileSendUtils(smtp, username, password);
//  email.sendMail(from, to, copyto, subject, content, filename);
  email.sendMail(from, to, subject, content, filename);
 }

}

(附:SSL版)

public class MailFileSendUtils {

 private Properties props; //系統屬性
 private Session session; //郵件會話對象
 private MimeMessage mimeMsg; //MIME郵件對象
 private Multipart mp; //Multipart對象,郵件內容,標題,附件等內容均添加到其中后再生成MimeMessage對象

 /**
  * Constructor
  * @param
  */
 public MailFileSendUtils(){
  props = System.getProperties();
  props.put("mail.smtp.auth","false");
  session = Session.getDefaultInstance(props, null);
  session.setDebug(true);
  mimeMsg = new MimeMessage(session);
  mp = new MimeMultipart();
 }

 /**
  * Constructor
  * @param smtp 郵件發送服務器
  */
 public MailFileSendUtils(String smtp,
        String username,
        String password){
  Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  props = System.getProperties();
  MailSSLSocketFactory sf = null;
  try {
   sf = new MailSSLSocketFactory();
  } catch (GeneralSecurityException e) {
  }
  sf.setTrustAllHosts(true);
  props.put("mail.smtp.auth","true");
  props.put("mail.smtp.host", smtp);
  props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
  props.put("mail.smtp.socketFactory.fallback", "false");
  props.put("mail.smtp.ssl.enable", "true");
  props.put("mail.smtp.port", "465");
  props.put("mail.smtp.ssl.socketFactory", sf);

//  props.put("username", username);
//  props.put("password", password);
  session = Session.getInstance(props, new Authenticator() {
   @Override
   protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(username, password);
   }
  });
  session.setDebug(true);
  mimeMsg = new MimeMessage(session);
  mp = new MimeMultipart();
 }

 /**
  * 發送郵件
  */
 public boolean sendMail(String from,
       String[] to,
       String subject,
       String content,
       String filename) {
  try {
   //設置發信人
   mimeMsg.setFrom(new InternetAddress(from));
   //設置接收人
   for (int i = 0; i < to.length; i++) {
    mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to[i]));
   }
   //設置抄送人
//   for (int i = 0; i < copyto.length; i++) {
//    mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(copyto[i]));
//   }
   //設置主題
   mimeMsg.setSubject(subject);
   //設置正文
   BodyPart bp = new MimeBodyPart();
   bp.setContent(content, "text/html;charset=utf-8");
   mp.addBodyPart(bp);
   //設置附件
   bp = new MimeBodyPart();
   FileDataSource fileds = new FileDataSource(filename);
   bp.setDataHandler(new DataHandler(fileds));
   bp.setFileName(MimeUtility.encodeText(fileds.getName(),"UTF-8","B"));
   mp.addBodyPart(bp);
   mimeMsg.setContent(mp);
   mimeMsg.saveChanges();
   //發送郵件
   if(props.get("mail.smtp.auth").equals("true")){
    Transport transport = session.getTransport("smtp");
    transport.connect((String)props.get("mail.smtp.host"), (String)props.get("username"), (String)props.get("password"));
    transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
//    transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.CC));
    transport.close();
   }else{
    Transport.send(mimeMsg);
   }
   System.out.println("郵件發送成功");
  } catch (MessagingException e) {
   e.printStackTrace();
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return true;
 }

 public boolean toSendMail(SendMailParam sendMailParam){
  MailFileSendUtils email = new MailFileSendUtils(
    sendMailParam.getSmtp(),
    sendMailParam.getUsername(),
    sendMailParam.getPassword());
  email.sendMail(
    sendMailParam.getFrom(),
    sendMailParam.getTo(),
    sendMailParam.getSubject(),
    sendMailParam.getContent(),
    sendMailParam.getFilepath());
  return true;
 }


// public static void main(String[] args) {
//  String smtp = "smtp.mxhichina.com";
//  String username = "郵箱";
//  String password = "郵箱密碼";
//  String from = "誰去發";
//  String[] to = {"發給誰"};
////  String[] copyto = {"抄送的郵箱"};
//  String subject = "huawei";
//  String content = "郵件內容6666";
//  String filename = "gdt-3583118353-ad-20170823.xls";
//  MailFileSendUtils email = new MailFileSendUtils(smtp, username, password);
////  email.sendMail(from, to, copyto, subject, content, filename);
//  email.sendMail(from, to, subject, content, filename);
// }

}

在項目中使用這套工具,main方法我注釋掉,然后使用toSendMail(SendMailParam sendMailParam)。
這里定義的SendMailParam 為:

public class SendMailParam {
 private String smtp;
 private String username;
 private String password;
 private String from;//發送人
 private String[] to;//接收人
 //  String[] copyto = {"909891736@qq.com"};
 private String subject;//郵件主題
 private String content;//郵件內容
 private String filepath;//文件拿到的路徑

 public SendMailParam(){
  this.smtp = "smtp.exmail.qq.com";//例子
  this.username = "郵箱賬號";
  this.password = "郵箱密碼";
  this.from = "郵箱";
  this.subject = "";
  this.content = "";
  this.filepath = "";
 }

 public String getSmtp() {
  return smtp;
 }

 public void setSmtp(String smtp) {
  this.smtp = smtp;
 }

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String getFrom() {
  return from;
 }

 public void setFrom(String from) {
  this.from = from;
 }

 public String[] getTo() {
  return to;
 }

 public void setTo(String[] to) {
  this.to = to;
 }

 public String getSubject() {
  return subject;
 }

 public void setSubject(String subject) {
  this.subject = subject;
 }

 public String getContent() {
  return content;
 }

 public void setContent(String content) {
  this.content = content;
 }

 public String getFilepath() {
  return filepath;
 }

 public void setFilepath(String filepath) {
  this.filepath = filepath;
 }
}

maven依賴包

<dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
  <version>1.4.7</version>
 </dependency>

gradle依賴包

compile "javax.mail:mail:1.4.7"

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

洪湖市| 松江区| 集贤县| 延长县| 托克逊县| 大关县| 湖口县| 霞浦县| 准格尔旗| 军事| 花垣县| 原平市| 洪湖市| 阜新市| 大新县| 图木舒克市| 普格县| 阿尔山市| 蒙自县| 塘沽区| 沈丘县| 金堂县| 江源县| 宁安市| 五寨县| 山阴县| 新河县| 天镇县| 尉犁县| 民乐县| 镇远县| 分宜县| 喀喇| 石渠县| 阳新县| 长岭县| 明水县| 满城县| 昌乐县| 石狮市| 牟定县|