1, 引用jar 包
<dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.6</version> </dependency>
2, 代码实现, 不解释
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileReader; import java.io.PrintStream; import java.util.Date; import java.util.Properties; import java.util.StringTokenizer; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message.RecipientType; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class EmailSender { private String from = "darren.li@126.com"; private String to = "darren.li@126.com"; private String mail_subject = "TEST"; private String mail_message = "TEST"; private String filename; private String urlPath = ""; private String attachmentFlag; private String messageType = "text"; private String host = "xx.x.x.com"; public static void main(String[] args) throws Exception { if (args.length > 0) try { if (args.length == 1) { Properties properties = new Properties(); BufferedInputStream stream = new BufferedInputStream(new FileInputStream(args[0])); properties.load(stream); stream.close(); EmailSender es = new EmailSender(); es.setFrom(properties.getProperty("ALERT_FROM")); es.setTo(properties.getProperty("ALERT_TO")); es.setHost(properties.getProperty("ALERT_HOST")); es.setMessageType(properties.getProperty("ALERT_TYPE")); es.setMail_subject(properties.getProperty("ALERT_SUBJECT") + new Date().toString()); es.setFilename(properties.getProperty("ALERT_CONTENT")); es.setAttachmentFlag("N"); StringTokenizer stk_to = new StringTokenizer(properties.getProperty("ALERT_TO"), ";"); String mailTo = ""; while (stk_to.hasMoreTokens()) try { mailTo = stk_to.nextToken(); es.setTo(mailTo); String sResult = es.sendMail(); System.out.println("Mail: " + mailTo + ":" + sResult); } catch (Exception e) { System.out.println("Mail: " + mailTo + ":" + e.getMessage()); } return; }EmailSender es = new EmailSender(); es.setFrom(args[0]); es.setTo(args[1]); es.setHost(args[2]); es.setMessageType("file"); es.setMail_subject(args[3]); es.setUrlPath(args[4]); es.setFilename(args[5]); es.setAttachmentFlag("Y"); try { String sResult = es.sendMail(); System.out.println("Mail: " + es.getTo() + ":" + sResult); } catch (Exception e) { System.out.println("Mail: " + es.getTo() + ":" + e.getMessage()); } } catch (Exception e) { System.out.println("Error Message: " + e.getMessage()); } else System.out.println("Usage: emailSender.jar [conf_file] | emailSender.jar [from][to][host][subject][path][file]"); } public String sendMail() throws Exception { String sResult = ""; try { String success = "N"; StringTokenizer stk_to = new StringTokenizer(getTo(), ";"); StringTokenizer stk_from = new StringTokenizer(getFrom(), ";"); Properties props = System.getProperties(); props.put("mail.smtp.host", this.host); props.put("mail.smtp.auth", "false"); Session session = Session.getDefaultInstance(props, null); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(stk_from.nextToken())); while (stk_to.hasMoreTokens()) { String mailTo = stk_to.nextToken(); message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo)); } message.setSubject(getMail_subject()); BodyPart messageBodyPart = new MimeBodyPart(); if ((getAttachmentFlag() != null) && (getAttachmentFlag().equalsIgnoreCase("y"))) { Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(new FileDataSource(this.urlPath + this.filename))); messageBodyPart.setFileName(getFilename()); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); } if ((getMessageType() != null) && (getMessageType().equalsIgnoreCase("file"))) { BufferedReader in = new BufferedReader(new FileReader(this.urlPath + this.filename)); String line = null; StringBuffer text = new StringBuffer(); while ((line = in.readLine()) != null) { text.append(line + "\n"); } in.close(); setMail_message(text.toString()); } messageBodyPart.setText(getMail_message()); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); Transport.send(message); success = "Y"; return sResult + success; } catch (Exception e) { sResult = sResult + " Exception " + e.getMessage(); }return sResult; } public String getFrom() { return this.from; } public void setFrom(String from) { this.from = from; } public String getTo() { return this.to; } public void setTo(String to) { this.to = to; } public String getMail_subject() { return this.mail_subject; } public void setMail_subject(String mail_subject) { this.mail_subject = mail_subject; } public String getMail_message() { return this.mail_message; } public void setMail_message(String mail_message) { this.mail_message = mail_message; } public String getFilename() { return this.filename; } public void setFilename(String filename) { this.filename = filename; } public String getUrlPath() { return this.urlPath; } public void setUrlPath(String urlPath) { this.urlPath = urlPath; } public String getAttachmentFlag() { return this.attachmentFlag; } public void setAttachmentFlag(String attachmentFlag) { this.attachmentFlag = attachmentFlag; } public String getMessageType() { return this.messageType; } public void setMessageType(String messageType) { this.messageType = messageType; } public void setHost(String host) { this.host = host; } public String getHost() { return this.host; } }
3, 测试
java -jar $WKDIR/emailSender.jar ldaolong@126.comldaolong@126.com mail.com "Hello World" $WKDIR/ 12.csv 111
相关推荐
通过java扩展包javax.mail-1.4.4.jar实现邮件发送功能。 import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart...
基于javax.mail的邮件发送测试工具
基于Maven的javax.mail发送邮件例子程序 pom.xml添加依赖jar: <groupId>javax.mail <artifactId>mail ${javamail.version} </dependency> <javamail.version>1.4.1</javamail.version> /* 功能:...
javax.mail.jar-Java发送邮件API。Apache Email基于javax.mail做了一个开源项目,参见:http://commons.apache.org/proper/commons-email/
JDK官方的Java Mail工具,可实现收发邮件。
采用javax.mail发送邮件,可以发送附件
在Oracle 11.2.0.4中调用 javax.mail 发送邮件时可能会遇到以下错误: ORA-29532: Java call terminated by uncaught Java exception: javax.mail.MessagingException: IOException while sending message; nested ...
JSP发送邮件实例 利用javax.mail和java.activation这两个包. JSP发送邮件实例 利用javax.mail和java.activation这两个包.
JAVA发送邮件demo 代码 及jar包 javax.mail.jar JAVA发送邮件demo 代码 及jar包 javax.mail.jar
使用javax.mail发送邮件,协议为smtp,依赖版本为1.4.7
3. * 发送邮件需要使用的基本信息 4. */ 5. import java.util.Properties; 6. public class MailSenderInfo { 7. // 发送邮件的服务器的IP和端口 8. private String mailServerHost; 9. private String ...
javax.mail jar 包,版本为1.6.0 用于java的邮件发送
该压缩包中包含了javax.mail.jar和activation.jar两个架包,这是java发送邮件必须要导入的,该资源为免费下载资源,希望可以帮助到需要的小伙伴
javax.mail jar 包,版本为1.6.0 用于java的邮件发送 提供给开发者处理电子邮件相关的编程接口。它是Sun发布的用来处理email的API。它可以方便地执行一些常用的邮件传输。我们可以基于JavaMail开发出类似于Microsoft...
java发送邮件的jar包. JavaMail,提供给开发者处理电子邮件相关的编程接口。它是Sun发布的用来处理email的API。它可以方便地执行一些常用的邮件传输。
转载的资源,java语言写的,利用javax.mail写的上传下载文件的source代码,利用stmp发送邮件,利用pop3接收邮件。一般能上网情况下都可以运行成功,但是注意在公司里由于公司经常会进行一些限制,所以有时候会连接...
NULL 博文链接:https://lingf.iteye.com/blog/1131542
java发送邮件所用jar包 包含javax.mail-api.jar,activation.jar两个jar包
java邮件jar包