admin管理员组文章数量:1564172
高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");//主机名 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
版权声明:本文标题:JavaDemo——使用javax.mail发送腾讯企业邮件 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1727422403a1113729.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论