使用java给指定邮箱发邮件(使用163邮箱)

1. 写一个public类,包含一个发送邮件的静态方法(需要什jar包直接一百度就行)
需要修改账号和密码

package com.drshow.utils;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailUtils {

	public static void sendMail(String receiveEmail,String emailContent)
			throws AddressException, MessagingException {
		// 1.创建一个程序与邮件服务器会话对象 Session

		Properties props = new Properties();
		//设置第三方服务器所要设置的基本配置
		//邮箱所应用的协议
		props.setProperty("mail.transport.protocol", "SMTP");
		//主机位置
		props.setProperty("mail.host", "smtp.163.com");
		//是否要进行授权
		props.setProperty("mail.smtp.auth", "true");// 指定验证为true

		// 创建验证器
		Authenticator auth = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("账号@163.com", "密码");
			}
		};

		//获取到目标邮箱返回的session
		Session session = Session.getInstance(props, auth);

		// 2.创建一个Message,它相当于是邮件内容
		Message message = new MimeMessage(session);

		message.setFrom(new InternetAddress("账号@163.com")); // 设置发送者

		message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiveEmail)); // 设置发送方式与接收者

		//设置主题
		message.setSubject("用户激活");
		// message.setText("这是一封激活邮件,请<a href='#'>点击</a>");

		//设置正文内容
		message.setContent(emailContent, "text/html;charset=utf-8");

		// 3.创建 Transport用于将邮件发送

		Transport.send(message);
	}
}

2. 在需要发送邮件的地方调用sendEmail方法

 String result="";
        for (int i=0;i<6;i++) {
            result += random.nextInt(10);
        }
        //获取邮箱
        String mail = req.getParameter("mail");
        try {
            MailUtils.sendMail(mail,"这是一封激活邮件,您的激活码是:"+result);
        } catch (MessagingException e) {
            
        }
        out.print(result);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java发送电子邮件,您需要使用JavaMail API。以下是使用JavaMail API和163邮箱发送电子邮件的示例代码: ```java import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SendEmail { public static void main(String[] args) { String to = "recipient@example.com"; // 收件人电子邮件 String from = "your_email@163.com"; // 发件人电子邮件 String password = "your_email_password"; // 发件人电子邮件密码 // 配置SMTP服务器属性 Properties properties = new Properties(); properties.put("mail.smtp.host", "smtp.163.com"); properties.put("mail.smtp.port", "25"); properties.put("mail.smtp.auth", "true"); // 获取会话对象并进行身份验证 Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() { protected javax.mail.PasswordAuthentication getPasswordAuthentication() { return new javax.mail.PasswordAuthentication(from, password); } }); try { // 创建MimeMessage对象 MimeMessage message = new MimeMessage(session); // 设置发件人 message.setFrom(new InternetAddress(from)); // 设置收件人 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置邮件主题 message.setSubject("Test Email"); // 设置邮件正文 message.setText("Hello, this is a test email from Java."); // 发送电子邮件 Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { throw new RuntimeException(e); } } } ``` 请注意,您需要将您的电子邮件地址和密码替换为示例代码中的“your_email@163.com”和“your_email_password”。另外,请确保在使用此代码之前已经在您的帐户中启用了SMTP服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值