mail----java发送普通邮件

package com.mail;

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

import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class mailtest {
	public static MimeMessage createmail(Session se,String send,String recv1,String recv2,String recv3) throws MessagingException, UnsupportedEncodingException {
		MimeMessage message=new MimeMessage(se);
		message.setSubject("标题","utf-8");

		Address address=new InternetAddress(send,"sengname","utf-8");
		message.setFrom(address);
		message.setContent("正文dsklfjaksldjfklasdfjaskj","text/html;charset=utf-8");
		message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(recv1,"shoujianren1","utf-8"));//发送shojianren
		message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress(recv2,"shoujianren2","utf-8"));//抄送
		message.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress(recv3,"shoujianren3","utf-8"));//密送
		message.setSentDate(new Date());
		
		message.saveChanges();
		return message;
		
	}
	public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {
		// TODO Auto-generated method stub
		Properties props=new Properties();

		props.setProperty("mail.transport.protocol", "smtp");// 使用的协议(JavaMail规范要求)
		props.setProperty("mail.smtp.host", "smtp.qq.com");
		props.setProperty("mail.smtp.port","465");//设置端口  // 发件人的邮箱的 SMTP 服务器地址
		props.setProperty("mail.smtp.auth", "true");// 需要请求认证
		
		// 需要请求认证;只有qq有这一项
		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		props.setProperty("mail.smtp.socketFactory.port", "465");
		
		Session se=Session.getInstance(props);
		se.setDebug(true);//dayin运行信息
		MimeMessage message=createmail(se, "1215230335@qq.com", "1215230335@qq.com", "1215230335@qq.com", "1215230335@qq.com");
		
		Transport transport=(Transport) se.getTransport();
		
		transport.connect("1215230335@qq.com", "nsfurvsloduyghca");//邮箱授权码
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();
	}

}

  

posted @ 2019-02-16 23:49  黑魔法os  阅读(211)  评论(0编辑  收藏  举报