使用Java发送139邮箱邮件 - 代码示例和步骤
使用Java发送139邮箱邮件,你可以使用JavaMail库。下面是一个示例代码:
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class SendEmail {
public static void main(String[] args) {
// 收件人邮箱地址
String to = 'recipient@example.com';
// 发件人邮箱地址
String from = 'sender@example.com';
// 139邮箱的SMTP服务器地址
String host = 'smtp.139.com';
// 获取系统属性
Properties properties = System.getProperties();
// 设置邮件服务器
properties.setProperty('mail.smtp.host', host);
// 获取默认的Session对象
Session session = Session.getDefaultInstance(properties);
try {
// 创建默认的MimeMessage对象
MimeMessage message = new MimeMessage(session);
// 设置发件人
message.setFrom(new InternetAddress(from));
// 设置收件人
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// 设置邮件主题
message.setSubject('This is the subject');
// 设置邮件正文
message.setText('This is a sample email message.');
// 发送邮件
Transport.send(message);
System.out.println('Email sent successfully.');
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
在代码中,你需要将to和from变量替换为实际的收件人和发件人邮箱地址。你还需要设置139邮箱的SMTP服务器地址,并将其赋值给host变量。然后,使用Transport.send(message)方法发送邮件。
原文地址: https://www.cveoy.top/t/topic/qm5h 著作权归作者所有。请勿转载和采集!