mail----java

Posted 黑魔法os

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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, "[email protected]", "[email protected]", "[email protected]", "[email protected]");
		
		Transport transport=(Transport) se.getTransport();
		
		transport.connect("[email protected]", "nsfurvsloduyghca");//邮箱授权码
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();
	}

}

  

以上是关于mail----java的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数