java发送电子邮件(三)

2014-11-24 07:56:19 · 作者: · 浏览: 2
ansport = mailSession.getTransport("smtp");
// 真正的连接邮件服务器并进行身份验证
transport.connect((String) props.get("mail.smtp.host"), username, password);
// 发送邮件
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(javax.mail.Message.RecipientType.TO));
System.out.println("发送邮件成功!");
transport.close();
}
catch (Exception e)
{
System.err.println("邮件发送失败!" + e.getMessage());
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args)
{
/**
*
*************切切注意********
*
*注意 用此程序发邮件必须邮箱支持smtp服务 在2006年以后申请的163邮箱是不支持的
*我知道sina邮箱 sohu邮箱 qq邮箱支持 但是sina和qq邮箱需要手工设置开启此功能
*所以在测试时最好使用这三个邮箱
*sina邮箱的smtp设置方法如下
*登录sina邮箱 依次点击 邮箱设置--->帐户--->POP/SMTP设置 将开启复选框选中 然后保存
*
*************切切注意********
*/
Email themail = new Email("smtp.163.com");//这里以新浪邮箱为例子
String mailbody = "芜湖职业技术学院发送成功了哦";//邮件正文
themail.setNeedAuth(true);
themail.setSubject("JAVA发邮件的测试");//邮件主题
themail.setBody(mailbody);//邮件正文
themail.setTo("115621060@qq.com");//收件人地址
themail.setFrom("cuiran2001@163.com");//发件人地址
themail.addFileAffix("F:/download/email.rar");// 附件文件路径,例如:C:/222.jpg,*注;"/"的写法; 如果没有可以不写
themail.setNamePass("cuiran2001@163.com", "****");//发件人地址和密码 **改为相应邮箱密码
themail.sendout();
}
}

摘自 小崔的博客