java代码如何使用javax.mail发送腾讯企业邮件呢?

戚薇 Java经验 发布时间:2023-06-24 22:36:03 阅读数:2110 1
下文笔者讲述使用javax.mail发送企业邮件的方法分享,如下所示
java发送邮件的实现思路:
   1.引入相应的依赖2
   2.定义相应的properties
   3.定义Message
   4.定义Transport对象
   5.使用Transport对象的sendMessage方法,发送消息
例:Java发送邮件的示例分享
//1.引入依赖

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


//2.编写相应的测试代码

package com.java265;
 
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;
 

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("2023/06/24"));//设置发送时间(显示)
			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.Java265.com/JavaJingYan/202306/16876174426885.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者