package com.asm;

import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMail {
	private static String host = ""; // smtp服务
	private static String user = ""; // 用户
	private static String pwd = ""; // 密码
	private static String from = ""; // 发件人地
	private static String from_tile = "定时任务"; // 
	private static String to = ""; // 收件人地
	private static String subject = "定时发送邮件任务"; //邮件标题
	
	static int i=0;

	public static void sendEmail(String txt) {
		Properties props = new Properties();
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.auth", "true");
		Session session = Session.getDefaultInstance(props);
		// session.setDebug(false);
		MimeMessage message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress(from, from_tile));
			message.addRecipients(Message.RecipientType.TO,InternetAddress.parse(
					to));
			message.setSubject(subject);
			Multipart multipart = new MimeMultipart();

			// 设置邮件的文本内�?
			BodyPart contentPart = new MimeBodyPart();
			contentPart.setText(txt);
			multipart.addBodyPart(contentPart);

			// 添加附件
			// BodyPart messageBodyPart = new MimeBodyPart();
			// DataSource source = new FileDataSource(affix);
			// 添加附件的内�?
			// messageBodyPart.setDataHandler(new DataHandler(source));
			// 添加附件的标�?
			// 这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
			// sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
			// messageBodyPart.setFileName("=?GBK?B?"+
			// enc.encode(affixName.getBytes()()) + "?=");
			// multipart.addBodyPart(messageBodyPart);

			message.setContent(multipart);
			// 保存邮件
			message.saveChanges();
			// 发�?邮件
			Transport transport = session.getTransport("smtp");
			// 连接服务器的邮箱
			transport.connect(host, user, pwd);
			// 把邮件发送出�?
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

		SendMail.sendEmail(""+i++);

	}
}
最近下载更多
annazhang  LV29 2023年4月7日
1358849392  LV21 2022年11月11日
国服后端  LV10 2021年8月21日
dpfjames  LV6 2019年9月23日
盗码nsdjdhfhfjn  LV4 2019年2月19日
阿昌先生  LV13 2019年1月30日
1943815081  LV13 2019年1月2日
wyx123456  LV19 2018年9月19日
17608417105  LV9 2018年8月21日
MYECLIPSEyar  LV10 2018年7月30日
最近浏览更多
zhujunnan  LV12 3月5日
annazhang  LV29 2023年4月7日
Sunsi12345  LV8 2022年12月2日
1358849392  LV21 2022年11月11日
Gin19960217  LV4 2022年11月10日
adminadminsqwqe  LV7 2022年8月18日
pxqtsht  LV15 2022年3月21日
1234mama  LV19 2022年3月14日
denliv_hui  LV13 2021年10月20日
国服后端  LV10 2021年8月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友