Java发送邮件(还不太懂,为自己以后用得到的时候再来看看)

package java发送邮件;

import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;

import com.sun.mail.util.MailSSLSocketFactory;

public class SendEmail {
	public static void main(String[] args) {
/*		String to = "abcd@gmail.com";
		String from = "web@gmail.com";
		String host = "localhost";
*/
		//收件人电子邮箱
		String to = "收件人@qq.com";
		//发件人电子邮箱
		String from = "发件人@qq.com";
		//指定发送邮件的主机为 smtp.qq.com
		String host = "smtp.qq.com";//QQ服务器
		//获取系统属性
		Properties properties = System.getProperties();
		//设置邮件服务器
		properties.setProperty("mail.smtp.host", host);
		
		
		/*//这一段是干嘛的,加了也可以
		properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		properties.setProperty("mail.smtp.port", "465");
		properties.setProperty("mail.smtp.socketFactory.port", "465");*/
		
		//开启ssl加密   就可以了
		MailSSLSocketFactory sf;
		try {
			sf = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);
			properties.put("mail.smtp.ssl.enable", "true");
			properties.put("mail.smtp.ssl.socketFactory", sf);
		} catch (GeneralSecurityException e1) {
			e1.printStackTrace();
		}
		
		properties.put("mail.smtp.auth", "true");
		//获取默认session对象
//		Session session = Session.getDefaultInstance(properties);
		Session session = Session.getDefaultInstance(properties, new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication(){
				//发件人用户和密码(密码填授权码)
				return new PasswordAuthentication("发件人@qq.com", "授权码");
			}
		});
		
		try {
			//创建默认的MimeMessage对象
			MimeMessage message = new MimeMessage(session);
			//Set From:头部头字段
			message.setFrom(new InternetAddress(from));
			//Set To:头部头字段
			//发送给一个人
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			//Message.RecipientType.TO type:要被设置为 TO, CC 或者 BCC,这里 CC 代表抄送、BCC 代表秘密抄送
			/*//发送给多个人	传入邮件地址数组
			Address[] addresses = {new InternetAddress(to),new InternetAddress("收件人@qq.com")};
			message.addRecipients(Message.RecipientType.TO, addresses);*/
			//Set Subject:头部头字段
			//主题,可设置编码
			message.setSubject("邮件","utf-8");
			// 设置邮件发送时期
			message.setSentDate(new Date());
			/*//设置消息体
			message.setText("bbb");*/
			/*//发送HTML页面
			message.setContent("<h1>This is actual message</h1>",
                    "text/html" );*/
			
			//发送带附件的邮件
			//创建消部分
			BodyPart messageBodyPart = new MimeBodyPart();
			//消息
//			messageBodyPart.setText("带附件的");
			messageBodyPart.setContent("<h1>带附件的</h1>",
                    "text/html;charset=utf-8");//在这里设置编码
			//创建多重消息
			Multipart multipart = new MimeMultipart();
			//设置本文消息部分
			multipart.addBodyPart(messageBodyPart);
			//附件部分
			messageBodyPart = new MimeBodyPart();
			String filename = "y.txt";
			DataSource source = new FileDataSource(filename);
			messageBodyPart.setDataHandler(new DataHandler(source));
			messageBodyPart.setFileName(filename);
			multipart.addBodyPart(messageBodyPart);
			//发送完整消息
			message.setContent(multipart);
			
			//发送消息
			Transport.send(message);
			System.out.println("send message successfully...");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值