admin管理员组

文章数量:1558996

高jdk版本导入javax.mail,maven导入:


		    javax.mail
		    mail
		    1.5.0-b01
		

demo:

/**
 * 2019年6月25日下午5:53:19
 */
package testqqmail;
import java.io.file;
import java.util.properties;
import javax.activation.datahandler;
import javax.activation.filedatasource;
import javax.mail.bodypart;
import javax.mail.message;
import javax.mail.messagingexception;
import javax.mail.nosuchproviderexception;
import javax.mail.session;
import javax.mail.transport;
import javax.mail.internet.addressexception;
import javax.mail.internet.internetaddress;
import javax.mail.internet.mimebodypart;
import javax.mail.internet.mimemessage;
import javax.mail.internet.mimemultipart;
/**
 * @author xwf
 *
 */
public class testqqmail {
	/**
	 * @param args
	 */
	public static void main(string[] args) {
		properties props = new properties();
		props.put("mail.transport.protocol", "smtp");//链接协议
		props.put("mail.smtp.host", "smtp.exmail.qq");//主机名	smtp.exmail.qq:腾讯企业邮箱		smtp.qq:qq邮箱
//		props.put("mail.smtp.host", "smtp.qq");
		props.put("mail.smtp.port", 465);//端口号
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.ssl.enable", "true");//使用ssl安全链接
		props.put("mail.debug", "true");//控制台打印debug信息
		try {
			session session = session.getinstance(props);//获得回话
			message msg = new mimemessage(session);//获取邮件
			
			msg.setsubject("主题主题");//主题
			msg.setfrom(new internetaddress("xxxxxxx@xxx"));//设置发件人(必须与授权地址一致)
//			msg.setsentdate(new date("2020/11/11"));//设置发送时间(显示)
			msg.setrecipient(message.recipienttype.to, new internetaddress("xxxxxx@qq"));//设置一个收件人,to
//			msg.setrecipients(message.recipienttype.to, new internetaddress[] {new internetaddress("")});//设置多个收件人
//			msg.setrecipients(message.recipienttype.cc, arg1);//抄送,cc
//			msg.setrecipients(message.recipienttype.bcc, arg1);//密送,bcc
//			msg.setreplyto(addresses);//回复
			
			//if需要发送附件( 文本)
			mimemultipart multipart = new mimemultipart();
			//设置附件
			bodypart filebodypart = new mimebodypart();
			datahandler dh = new datahandler(new filedatasource(new file("e:/md5.h")));
			filebodypart.setdatahandler(dh);
			filebodypart.setfilename("fileabc.h");//设置附件名
			multipart.addbodypart(filebodypart);
			//设置内容
			bodypart textbodypart = new mimebodypart();
			textbodypart.settext("这是带附件的邮件。");
			multipart.addbodypart(textbodypart);
			msg.setcontent(multipart);
			
			//else只发文本
//			msg.settext("内容内容内容内容内容\n内容内容内容内容内容内容内容---------内容");
			
			transport trans = session.gettransport();
			trans.connect("xxxxxxx@xxx", "password");//登陆邮箱
			trans.sendmessage(msg, msg.getallrecipients());
			trans.close();
		} catch (addressexception e) {
			e.printstacktrace();
		} catch (nosuchproviderexception e) {
			e.printstacktrace();
		} catch (messagingexception e) {
			e.printstacktrace();
		}
	}
}

结果:

 

参考:

https://wwwblogs/xmqa/p/8458300.html

https://blog.csdn/luo201227/article/details/28425283

本文标签: 腾讯邮件企业javademojavax