java调用163邮箱
时间: 2023-09-14 09:12:01 浏览: 41
可以使用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 EmailSender {
public static void main(String[] args) {
// 发件人电子邮箱
String from = "your_email@163.com";
// 收件人电子邮箱
String to = "recipient_email@example.com";
// 发件人电子邮箱的用户名和密码
final String username = "your_email_username";
final String password = "your_email_password";
// 指定发送邮件的主机为 smtp.163.com
String host = "smtp.163.com";
// 获取系统属性
Properties properties = System.getProperties();
// 设置邮件服务器
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.auth", "true");
// 获取默认的 Session 对象
Session session = Session.getDefaultInstance(properties);
try {
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// 设置 From: 头部头字段
message.setFrom(new InternetAddress(from));
// 设置 To: 头部头字段
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// 设置主题
message.setSubject("测试邮件");
// 设置消息体
message.setText("这是一封测试邮件");
// 发送消息
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("邮件发送成功");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
```
请注意,您需要将示例代码中的电子邮件地址、用户名和密码更改为您自己的值,并确保在使用JavaMail API之前,您已在计算机上安装了JavaMail库。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)