zl程序教程

您现在的位置是:首页 >  其他

当前栏目

JavaDemo——使用javax.mail发送腾讯企业邮件

企业腾讯 发送 邮件 mail javax JavaDemo 使用
2023-09-11 14:16:28 时间

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

<dependency>
		    <groupId>javax.mail</groupId>
		    <artifactId>mail</artifactId>
		    <version>1.5.0-b01</version>
		</dependency>

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.com");//主机名	smtp.exmail.qq.com:腾讯企业邮箱		smtp.qq.com:qq邮箱
//		props.put("mail.smtp.host", "smtp.qq.com");
		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.cn"));//设置发件人(必须与授权地址一致)
//			msg.setSentDate(new Date("2020/11/11"));//设置发送时间(显示)
			msg.setRecipient(Message.RecipientType.TO, new InternetAddress("xxxxxx@qq.com"));//设置一个收件人,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.cn", "password");//登陆邮箱
			trans.sendMessage(msg, msg.getAllRecipients());
			trans.close();
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

}

结果:

 

参考:

https://www.cnblogs.com/xmqa/p/8458300.html

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