java 调用163邮箱发送邮件
时间: 2023-02-15 13:37:09 浏览: 103
要在 Java 中调用 163 邮箱发送邮件,你需要使用 JavaMail API。
首先,你需要在你的 Java 项目中引入 JavaMail API 库。你可以在这里下载它:https://javaee.github.io/javamail/downloads/index.html
然后,你可以使用以下代码来调用 163 邮箱发送邮件:
```
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 SendMailTLS {
public static void main(String[] args) {
final String username = "your_email@163.com";
final String password = "your_password";
Properties prop = new Properties();
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", "smtp.163.com");
prop.put("mail.smtp.port", "587");
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from_email@163.com"));
message.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse("to_email@gmail.com")
);
message.setSubject("Testing Gmail TLS");
message.setText("Dear Mail Crawler,"
+ "\n\n Please do not spam my email!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
```
在上面的代码中,你需要将 `your_email@163.com` 替换为你的 163 邮箱地址,将 `your_password` 替换为你的邮箱密码,并将 `from_email@163.com` 替换为发件人的邮箱地址,将 `to_email@gmail.com` 替换为
阅读全文