- 浏览: 57860 次
- 性别:
- 来自: 深圳
文章分类
最新评论
First ,create a properies file for config file,
EmailSMTP=mail.*.com FromAddress=email@*.com EmailUser=email EmailPassWord=email ToAddress=tom@*.com;ray@*.com FileName=H://*.txt;H://*.txt ShowFileName=*.txt
then the java code should be:
package com.test; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class Mail { public static void main(String[] args){ String EmailSMTP=""; String FromAddress=""; String EmailUser=""; String EmailPassWord=""; String ToAddress = ""; String FileName = ""; String ShowFileName = ""; //get the config file try { InputStream in = new BufferedInputStream(new FileInputStream("email.properties")); Properties p = new Properties(); p.load(in); ToAddress = p.getProperty("ToAddress"); FileName = p.getProperty("FileName"); ShowFileName = p.getProperty("ShowFileName"); EmailSMTP = p.getProperty("EmailSMTP"); FromAddress = p.getProperty("FromAddress"); EmailUser = p.getProperty("EmailUser"); EmailPassWord = p.getProperty("EmailPassWord"); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Properties props = System.getProperties(); props.setProperty("mail.smtp.host", EmailSMTP); props.put("mail.smtp.auth", "true"); Session s = Session.getInstance(props); s.setDebug(true); MimeMessage message = new MimeMessage(s); try { //from server InternetAddress from = new InternetAddress(FromAddress); message.setFrom(from); //define receiver if(ToAddress.trim().length()>0){ String[] arr = ToAddress.split(";"); int receiverCount = arr.length; if(receiverCount>0){ InternetAddress[] address = new InternetAddress[receiverCount]; for(int i=0;i<receiverCount;i++){ address[i] = new InternetAddress(arr[i]); } message.addRecipients(Message.RecipientType.TO, address); }else{ System.out.println("None receiver!Check the 'ToAddress' please!"); System.exit(0); } } message.setSubject("test"); String content = "automatically send by java api"; message.setContent(content, "text/html;charset=GBK"); //add the attachment Multipart multipart = new MimeMultipart(); MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText("Automatically send by JAVA API!"); multipart.addBodyPart(messageBodyPart); if(FileName.trim().length()>0){ String[] arr = FileName.split(";"); int attCount = arr.length; if(attCount>0){ for(int i=0;i<attCount;i++){ messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(arr[i]); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(arr[i]); multipart.addBodyPart(messageBodyPart); } }else{ System.out.println("None attachment!"); System.exit(0); } } message.setContent(multipart); message.saveChanges(); Transport transport = s.getTransport("smtp"); transport.connect(EmailSMTP, EmailUser, EmailPassWord); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
export as a jar file.
Find this jar and extract it,go to "/META-INF/" to find the MANIFEST.MF file, then add "Main-Class: com.test.Mail" with the enter as the end.
use the jar command to create a new jar: jar -cvfm test.jar manifest.mf com
then put activation.jar;mail.jar;test.jar;runJava.bat;email.properties in the same folder,the runJava.bat file is:
java -cp test.jar;mail.jar;activation.jar com.test.Mail
then we got a bat file,so we can use the timer or assignmanagerment to choose email send time
发表评论
-
清理mongodb最老的数据
2017-01-18 14:20 2680public class MongoDBDataClean ... -
db2
2012-02-20 17:37 631Class.forName("com.ibm.db2 ... -
网上银行one
2011-10-24 23:08 823关于网银的一些认识, ... -
ireport using javabean as the datasource
2010-10-28 14:11 1751转自:http://hi.baidu.com/nieweigu ... -
Export the excel/txt file of report list by java
2010-09-21 16:16 1644First of all, we should click a ... -
batch execution
2010-02-26 17:09 729Statement st = null; try{ ... -
Socket -learning a little from IBM tutorial
2010-01-14 18:43 910There are three files to comple ... -
Simple expample of MessageDigest
2009-11-16 17:57 706When use the encrypted arithmet ... -
judgement method of collections
2009-10-19 18:18 561When the collections want to ev ... -
The difference of some collections
2009-09-24 18:05 680The content of this chapter is ... -
Aop of Spring2.5 doesnot support well in the jdk1.6
2009-08-20 20:53 654The exception throwed by the ap ... -
The usage of log4j
2009-08-05 15:41 613文章出处:http://www.blo ... -
The usage of time and date
2009-07-30 20:46 679There are some usage of time we ... -
Widget developpment by JIL SDK
2009-07-30 19:49 862Widget is an application that c ... -
Testing of override of toString()
2009-07-27 19:15 456At fisrt,thanks for the tips of ... -
Bubble Sort - The arithmetic always queried by interviewer
2009-07-27 18:26 728/** * * @author tanglei ...
相关推荐
start with index.html
Java开发的后台发送单个或多个邮件的工具类,该类稳定性好,邮件发送后不会被当做垃圾邮件拦截,因此我在这里给大家分享一下。
功能描述: 1、发送简单文本邮件 2、发送带附件邮件,本地图片 3、发送带附件邮件,网络图片 4、发送带附件邮件,网络图片2 5、发送带HTML文本邮件
send_email.py
java SMS for send and receive
java程序发送电子邮件例子,很简单,一看就会
JAVA中socket的使用,本界面程序用于传输浮点数据,当收到一个"开始”命令后,开始产生随机数,并将其打包为502个字节的帧,然后发送出去,并每隔一定的时间将发送一次。
java2 me sms_send 手机发送短信代码,对你你帮助的
jsp带附件邮件发送工具,运用了servlet,基本功能都实现了
linux专门为文件传输开放的API sendfile 比你自己写的文件传输程序速度快得多
Send email under Windows Command Line.
receiving messages by dividing the API into two parts: * The first part of the API is the focus of this course --basically, how to send and receive messages independent of the provider/protocol. ...
using clicatell API to send SMS
NULL 博文链接:https://tgwall.iteye.com/blog/1735704
1.怎样用java实现短信发送 Copy to clipboard ...Posted by:jacksunPosted on:2003-04-21 15:02看看西门子的JAVA API,就在com.siemens.mp.gsm.SMS类里有一个方法send(String number, String data)
Delphi Send Email with Gmail 2021
String errMsg = "Could not send email: smtp host address is null"; logger.error(errMsg); throw new CatalogException(errMsg); } try { Properties props = System.getProperties(); ...
package simple; import org.apache.... email.send(); System.out.println("发送邮件成功"); }catch(Throwable e){ System.out.print("发送邮件失败,可能原因是:"); e.printStackTrace(); } } }
NULL 博文链接:https://javaeedevelop.iteye.com/blog/1726261