`
tanglei198577
  • 浏览: 57860 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

send email by java api

    博客分类:
  • java
阅读更多

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics