javamail发送邮件的例子

package com.cs.mail;

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;

/**
 * 这个发送邮件的步骤比较复杂,要自己去连接,发送,关闭。
 * @author Administrator
 *
 */
public class SmtpTest {
	public static void main(String[] args)throws Exception {
		String title = "java 邮件系统练习";
		String content = "你好,我是XXXX!很高兴认识你!";
		String user1 = "l4@126.com";
		String user2 = "l04@126.com";
		String user3 = "t6an@ssel.com";
		String user4 = "5965444424@qq.com";
		String user5 = "175355633@qq.com";
		String server163 = "top3.126.com";
		String serverSohu = "smtp.126.com";
		//title = new String(title.getBytes("iso-8859-1"));//不用转编码了,否则是乱码
		Properties props = new Properties();
		props.put("mail.smtp.host", serverSohu);// 指定SMTP服务器
		props.put("mail.smtp.auth", "true");// true表示需要SMTP验证,false现在已经不能使用了,服务器要求必须使用true

		Session mailSession = Session.getDefaultInstance(props);//创建一次,以后就直接使用它,发和送应该要使用不同的实例
		//Session mailSession = Session.getInstance(props);//每次调用都创建一个新的

		mailSession.setDebug(true);// 是否在控制台显示debug信息
		Message message = new MimeMessage(mailSession);
		message.setFrom(new InternetAddress(user2));// 发件人
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(user1));// 收件人
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(user4));// 收件人
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(user5));// 收件人
		message.addRecipient(Message.RecipientType.CC, new InternetAddress(user2));// 收件人
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(user3));// 收件人
		message.setSubject(title);// 邮件主题
		message.setText(content);// 邮件内容 纯文本的
		message.saveChanges();

		Transport transport = mailSession.getTransport("smtp");
		//下面三行代码就是连接邮件服务器,发送邮件,关闭连接三个动作
		transport.connect(serverSohu,25, "邮箱的用户名", "密码"); //连接时要使用正确的邮箱和密码,并且要跟setFrom里的一致
		transport.sendMessage(message, message.getAllRecipients());
		transport.close();
		
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用JavaMail API来发送纯文本邮件,下面是一个简单的例子: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendTextEmail { public static void main(String[] args) { final String username = "你的邮箱地址"; final String password = "你的邮箱密码"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); 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("发件人邮箱地址")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("收件人邮箱地址")); message.setSubject("邮件主题"); message.setText("邮件内容"); Transport.send(message); System.out.println("邮件发送成功!"); } catch (MessagingException e) { throw new RuntimeException(e); } } } ``` 其中,需要替换的部分包括: - `username`:你的邮箱地址 - `password`:你的邮箱密码 - `message.setFrom(new InternetAddress("发件人邮箱地址"))`:发件人的邮箱地址 - `message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("收件人邮箱地址"))`:收件人的邮箱地址 - `message.setSubject("邮件主题")`:邮件的主题 - `message.setText("邮件内容")`:邮件的内容 你需要确保你的邮箱已经开启了SMTP服务。如果你使用的是Gmail邮箱,你可能需要在你的邮箱账户中允许不太安全的应用程序访问你的邮箱。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值