javamail邮件发送

javamail邮件发送

 

封装了一个简单的邮件发送的代码,质量不高,不过,二话不说,先看剑。

 

所需包:

dsn.jar

mailapi.jar

pop3.jar

smtp.jar

 

贴上代码:

package com.test.email;

import java.io.File;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
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.MimeMessage.RecipientType;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;


/**
 * Description: 发送Email
 * @author cglin
 * @Version V 1.0
 * @Created at 2014年3月28日 下午4:03:21
 * @Modified by
 *  on 
 */
public class Email{
	
	private static String systemEmail = "[email protected]";
	private static String emailName = "[email protected]";
	private static String emailPwd = "123456";
	private static String emailHost = "mail.cc.com";

	public static void pushEmail(String emailTitle,String emailContent,String ... receiveEmail){
		
		
		Properties prop = new Properties();
		//设置属性值
		prop.put("mail.smtp.auth", "true");//启动验证		
		Session session = Session.getInstance(prop);//创建Session对象
		session.setDebug(true);
		
		for(String rEmail : receiveEmail){
			try {
				
				MimeMessage msg = new MimeMessage(session);//设置邮件的内容
				msg.setFrom(new InternetAddress(systemEmail));//发件人
				//收件人   RecipientType.TO 收件人  CC抄送   CC秘密操作
				msg.setRecipient(RecipientType.TO, new InternetAddress(rEmail,false));
				msg.setSubject(emailTitle);//主题
				
					Multipart mm = new MimeMultipart();//内容设置
					
					BodyPart b = new MimeBodyPart();//正文
					b.setContent(emailContent, "text/html;charset=GBK");
					b.setText(emailContent);
					
					mm.addBodyPart(b);						//将发送的所有BodyPart,存放在mm对象中
				
				msg.setContent(mm);							//设置邮件的内容
				msg.setSentDate(new Date());				//发送邮件的时间
				
				Transport tran = session.getTransport("smtp");				//发送邮件
				tran.connect(emailHost, emailName, emailPwd);				//登陆到服务器
				tran.sendMessage(msg, msg.getAllRecipients());				//发送指定的邮件	
				tran.close();
				System.out.println("邮件发送成功!!!!");
				
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("邮件发送失败!!!");
			}
		}
		
	}
	
	public static void pushAnnexEmail(String emailTitle,String emailContent,String Filepath,String FileTitle,String ... receiveEmail){
		
		Properties prop = new Properties();
		//设置属性值
		prop.put("mail.smtp.auth", "true");		
		Session session = Session.getInstance(prop);//创建Session对象
		session.setDebug(true);
		
		for(String rEmail : receiveEmail){
			try {
				
				MimeMessage msg = new MimeMessage(session);//设置邮件的内容
				msg.setFrom(new InternetAddress(systemEmail));//发件人
				//收件人   RecipientType.TO 收件人  CC抄送   CC秘密操作
				msg.setRecipient(RecipientType.TO, new InternetAddress(rEmail,false));
				msg.setSubject(emailTitle);//主题
				
					Multipart mm = new MimeMultipart();//内容设置
					
					BodyPart b = new MimeBodyPart();//正文
					b.setContent(emailContent, "text/html;charset=GBK");
					b.setText(emailContent);
					
					b.setFileName(MimeUtility.encodeText(FileTitle, "utf-8", null));
					FileDataSource fds = new FileDataSource(new File(Filepath));	//读取文件
					b.setDataHandler(new DataHandler(fds));							//文件位置的指针
					
					mm.addBodyPart(b);						//将发送的所有BodyPart,存放在mm对象中
				
				msg.setContent(mm);							//设置邮件的内容
				msg.setSentDate(new Date());				//发送邮件的时间
				
				Transport tran = session.getTransport("smtp");				//发送邮件
				tran.connect(emailHost, emailName, emailPwd);				//登陆到服务器
				tran.sendMessage(msg, msg.getAllRecipients());				//发送指定的邮件	
				tran.close();
				System.out.println("邮件发送成功!!!!");
				
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("邮件发送失败!!!");
			}
		}
		
	}
	
}


前面的封装的是普通的邮箱发送,第二个方法是带附件的邮件发送。

邮箱账户和密码,还有邮箱服务器自己稍作调整就好。哈哈哈

 

你可能感兴趣的:(邮件,javamail)