Java企业邮箱发送邮件
时间: 2023-07-29 18:07:49 浏览: 99
可以使用JavaMail API来发送企业邮箱邮件。以下是一个简单的示例代码:
```java
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class EmailSender {
public static void main(String[] args) {
String to = "recipient@example.com"; // 收件人邮箱
String from = "sender@example.com"; // 发件人邮箱
String host = "mail.example.com"; // 邮箱服务器地址
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("This is the subject of the email");
message.setText("This is the body of the email");
Transport.send(message);
System.out.println("Email sent successfully.");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
```
需要注意的是,在使用此代码之前,需要先配置JavaMail API。具体的步骤可以参考JavaMail API的官方文档。另外,还需要确保企业邮箱的SMTP服务已经开启,并且知道SMTP服务器的地址和端口号。