send email by java api

本文提供了一个使用Java发送带附件电子邮件的示例代码。通过配置属性文件,设置SMTP服务器及登录凭证,实现从指定地址发送邮件到多个接收者,并附带多个文件作为附件。

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值