java_mail收发邮件

一、邮件简介



示例:创建一个邮件并写到磁盘

package mail;

import javax.mail.*;
import javax.mail.internet.*;

public class SendMail2 {
	public static void main(String args[]){
		
		Session session = Session.getInstance(new Properties());		
		MimeMessage  message = new MimeMessage(session);
		
		try {
			//设置邮件的基本信息
			message.setFrom(new InternetAddress("xx@sina.com"));
			message.setRecipient(Message.RecipientType.TO, new InternetAddress("xx@sina.com"));
			message.setSubject("测试");
			
			//邮件正文
			MimeBodyPart text = new MimeBodyPart();
			text.setContent("我就是我xxx","text/html;charset=UTF-8");
			
			//创建邮件附件
			MimeBodyPart  attach = new MimeBodyPart();
			DataHandler dh = new DataHandler(new FileDataSource("e:\\z_src\\images\\1.jpg"));
			attach.setDataHandler(dh);
			attach.setFileName(dh.getName());
			
			//创建容器描述 数据关系
			MimeMultipart mp = new MimeMultipart();
			mp.addBodyPart(text);
			mp.addBodyPart(attach);
			mp.setSubType("mixed");
			
			message.setContent(mp);
			message.saveChanges();
			
			message.writeTo( new FileOutputStream("e:\\1.eml"));
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
}

示例2:发送邮件

public class SendMail {
	public static void main(String args[]){
		Properties prop = new Properties();
		prop.setProperty("mail.smtp.host", "smtp.sohu.com");
		prop.setProperty("mail.transport.protocol", "smtp");
		prop.setProperty("mail.smtp.auth", "true");				
		try {
			//1、创建Session
			Session session = Session.getInstance(prop);
			session.setDebug(true);

			//2、创建Transport
			Transport ts = session.getTransport();	
			
			//3、创建邮件
			Message message =  makeMessage(session);
					
			//连上邮件服务器
			ts.connect("smtp.sohu.com", "username", "password");	
			//发送邮件
			ts.sendMessage(message, message.getAllRecipients());
			ts.close();
		} catch (MessagingException e) {
			e.printStackTrace();
		}	
	}
	public static Message makeMessage(Session session) throws AddressException, MessagingException{
		MimeMessage  message = new MimeMessage(session);		
		//设置邮件的基本信息
		message.setFrom(new InternetAddress(""));
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(""));
		message.setSubject("test");	
		
		message.setContent("aaa", "text/html");
		return message;
	}
}



转自:传智播客

Java可以通过JavaMail API实现邮件收发。下面是一个简单的JavaMail邮件收发的例子: 1. 导入JavaMail API的jar包。 2. 编写邮件收取代码: ```java import javax.mail.*; import javax.mail.internet.*; import java.util.*; public class ReceiveMail { public static void main(String[] args) { // 邮箱的帐号和密码(替换为自己的邮箱和密码) String user = "xxxxx@xxx.com"; String password = "xxxxx"; // 邮箱的服务地址 Properties props = new Properties(); props.setProperty("mail.store.protocol", "imap"); props.setProperty("mail.imap.host", "imap.xxx.com"); props.setProperty("mail.imap.port", "993"); try { // 连接邮箱 Session session = Session.getDefaultInstance(props); Store store = session.getStore(); store.connect(user, password); // 获取收件箱 Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_ONLY); // 获取邮件数量 int messageCount = folder.getMessageCount(); System.out.println("共有邮件:" + messageCount + "封"); // 获取前20封邮件 Message[] messages = folder.getMessages(1, 20); // 遍历邮件并打印 for (Message message : messages) { System.out.println("主题:" + message.getSubject()); System.out.println("发件人:" + message.getFrom()[0]); System.out.println("收件人:" + message.getAllRecipients()[0]); System.out.println("时间:" + message.getSentDate()); System.out.println("内容:" + message.getContent()); System.out.println("-------------------------------------------------------"); } // 关闭收件箱和连接 folder.close(false); store.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 3. 编写邮件发送代码: ```java import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class SendMail { public static void main(String[] args) { // 发件人邮箱地址和密码(替换为自己的邮箱和密码) String from = "xxxxx@xxx.com"; String password = "xxxxx"; // 收件人邮箱地址(替换为自己的邮箱) String to = "xxxxx@xxx.com"; // 邮件服务器地址 String host = "smtp.xxx.com"; // 创建会话 Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props); try { // 创建邮件 MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("JavaMail邮件发送测试"); message.setText("这是一封JavaMail自动发送的测试邮件。"); // 发送邮件 Transport transport = session.getTransport("smtp"); transport.connect(host, from, password); transport.sendMessage(message, message.getAllRecipients()); transport.close(); System.out.println("邮件发送成功!"); } catch (MessagingException e) { e.printStackTrace(); } } } ``` 以上就是一个简单的JavaMail邮件收发的例子,可以进行参考和学习。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值