使用JavaMail 发送邮件(SSL认证)

需要的第三方Jar包见附件;

import java.security.Security;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
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;

public class SendMialUtil {

	public static void main(String[] args) throws Exception {
		sendMail("I am the error message for testing!");
	}

	public static void sendMail(String log){
		
		String host = "smtp.qq.com";
		String port = "465";
		String protocol = "smtp";
		String username = "IAmSender@qq.com";
		String password = "password";
		
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
		final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
		Properties props = System.getProperties();
		props.setProperty("mail.smtp.host", host);
		props.setProperty("mail.transport.protocol",protocol);
		props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.setProperty("mail.smtp.port", port);
		
		//There are 5 steps for send email via JavaMail
		try {
			//1. Create session
			Session session = Session.getInstance(props);
			//Open the Session debug model
			session.setDebug(true);
			//2. Get transport via session
			Transport ts = session.getTransport();
			//3. Connect the email server via user name and password
			ts.connect(host,username,password);
			//4. Create email
			Message message = createSimpleMail(session,log);
			//5. Send email
			ts.sendMessage(message, message.getAllRecipients());
			ts.close();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public static MimeMessage createSimpleMail(Session session,String log) throws Exception {
		//Create email object
		MimeMessage message = new MimeMessage(session);
		//Set sender
		message.setFrom(new InternetAddress("IAmSender@qq.com"));
		//Set receiver(s) (TO)
		String to1 = "IAmReceiver1@qq.com";
		String to2 = "IAmReceiver2@qq.com";
		InternetAddress[] toTotal = new InternetAddress[]{new InternetAddress(to1),new InternetAddress(to2)};
		//Invoke the setRecipinents when multiple recipients, otherwise invoke setRecipinent
		message.setRecipients(Message.RecipientType.TO, toTotal);

		//Set receiver(s) (CC)
		String cc = "IAmCCReceiver@qq.com";
		message.setRecipient(Message.RecipientType.CC,new InternetAddress(cc));
		//Set email subject
		message.setSubject("I AM The Subject");
		//Set email content
		MimeBodyPart mp = new MimeBodyPart();
		StringBuffer content = new StringBuffer();
		content.append("ContentHeader");
		// Error log
		content.append(log);
		content.append("ContentFooter");
		mp.setContent(content.toString(),"text/html;charset=UTF-8");
		
		MimeMultipart mmp = new MimeMultipart();
		mmp.addBodyPart(mp);
		message.setContent(mmp);
		//Return email object
		return message;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值