java web 实现qq发送邮箱验证码
时间: 2023-06-01 13:02:35 浏览: 116
首先需要引入JavaMail API和Java Activation Framework (JAF)的包。
以下是一个简单的Java代码示例,用于从QQ邮箱发送验证码邮件:
```java
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail {
public static void main(String[] args) {
final String username = "your_qq_email@qq.com";
final String password = "your_email_password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.qq.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("your_qq_email@qq.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("recipient_email@example.com"));
message.setSubject("Verification Code");
int code = 123456; // replace with actual code
message.setText("Your verification code is: " + code);
Transport.send(message);
System.out.println("Code sent successfully!");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
```
在上面的代码中:
- 将`your_qq_email@qq.com`替换为您的QQ邮箱地址。
- 将`your_email_password`替换为您的QQ邮箱密码。
- 将`recipient_email@example.com`替换为收件人的电子邮件地址。
- 将`123456`替换为实际的验证码。
运行代码后,您应该能够在收件人的邮箱中收到一封包含验证码的电子邮件。