Java使用465端口發送郵件

package com.capgemini.tools;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.Properties;

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.mail.Address;

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.MimeMultipart;

import javax.mail.internet.MimeUtility;

public class emailTest {

	private final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

	private String smtpServer; // SMTP服務器地址

	private String port; // 端口

	private String username; // 登錄SMTP服務器的用戶名

	private String password; // 登錄SMTP服務器的密碼

	private List<String> recipients = new ArrayList<String>(); // 收件人地址集合

	private String subject; // 郵件主題

	private String content; // 郵件正文

	private List<String> attachmentNames = new ArrayList<String>(); // 附件路徑信息集合

	public emailTest() {

	}

	public emailTest(String smtpServer, String port, String username,

			String password, List<String> recipients, String subject,

			String content, List<String> attachmentNames) {

		this.smtpServer = smtpServer;

		this.port = port;

		this.username = username;

		this.password = password;

		this.recipients = recipients;

		this.subject = subject;

		this.content = content;

		this.attachmentNames = attachmentNames;

	}

	public void setSmtpServer(String smtpServer) {

		this.smtpServer = smtpServer;

	}

	public void setPort(String port) {

		this.port = port;

	}

	public void setUsername(String username) {

		this.username = username;

	}

	public void setPassword(String password) {

		this.password = password;

	}

	public void setRecipients(List<String> recipients) {

		this.recipients = recipients;

	}

	public void setSubject(String subject) {

		this.subject = subject;

	}

	public void setContent(String content) {

		this.content = content;

	}

	public void setAttachmentNames(List<String> attachmentNames) {

		this.attachmentNames = attachmentNames;

	}
	/**
	 * 
	 * 進行base64加密,防止中文亂碼
	 * 
	 */
	public String changeEncode(String str) {

		try {

			str = MimeUtility.encodeText(new String(str.getBytes(), "UTF-8"),

					"UTF-8", "B"); // "B"代表Base64

		} catch (UnsupportedEncodingException e) {

			e.printStackTrace();

		}

		return str;

	}

	/**
	 * 
	 * 正式發郵件
	 * 
	 */

	public boolean sendMail() {

		Properties properties = new Properties();

		properties.put("mail.smtp.host", smtpServer);

		properties.put("mail.smtp.auth", "true");

		properties.put("mail.smtp.socketFactory.class", SSL_FACTORY); // 使用JSSE的SSL
																		// socketfactory來取代默認的socketfactory

		properties.put("mail.smtp.socketFactory.fallback", "false"); // 只處理SSL的連接,對於非SSL的連接不做處理

		properties.put("mail.smtp.port", port);

		properties.put("mail.smtp.socketFactory.port", port);
		properties.put("mail.smtp.ssl.enable", true);

		Session session = Session.getInstance(properties);

		session.setDebug(true);

		MimeMessage message = new MimeMessage(session);

		try {

			// 發件人

			Address address = new InternetAddress(username);

			message.setFrom(address);

			// 收件人

			for (String recipient : recipients) {

				System.out.println("收件人:" + recipient);

				Address toAddress = new InternetAddress(recipient);

				message.setRecipient(MimeMessage.RecipientType.TO, toAddress); // 設置收件人,並設置其接收類型爲TO
																				// 發送單個

				// message.addRecipient(MimeMessage.RecipientType.TO,
				// toAddress); //發送多個,

				/**
				 * 
				 * TO:代表有健的主要接收者。 CC:代表有健的抄送接收者。 BCC:代表郵件的暗送接收者。
				 * 
				 */

			}

			// 主題

			message.setSubject(changeEncode(subject));

			// 時間

			message.setSentDate(new Date());

			Multipart multipart = new MimeMultipart();

			// 添加文本

			BodyPart text = new MimeBodyPart();

			text.setText(content);

			multipart.addBodyPart(text);

			// 添加附件

			for (String fileName : attachmentNames) {

				BodyPart adjunct = new MimeBodyPart();

				FileDataSource fileDataSource = new FileDataSource(fileName);

				adjunct.setDataHandler(new DataHandler(fileDataSource));

				adjunct.setFileName(changeEncode(fileDataSource.getName()));

				multipart.addBodyPart(adjunct);

			}

			// 清空收件人集合,附件集合

			recipients.clear();

			attachmentNames.clear();

			message.setContent(multipart);

			message.saveChanges();

		} catch (Exception e) {

			e.printStackTrace();

			return false;

		}

		try {

			Transport transport = session.getTransport("smtp");

			transport.connect(smtpServer, username, password);

			transport.sendMessage(message, message.getAllRecipients());

			transport.close();

		} catch (Exception e) {

			e.printStackTrace();

			return false;

		}

		return true;

	}

	public static void main(String[] args) {

		List<String> recipients = new ArrayList<String>();

		recipients.add("[email protected]"); // 收件人,換成正確的郵箱地址

		String subject = "這封郵件是爲了測試SMTP的SSL加密傳輸";

		String content = "這是這封郵件的正文";

		List<String> attachmentNames = new ArrayList<String>();

		attachmentNames.add("E://aaaa.txt");

		emailTest sendMailBySSL = new emailTest("smtp.qq.com", "465",

				"[email protected]", "授權碼", recipients, subject, content,

				attachmentNames);

		sendMailBySSL.sendMail();

	}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章