1 回答
TA贡献1817条经验 获得超6个赞
send(msg):是一个静态方法,需要用Transport类直接调用,Transport.send(msg);
或者用Transport的一个实例transport:
transport.connect(host,port,username,password)
transport.sendMessage(msg,mailTo)
transport.close();
建议你全部贴出来看一下啦!我用下面代码可以发出来的!(邮件地址你改一下就可以了)
Properties props = new Properties();
props.setProperty("mail.debug", "true");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.sina.com");
Session session =Session.getInstance(props, new Authenticator(){
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("yourmail@sina.com","yourpassword");
}
});
Message msg = new MimeMessage(session);
msg.setSubject("subject ");
msg.setText("mailtext!");
Address from = new InternetAddress("yourmail@sina.com");//发送地址
Address to = new InternetAddress("mail1@yeah.net");//接收地址
Address cc = new InternetAddress("mail2@163.com");//抄送地址
Address bcc = new InternetAddress("mail3@126.com");//密送地址
msg.setFrom(from);
msg.setRecipient(RecipientType.TO, to);
msg.setRecipient(RecipientType.CC, cc);
msg.setRecipient(RecipientType.BCC, bcc);
Transport.send(msg);//这个直接包含了打开连接/发送邮件/关闭连接
添加回答
举报