Java发送邮件_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java发送邮件

Java发送邮件

 2017/12/28 18:16:26  我的无奈  程序员俱乐部  我要评论(0)
  • 摘要:最近做了个java发送邮件的功能,跟大家分享下publicfinalclassMail{/***日志跟踪器*/privatestaticfinalLoggerLOGGER=LoggerFactory.getLogger(Mail.class);/**发送邮件的服务器的IP*/privateStringhost;/**邮件发送者的地址*/privateStringfrom;/**发件人昵称*/privateStringnick;/**邮件接收者的地址*/privateStringto
  • 标签:Java 邮件 发送邮件

最近做了个java发送邮件的功能,跟大家分享

class="java" name="code">public final class Mail {

	/**
	 * 日志跟踪器
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(Mail.class);
	
	/** 发送邮件的服务器的IP*/
	private String host;
	/** 邮件发送者的地址*/
	private String from;
	/** 发件人昵称*/
	private String nick;
	/** 邮件接收者的地址*/
	private String to;
	/** 邮件主题*/
	private String subject;
	/** 邮件的文本内容*/
	private String body;
	/** 发送邮件的服务器的端口*/
	private String port;
	private EmailAutherticator auth;
	
	private static Mail instance = null;

	private Mail() {
		init();
	}

	private void init() {
		final String emailAddress = "*****";//邮箱地址
		final String emailPwd = "****";//邮箱:设置-》账号开启IMAP/SMTP服务的密码
		 
		final EmailAutherticator emailAuth = new EmailAutherticator(emailAddress, emailPwd);
		host = "";
		from = "***";//邮箱地址
		nick = "****";//标题 
		port = "465"; 
		this.setAuth(emailAuth);
		this.setHost(host);
		this.setNick(nick);
		this.setFrom(from);
		this.setPort(port);
	}
	
	/**
	 * 获得对象
	 * @return
	 */
	public static Mail getInstance() {
		synchronized(Mail.class){
			if (instance == null) {
				instance = new Mail();
			}
		}
		return instance;
	}

	public String transferChinese(final String strText) {
		String tempText = "";
		try {
			tempText = MimeUtility.encodeText(new String(strText.getBytes(), "UTF-8"), "UTF-8", "B");
		} catch (Exception e) {
			LOGGER.error("编码异常:{}",e.getMessage(), e);
		}
		return tempText;
	}

	/**
	 * 发送邮件
	 * @throws MessagingException 
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	public void sendMail() throws MessagingException, UnsupportedEncodingException {
		final Properties props = new Properties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");
		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.port", port);
		props.setProperty("mail.smtp.socketFactory.port", port);

		final Session session = Session.getDefaultInstance(props, auth);
		final MimeMessage message = new MimeMessage(session);
		message.setContent("Hello", "text/plain");
		message.setSubject(subject, "utf-8");// 设置邮件主题
		message.setSentDate(DateUtil.getNow());// 设置邮件发送时期
		final Address address = new InternetAddress(from, nick, "utf-8");

		message.setFrom(address);// 设置邮件发送者的地址
		if(to.length() <= 0){
			LOGGER.warn("邮件接收者的地址为空");
			return;
		}
		final Address toaddress = new InternetAddress(to);// 设置邮件接收者的地址
		message.addRecipient(Message.RecipientType.TO, toaddress);
		// 创建一个包含HTML内容的MimeBodyPart
		final Multipart mainPart = new MimeMultipart();
		final BodyPart html = new MimeBodyPart();
		html.setContent(body, "text/html; charset=utf-8");
		mainPart.addBodyPart(html);
		// 将MiniMultipart对象设置为邮件内容
		message.setContent(mainPart);
		try {
			Transport.send(message);
		} catch (MessagingException e) {
			LOGGER.error("Send Email founds error!",e);
		}
	}

	/**
	 * 获取发送邮件的服务器的IP
	 * @return host
	 */
	public String getHost() {
		return host;
	}

	/**
	 * 设置发送邮件的服务器的IP
	 * @param  host
	 */
	public void setHost(final String host) {
		this.host = host;
	}

	/**
	 * 获取邮件发送者的地址
	 * @return from
	 */
	public String getFrom() {
		return from;
	}

	/**
	 * 设置邮件发送者的地址
	 * @param  from
	 */
	public void setFrom(final String from) {
		this.from = from;
	}

	/**
	 * 获取发件人昵称
	 * @return nick
	 */
	public String getNick() {
		return nick;
	}

	/**
	 * 设置发件人昵称
	 * @param  nick
	 */
	public void setNick(final String nick) {
		this.nick = nick;
	}

	/**
	 * 获取邮件接收者的地址
	 * @return to
	 */
	public String getTo() {
		return to;
	}

	/**
	 * 设置邮件接收者的地址
	 * @param  to
	 */
	public void setTo(final String to) {
		this.to = to;
	}

	/**
	 * 获取邮件主题
	 * @return subject
	 */
	public String getSubject() {
		return subject;
	}

	/**
	 * 设置邮件主题
	 * @param  subject
	 */
	public void setSubject(final String subject) {
		this.subject = subject;
	}

	/**
	 * 获取邮件的文本内容
	 * @return body
	 */
	public String getBody() {
		return body;
	}

	/**
	 * 设置邮件的文本内容
	 * @param  body
	 */
	public void setBody(final String body) {
		this.body = body;
	}

	/**
	 * 获取发送邮件的服务器的端口
	 * @return port
	 */
	public String getPort() {
		return port;
	}

	/**
	 * 设置发送邮件的服务器的端口
	 * @param  port
	 */
	public void setPort(final String port) {
		this.port = port;
	}

	/**
	 * 获取auth
	 * @return auth
	 */
	public EmailAutherticator getAuth() {
		return auth;
	}

	/**
	 * 设置auth
	 * @param  auth
	 */
	public void setAuth(EmailAutherticator auth) {
		this.auth = auth;
	}

	public void readActiveMailMsg() {
		this.readMsg("/res/mail.msg");
	}

	/**
	 * 读取消息
	 * @param filename
	 */
	private void readMsg(final String filename) {
		final StringBuilder sb = new StringBuilder();
		final InputStream fis = Mail.class.getResourceAsStream(filename);
		InputStreamReader isr = null;
		try {
			isr = new InputStreamReader(fis, "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			LOGGER.error("读取文件遇见不正确的文件编码,{}!", e1.getMessage(), e1);
		}
		final BufferedReader br = new BufferedReader(isr);
		String line = "";
		try {
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
		} catch (IOException e) {
			LOGGER.error(e.getMessage(), e);
		}
		final String msg = sb.toString();
		setBody(msg);
	}

}

??EmailAutherticator类:

public class EmailAutherticator extends Authenticator {
	
	private String username;

	private String password;

	public EmailAutherticator(String user, String pwd) {
		super();
		username = user;
		password = pwd;
	}

	public PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(username, password);
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}

?

发表评论
用户名: 匿名