java实现电子邮件发送

本文介绍了使用Java发送电子邮箱的方法。首先要做发送邮箱前的准备,开启POP3、SMTP服务并引入javax.mail.jar。接着阐述发送步骤,包括创建邮箱配置、获取Session对象、Transport对象及连接、创建MimeMessage对象、发送邮件和关闭资源,还给出了代码实现。

发送邮箱前的准备:

开启POP3,SMTP服务

 

使用java发送邮件需要javax.mail.jar

发送电子邮箱的步骤:

1.创建邮箱配置(使用的邮箱类型,协议,是否需要认证等)

smtp.qq.com QQ邮箱服务器地址,smtp.126.com 网页邮箱服务器地址

2.获取Session对象(QQ邮箱需要授权码,其他邮箱暂时不需要)

3.从Session中获取Transport对象(可以理解为打开了邮箱)

4.从Transport对象获取连接(可以理解为点开了写信)

5.创建一个MimeMessage对象,可以设置收件人,标题,内容,附件等(可以理解为写邮件的区域)

6.发送邮件

7.关闭资源

代码实现如下:



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

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
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 MailDemo {
	// QQ的授权码
	static String QQSecurity = "ztfcwtdzleptcbbj";

	static String QQHost = "smtp.qq.com";

	public static void main(String[] args) {
		
		try {
			// 1.创建一个配置文件并保存
			Properties properties = new Properties();
			// 使用的协议(JavaMail规范要求)
			properties.setProperty("mail.transport.protocol", "smtp");
			// 使用的的邮箱类型的服务器地址
			// smtp.qq.com QQ邮箱服务器地址
			// smtp.126.com 网页邮箱服务器地址
			properties.setProperty("mail.smtp.host", QQHost);
			// 需要请求认证
			properties.setProperty("mail.smtp.auth", "true");

			// 关于QQ邮箱,还要设置SSL加密,加上以下代码即可
			MailSSLSocketFactory sf = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);
			properties.put("mail.smtp.ssl.enable", "true");
			properties.put("mail.smtp.ssl.socketFactory", sf);

			// 2.创建会话对象,相当于打开了邮箱
			//Session session = Session.getInstance(properties);
			
			// QQ邮箱所需要的有授权码的session对象 
	        Session session = Session.getDefaultInstance(properties, new Authenticator() {
	            public PasswordAuthentication getPasswordAuthentication() {
	                //发件人邮件用户名、授权码
	                return new PasswordAuthentication("545646733@qq.com", QQSecurity);
	            }
	        });

			// 设置为debug模式, 可以查看详细的发送 log
			session.setDebug(true);
			// 3.根据 Session 获取邮件传输对象,相当于点击了写信
			Transport transport = session.getTransport();
			//4.使用邮箱的用户名和授权码连上邮件服务器
			String account = "321069526@qq.com";
			transport.connect(QQHost, account, QQSecurity);
			//5.创建邮箱
			MimeMessage message = new MimeMessage(session);
			//指定发送人的邮箱
			message.setFrom(new InternetAddress(account));
			//指定收件人的邮箱
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(account));
			
			//设置图片
			MimeBodyPart img = new MimeBodyPart();
			
			//图片地址
			DataHandler handler = new DataHandler(new FileDataSource("src/resources/sis.jpg"));
			//添加图片
			img.setDataHandler(handler);
			//设置图片ID
			img.setContentID("img.jpg");
			
			// 准备正文数据
	        MimeBodyPart text = new MimeBodyPart();
	        text.setContent("这是一封邮件正文带图片<img src='cid:img.jpg'/>的邮件", "text/html;charset=UTF-8");

			//设置关系,拼接邮件内容
	        MimeMultipart mm = new MimeMultipart();
	        mm.addBodyPart(text);
	        mm.addBodyPart(img);
	        mm.setSubType("related");

	        //设置到消息中,保存修改
	        message.setContent(mm);
	        message.saveChanges();
	        
			//6、发送邮件
			transport.sendMessage(message, message.getAllRecipients());
	        //关闭资源
			transport.close();
			
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (GeneralSecurityException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}

	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值