java由gmail或者163邮箱发送

package com.piend.sqlserver.email;

// Copyright YiFan SoftWare

import java.util.Date;
import java.util.Properties;

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

public class EMailUtil {
	/**
	 * 发信人信息
	 */
	// 发信人的地址
	private String fromAdress = "[email protected]";
	// SMTP邮件服务器的登陆账号密码
	private String password = "tc19880909";
	// 邮件服务器的地址 smtp.163.com
	private String smtpAddress = "smtp.163.com";
	// SMTP服务器端口号
	private String port = "25";
	// SMTP邮件协议
	private String protocol = "smtp";
	/**
	 * 收信人信息
	 */
	// 收信人的地址
	private String toAddress = "[email protected]";
	// 邮件的主题
	private String subject;
	// 发送时间
	private Date sendDate = new Date();
	// 邮件内容
	private String content;

	public static void main(String[] args) {
		EMailUtil eutil = new EMailUtil();
		eutil.setSmtpAddress("smtp.gmail.com");
		eutil.setFromAdress("[email protected]");
		eutil.setPassword("guolong19880909");
		eutil.setPort("465");
		eutil.setSubject("刘德华演唱会");
		eutil.setContent("12333444444444444444444444444444444444444444444444444444444444444444");
		eutil.sendEmail();
	}

	public void setFromAdress(String fromAdress) {
		this.fromAdress = fromAdress;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setToAddress(String toAddress) {
		this.toAddress = toAddress;
	}

	public void setSmtpAddress(String smtpAddress) {
		this.smtpAddress = smtpAddress;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	/**
	 * 
	 * 发送邮件
	 * 
	 */
	public void sendEmail() {
		Properties props = new Properties();
		// 指定邮件传输协议
		props.setProperty("mail.transport.protocol", this.protocol);
		// 指定smtp服务器地址
		props.setProperty("mail.smtp.host", this.smtpAddress);
		// 指定smtp服务器端口
		props.setProperty("mail.stmp.port", this.port);
		// 是否验证
		props.setProperty("mail.smtp.auth", "true");
		props.put("mail.debug", "false");
		// gmail发送
		if ("smtp.gmail.com".equals(this.smtpAddress)) {
			props.setProperty("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
			props.setProperty("mail.smtp.socketFactory.fallback", "false");
			props.setProperty("mail.smtp.socketFactory.port", "465");
		}
		try {
			// c创建会话
			Session session = Session.getInstance(props);
			// 构建邮件体
			MimeMessage mime = new MimeMessage(session);
			// 创建邮件信息
			mime.setFrom(new InternetAddress(this.fromAdress));
			mime.setRecipients(RecipientType.TO, InternetAddress.parse(this.toAddress));
			mime.setSubject(this.subject);
			mime.setSentDate(this.sendDate);
			MimeMultipart body = new MimeMultipart("mixed");//related
			// 创建文本
			MimeBodyPart html = new MimeBodyPart();
			html.setHeader("Content-Transfer-Encoding", "base64");
			html.setContent(this.content, "text/html;charset=UTF-8");
			// 添加附件
			//FileDataSource fileds = new FileDataSource("D:\\11.txt");
			//FileDataSource fileds = new FileDataSource("D:\\11.txt");
			//html.setDataHandler(new DataHandler(fileds));
			//html.setFileName(MimeUtility.encodeWord(fileds.getName(),"GB2312",null));//设置附件文件名
			body.addBodyPart(html);
			
			mime.setContent(body);
			mime.saveChanges();
			Transport trans = session.getTransport();
			// 用户与密码
			trans.connect(this.smtpAddress, this.fromAdress, this.password);
			trans.sendMessage(mime, mime.getAllRecipients());
			trans.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getUrl(String email) {

		String result = null;
		String[] emailurl = { "http://mail.yeah.net", "www.gmail.com",
				"http://mail.sohu.com/", "http://mail.sogou.com/",
				"mail.foxmail.com", "http://mail.qq.com/",
				"http://www.56.com/home.html", "http://mail.cn.yahoo.com/?cns",
				"http://mail.sina.com.cn/", "http://mail.hexun.com/",
				"http://login.live.com/", "http://email.163.com/",
				"http://mail.126.com/", "http://172.16.10.33:6080/index.php" };
		String str = email.substring(email.indexOf("@") + 1, email
				.lastIndexOf("."));
		for (int i = 0; i < emailurl.length; i++) {
			if (emailurl[i].indexOf(str) != -1) {
				result = emailurl[i];
				break;
			}
		}
		if (result == null) {
			result = emailurl[emailurl.length - 1];
		}
		return result;
	}
}

 

你可能感兴趣的:(javamail)