SendEmail.properties __________________________________________ emailSender=ATS_UAT@xxx.com emailHost=mlilhkmail01 emailProtocol=smtp emailAuth=false emailPort=25 emailDebug=false errorEmailTo=josh_tl_kei@xxx.com;jimmy_kc_wong@xxx.com errorEmailSubject=ATS Error ______________________________________________ package com.xxx.mil.application.yyy.util; import java.util.*; import java.io.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; import java.util.Date; import java.text.SimpleDateFormat; public class SendEmail { private static boolean emailDebug = false; private static String emailFrom = "ATS@xxx.com"; private static String emailHost = "mlilhkmail01"; private static String emailPort = "25"; private static String emailProtocol = "smtp"; private static String emailAuth = "false"; private static String emailTo = ""; private static String emailCc = ""; private static String emailBcc = ""; private static String emailText = ""; private static String emailSubject = ""; private static String errorEmailTo = ""; private static String errorEmailText = ""; private static String errorEmailSubject = ""; //private static String EMAIL_ATTACHMENT = ""; private static ArrayList emailAttachmentList = new ArrayList(); private static ResourceBundle bundle = null; public SendEmail() { try { bundle = ResourceBundle.getBundle("com.xxx.mil.application.yyy.SendEmail"); String str = ""; str = bundle.getString("emailSender"); if(str != null && !"".equals(str)) { emailFrom = str; } str = bundle.getString("emailHost"); if(str != null && !"".equals(str)) { emailHost = str; } str = bundle.getString("emailProtocol"); if(str != null && !"".equals(str)) { emailProtocol = str; } str = bundle.getString("emailAuth"); if(str != null && !"".equals(str)) { emailAuth = str; } str = bundle.getString("emailPort"); if(str != null && !"".equals(str)) { emailPort = str; } str = bundle.getString("emailDebug"); if(str != null && !"".equals(str)) { emailDebug = Boolean.getBoolean(str); } str = bundle.getString("errorEmailTo"); if(str != null && !"".equals(str)) { errorEmailTo = str; } str = bundle.getString("errorEmailSubject"); if(str != null && !"".equals(str)) { errorEmailSubject = str; } } catch(Exception e) { System.out.println("SendEmail.init Error: " + e.toString()); e.printStackTrace(); } } public boolean sendMail(String subject, ArrayList fileList, String text , String mailto, String mailcc, String mailbcc) throws Exception { boolean bMailSentFlag = false; MimeBodyPart mimebodypart = null; MimeMultipart mimemultipart = null; MimeMessage mimemessage = null; Session session = null; Transport transport = null; Properties props = null; String filename = ""; Logger.writeLogln("Start to send email"); if (hasReceiver(mailto, mailcc, mailbcc)) { props = new Properties(); props.put("mail.transport.protocol",emailProtocol); props.put("mail.smtp.host", emailHost); props.put("mail.smtp.port", emailPort); props.put("mail.smtp.auth", emailAuth); session = Session.getDefaultInstance(props, null); session.setDebug(emailDebug); System.out.println("Mailing Process Started ............"); try{ //header : from , to , cc, subject mimemessage = new MimeMessage(session); mimemessage.setFrom(new InternetAddress(emailFrom)); mimemessage.setSentDate(new java.util.Date()); mimemessage.setSubject(subject); //mailto must be in the form of comma separated instead of semi-colon separated if(!mailto.equals("")) { InternetAddress[] ias = InternetAddress.parse(mailto); mimemessage.setRecipients(Message.RecipientType.TO, ias); System.out.println("EmailTO: " + mailto); Logger.writeLogln("EmailTO: " + mailto); } if(!mailcc.equals("")) { InternetAddress[] ias = InternetAddress.parse(mailcc); mimemessage.setRecipients(Message.RecipientType.CC,ias); System.out.println("EmailCC: " + mailcc); Logger.writeLogln("EmailCC: " + mailcc); } if(!mailbcc.equals("")) { InternetAddress[] ias = InternetAddress.parse(mailbcc); mimemessage.setRecipients(Message.RecipientType.BCC,ias); System.out.println("EmailBCC: " + mailbcc); Logger.writeLogln("EmailBCC: " + mailbcc); } //body mimemultipart = new MimeMultipart(); //1. message mimebodypart = new MimeBodyPart(); mimebodypart.setText(text); mimemultipart.addBodyPart(mimebodypart); Logger.writeLogln("Content: " + text); //2. file attachment try{ if (fileList != null) { for (int i=0; i<fileList.size(); i++) { filename = (String)fileList.get(i); System.out.println("Attaching file..." + filename); mimebodypart = new MimeBodyPart(); FileDataSource filedatasource = new FileDataSource(filename); mimebodypart.setDataHandler(new DataHandler(filedatasource)); mimebodypart.setFileName(filename); // set FILENAME mimemultipart.addBodyPart(mimebodypart); } } } catch(Exception exception3) { bMailSentFlag = false; System.out.println("Error in sending file not been able to attach ......\n" + exception3.getMessage()); } mimemessage.setContent(mimemultipart); System.out.println("Sending mail in progress..."); Transport.send(mimemessage); System.out.println("Sent Successfully.........."); } catch(Exception e){ bMailSentFlag = false; System.out.println("SendEmail.send Error: " + e.toString()); e.printStackTrace(); Logger.writeLogln(e.getMessage()); } bMailSentFlag = true; System.out.println("Mailing Process Ended ............"); } else System.out.println("No email receiver."); Logger.writeLogln("Send email completed"); return bMailSentFlag; } public boolean sendErrorEmail(Exception e) throws Exception { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String subject = errorEmailSubject + " from " + Utility.getHostName() + " @ " + dateFormat.format(now); return sendMail(subject, null, sw.toString(), errorEmailTo, "", ""); } public boolean sendErrorMail(String text) throws Exception { Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); String subject = errorEmailSubject + " from " + Utility.getHostName() + " @ " + dateFormat.format(now); return sendMail(subject, null, text, errorEmailTo, "", ""); } private boolean hasReceiver(String s1, String s2, String s3) { String str = ""; if (s1 != null) str += s1; if (s2 != null) str += s2; if (s3 != null) str += s3; return (str.length() >0 ? true:false); } public static void main(String[] args) { } private void prepare(String reportType, String feedFileName) throws Exception { String propertiesPrefix = ""; String attachment = ""; try { if ("REPORT".equalsIgnoreCase(reportType)) { propertiesPrefix = "Report."; } this.emailAttachmentList.clear(); emailSubject = bundle.getString(propertiesPrefix + "emailSubject"); emailTo = bundle.getString(propertiesPrefix + "emailTo"); emailCc = bundle.getString(propertiesPrefix + "emailCc"); emailBcc = bundle.getString(propertiesPrefix + "emailBcc"); emailText = bundle.getString(propertiesPrefix + "emailText"); if (!"".equals(feedFileName)) { ObjectInputStream in = new ObjectInputStream(new FileInputStream(feedFileName)); attachment = (String)in.readObject(); emailAttachmentList.add(attachment); } } catch (Exception e) { e.printStackTrace(); throw e; } } }
相关推荐
在"mail-demo.zip"这个压缩包中,我们有一个简单的Java邮件发送示例,包括了抄送(CC)和密送(BCC)功能,以及相关的YML配置文件和JAR包。下面将详细介绍这些知识点。 1. **JavaMail API**: JavaMail API 是一套...
Java-Mail API提供了一系列核心类,如Session(代表与SMTP服务器的通信连接)、Transport(处理邮件发送的算法对象)、MimeMessage(表示一封完整的邮件)、InternetAddress(表示邮件地址)以及MimeBodyPart和...
5. **Address**: Address类表示邮件地址,包括From、To、Cc和Bcc字段。有InternetAddress子类专门处理基于SMTP的邮件地址。 6. **Transport**: Transport类负责实际的邮件发送工作。通过调用Transport.send()方法,...
例如,`InternetAddress`可以用于构建`From`、`To`、`Cc`和`Bcc`字段。 2. **邮件加密与安全** - **S/MIME (Secure/Multipurpose Internet Mail Extensions)**: JavaMail支持S/MIME标准,可以对邮件进行加密和签名...
Java Mail 是一个强大的Java库,用于处理电子邮件的发送和接收。这个库支持多种协议,如SMTP(简单邮件传输协议)和POP3(邮局协议),使得开发者能够在Java应用程序中实现复杂的邮件功能。在这个主题中,我们将深入...
4. `Address`: 用于表示邮件地址,包括`InternetAddress`类,用于处理`To`、`From`、`Cc`、`Bcc`等邮件地址。 5. `Multipart`: 用于处理多部分消息,如混合文本和HTML,或者包含多个附件。`MimeMultipart`是实现这...
3. `InternetAddress`: 用于处理邮件地址,包括`from`、`to`、`cc`和`bcc`。`InternetAddress.parse()`方法可以解析邮件地址字符串。 4. `Transport`: 负责将`Message`发送到邮件服务器。调用`Transport.send...
3. **应用过滤规则**:对解析出的邮件应用这些规则,可以使用`Message.RecipientType`枚举值来确定邮件的目标接收者类型,比如TO、CC或BCC。 4. **评分系统**:为了提高过滤精度,可以采用评分系统,为每封邮件分配...
5. **邮件头信息处理**:解析和操作邮件的From、To、Cc、Bcc、Subject等头部信息。 6. **MIME支持**:处理MIME编码的邮件,支持多部分消息和复杂的数据结构。 7. **安全连接**:支持SSL和TLS协议,增强邮件通信的...
### Java Mail 发送邮件知识点详解 #### 一、概述 Java Mail API 是一个用于发送与接收电子邮件的标准 Java 库,支持多种协议如 SMTP (Simple Mail Transfer Protocol) 和 POP3 (Post Office Protocol version 3) ...
- `to`, `cc`, `bcc`:收件人、抄送和密送的电子邮件列表。 - `subject`:邮件的主题。 - `user` 和 `password`:用于身份验证的用户名和密码,通常对应发件人的电子邮件账户。 3. **邮件内容**: - `...
5. **Address**:表示电子邮件地址,包括From、To、Cc和Bcc等。 6. **Part**:邮件的组成部分,可以是纯文本、HTML或者附件。Message是Part的子类,因此可以包含其他Part。 `activation.jar` 文件: 这个JAR文件...
设置`From`、`To`、`Cc`、`Bcc`、`Subject`和`BodyPart`等属性。 3. **构建邮件内容** 对于复杂的邮件内容,可以使用`Multipart`来组合多个`BodyPart`,例如文本和附件。`Multipart`支持两种类型的边界,`Mixed`...
### 发送邮件及邮件附件Java代码实例解析 #### 一、概述 本篇文章将深入解析一个用于发送电子邮件及其附件的Java代码示例。这个示例不仅支持单个邮件的发送,还支持批量发送以及附加多个文件。通过分析这段代码,...
Java Mail 是一个强大的Java库,用于处理电子邮件的发送和接收。这个"Java Mail demo (仅发送邮件)"是一个示例程序,它展示了如何利用Java Mail API来实现邮件发送功能。在Java开发中,如果你需要集成邮件服务,这...
7. **RecipientType**: `RecipientType` 是枚举类型,用于定义邮件的接收者类型,如`TO`(主收件人)、`CC`(抄送)和`BCC`(密送)。 8. **Multipart 和 MimeBodyPart**: `Multipart` 和 `MimeBodyPart` 用于构建...
- `to`, `cc`, `bcc`: 分别表示收件人、抄送和密送列表。 - `subject`: 邮件主题。 - `user`, `password`: 登录邮件服务器的用户名和密码。 - `contentType`: MIME类型,定义邮件内容的类型,如`text/plain`或`...
8. **抄送和密送**: 可以通过`Message.RecipientType.CC`和`Message.RecipientType.BCC`设置抄送和密送的收件人。 在提供的压缩包文件"javamail"中,可能包含了示例代码,展示了如何使用Java Mail API发送邮件。...
本文将深入分析一个Java程序示例——`Mail.java`,它通过Socket编程实现邮件发送功能,并支持添加附件。 #### 二、类设计与功能概述 `Mail.java` 类的设计遵循了面向对象的原则,提供了发送邮件以及添加附件的方法...