📄 myfirstmail.java~15~
字号:
package asdf;
import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
public class MyFirstMail {
protected Session mailSession;
protected Message msg;
public MyFirstMail() throws Exception {
init();
}
public static void main(String[] args) {
try {
new MyFirstMail().sendMail();
System.out.print("邮件已发");
} catch (Exception e) {
e.printStackTrace();
}
} //初始化服务器环境
public void init() throws Exception {
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.port", "25");
mailSession = Session.getDefaultInstance(props, null); ;
}
public void sendMail() throws Exception {
try { //从哪里发的邮件
msg.setFrom(new InternetAddress("snake35]@localhost.com"));
//发送到目标邮件
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("snake35@163.com"));
//抄送的接收者
msg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse("snake35@163.com"));
//暗送的接收者
msg.setRecipients(Message.RecipientType.BCC,
InternetAddress.parse("snake35@163.com"));
//设置发送时间
msg.setSentDate(new java.util.Date());
//设置邮件标题
msg.setSubject("a test mail");
//设置邮件内容
msg.setText("this is the email content");
//指定协议发送消息的对像
Transport transport = mailSession.getTransport("smtp");
//发送消息
Transport.send(msg);
} catch (Exception e) {
throw e;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -