java实现简单邮件发送

package cn.lmj.demo;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail	
{
	public static void main(String[] args) throws Exception
	{
		//设置封装session与邮件服务器交互的元信息
		Properties prop = new Properties();
		//指定连接的邮件服务器的主机名
		prop.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
		//指定采用的邮件发送协议
		prop.setProperty("mail.transport.protocol", "smtp");
		//指定客户端是否向邮件服务器提交验证
		prop.setProperty("mail.smtp.auth", "true");
		
		//利用Properties得到与特定邮件服务器会话的session
		Session session = Session.getInstance(prop);
		//设置是否调试
		session.setDebug(true);
		
		//创建邮件
		Message message = createMessage(session);
		
		//得到Transport对象,利用该对象发送一份邮件
		Transport ts = session.getTransport();
		//首先连接发送人
		ts.connect("xxxxxxxxxx@qq.com","xxxxxxxxx");
		//再发送
		ts.sendMessage(message,message.getAllRecipients());
	}

	private static Message createMessage(Session session) throws Exception
	{
		//利用session创建一封邮件
		MimeMessage message = new MimeMessage(session);
		//设置发件人
		message.setFrom(new InternetAddress("xxxxxxxxxx@qq.com"));
		//设置接收人
		message.setRecipient(Message.RecipientType.TO,new InternetAddress("xxxxxxxxx@sina.cn"));
		//设置邮件内容
		message.setContent("我是Mj,这封邮件是通过程序写给你的,哈哈哈哈!!!","text/html;charset=utf-8");
		//保存
		message.saveChanges();
		
		return message;
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java 可以使用 JavaMail API 实现邮件发送功能。以下是一个简单的代码示例: ``` import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class MailSender { public static void main(String[] args) { final String username = "your-email@example.com"; // 替换为你的邮箱用户名 final String password = "your-password"; // 替换为你的邮箱密码 Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.example.com"); // 替换为你的 SMTP 服务器地址 props.put("mail.smtp.port", "587"); // 替换为你的 SMTP 端口号 Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("your-email@example.com")); // 替换为你的发件人邮箱地址 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient-email@example.com")); // 替换为你的收件人邮箱地址,多个邮箱地址用逗号分隔 message.setSubject("Test Email"); message.setText("This is a test email sent from Java."); Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { throw new RuntimeException(e); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值