今天狀況還真不少,也更了解JavaMail,曾因發Mail中文出現亂碼,經由客戶提供sample,從純文字改以HTML方式發送,果然OK。
Properties props = new Properties(); props.put("mail.transport.protocol", "smtp"); Session session = Session.getDefaultInstance(props); session.setDebug(this.debug); MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(this.from)); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(this.recipients)); msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(this.copyTos)); msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(this.blindCopyTo)); msg.setSubject(mailSubject,"UTF-8"); // 主旨轉UTF-8 Multipart mp = new MimeMultipart(); MimeBodyPart mbp = new MimeBodyPart(); mbp.setText(this.mailContent, "text/html;charset=UTF-8"); mp.addBodyPart(mbp); msg.setContent(mp); msg.setContent(this.mailContent, "text/html;charset=UTF-8"); msg.setText(this.mailContent); msg.setSentDate(new Date()); Transport.send(msg); |
從上面看到,mailContent被設置了三次:MimebodyPart的setText、MimeMessage的setContent和MimeMessage的setText。後來試出上述灰色字根本都不需要。只需setContent指定html的charset編碼,就不會出現亂碼。
而Transport.send為何也被灰色掉。透過Internet寄出可以,可是要發給Lotus Notes的Users就有問題了,原因不知為何就是被擋,還是循回老方法,透過Session取得Transport來發送就可以work:
Transport transport = session.getTransport("smtp"); transport.connect(this.host, "", ""); // this.username, this.password); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); |
留言列表