javamail发送邮件的简单实例Word下载.docx
- 文档编号:3204169
- 上传时间:2023-05-01
- 格式:DOCX
- 页数:8
- 大小:17.35KB
javamail发送邮件的简单实例Word下载.docx
《javamail发送邮件的简单实例Word下载.docx》由会员分享,可在线阅读,更多相关《javamail发送邮件的简单实例Word下载.docx(8页珍藏版)》请在冰点文库上搜索。
//邮件发送者的地址
privateStringfromAddress;
//邮件接收者的地址
privateStringtoAddress;
//登陆邮件发送服务器的用户名和密码
privateStringuserName;
privateStringpassword;
//是否需要身份验证
privatebooleanvalidate=false;
//邮件主题
privateStringsubject;
//邮件的文本内容
privateStringcontent;
//邮件附件的文件名
privateString[]attachFileNames;
/***//**
*获得邮件会话属性
*/
publicPropertiesgetProperties(){
Propertiesp=newProperties();
p.put("
mail.smtp.host"
this.mailServerHost);
mail.smtp.port"
this.mailServerPort);
mail.smtp.auth"
validate?
"
true"
:
false"
);
returnp;
}
publicStringgetMailServerHost(){
returnmailServerHost;
publicvoidsetMailServerHost(StringmailServerHost){
this.mailServerHost=mailServerHost;
}
publicStringgetMailServerPort(){
returnmailServerPort;
publicvoidsetMailServerPort(StringmailServerPort){
this.mailServerPort=mailServerPort;
publicbooleanisValidate(){
returnvalidate;
publicvoidsetValidate(booleanvalidate){
this.validate=validate;
publicString[]getAttachFileNames(){
returnattachFileNames;
publicvoidsetAttachFileNames(String[]fileNames){
this.attachFileNames=fileNames;
publicStringgetFromAddress(){
returnfromAddress;
publicvoidsetFromAddress(StringfromAddress){
this.fromAddress=fromAddress;
publicStringgetPassword(){
returnpassword;
publicvoidsetPassword(Stringpassword){
this.password=password;
publicStringgetToAddress(){
returntoAddress;
publicvoidsetToAddress(StringtoAddress){
this.toAddress=toAddress;
publicStringgetUserName(){
returnuserName;
publicvoidsetUserName(StringuserName){
this.userName=userName;
publicStringgetSubject(){
returnsubject;
publicvoidsetSubject(Stringsubject){
this.subject=subject;
publicStringgetContent(){
returncontent;
publicvoidsetContent(StringtextContent){
this.content=textContent;
}
第二个类:
SimpleMailSender.java
importjava.util.Date;
importjavax.mail.Address;
importjavax.mail.BodyPart;
importjavax.mail.Message;
importjavax.mail.MessagingException;
importjavax.mail.Multipart;
importjavax.mail.Session;
importjavax.mail.Transport;
importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeBodyPart;
importjavax.mail.internet.MimeMessage;
importjavax.mail.internet.MimeMultipart;
*简单邮件(不带附件的邮件)发送器
BT下载
publicclassSimpleMailSender {
*以文本格式发送邮件
*@parammailInfo待发送的邮件的信息
*/
publicbooleansendTextMail(MailSenderInfomailInfo){
//判断是否需要身份认证
MyAuthenticatorauthenticator=null;
Propertiespro=mailInfo.getProperties();
if(mailInfo.isValidate()){
//如果需要身份认证,则创建一个密码验证器
authenticator=newMyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
//根据邮件会话属性和密码验证器构造一个发送邮件的session
SessionsendMailSession=Session.getDefaultInstance(pro,authenticator);
try{
//根据session创建一个邮件消息
MessagemailMessage=newMimeMessage(sendMailSession);
//创建邮件发送者地址
Addressfrom=newInternetAddress(mailInfo.getFromAddress());
//设置邮件消息的发送者
mailMessage.setFrom(from);
//创建邮件的接收者地址,并设置到邮件消息中
Addressto=newInternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO,to);
//设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
//设置邮件消息发送的时间
mailMessage.setSentDate(newDate());
//设置邮件消息的主要内容
StringmailContent=mailInfo.getContent();
mailMessage.setText(mailContent);
//发送邮件
Transport.send(mailMessage);
returntrue;
}catch(MessagingExceptionex){
ex.printStackTrace();
}
returnfalse;
*以HTML格式发送邮件
*@parammailInfo待发送的邮件信息
publicstaticbooleansendHtmlMail(MailSenderInfomailInfo){
//如果需要身份认证,则创建一个密码验证器
//Message.RecipientType.TO属性表示接收者的类型为TO
//MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
MultipartmainPart=newMimeMultipart();
//创建一个包含HTML内容的MimeBodyPart
BodyParthtml=newMimeBodyPart();
//设置HTML内容
html.setContent(mailInfo.getContent(),"
text/html;
charset=utf-8"
mainPart.addBodyPart(html);
//将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
}
第三个类:
MyAuthenticator.java
importjavax.mail.*;
publicclassMyAuthenticatorextendsAuthenticator{
StringuserName=null;
Stringpassword=null;
publicMyAuthenticator(){
publicMyAuthenticator(Stringusername,Stringpassword){
this.userName=username;
this.password=password;
protectedPasswordAuthenticationgetPasswordAuthentication(){
returnnewPasswordAuthentication(userName,password);
下面给出使用上面三个类的代码:
publicstaticvoidmain(String[]args){
//这个类主要是设置邮件
MailSenderInfomailInfo=newMailSenderInfo();
mailInfo.setMailServerHost("
"
mailInfo.setMailServerPort("
mailInfo.setValidate(true);
mailInfo.setUserName("
han2000lei@"
mailInfo.setPassword("
**********"
//您的邮箱密码
mailInfo.setFromAddress("
mailInfo.setToAddress("
mailInfo.setSubject("
设置邮箱标题如http:
//www.guihua.org中国桂花网"
mailInfo.setContent("
设置邮箱内容如http:
//www.guihua.org中国桂花网是中国最大桂花网站=="
//这个类主要来发送邮件
SimpleMailSendersms=newSimpleMailSender();
sms.sendTextMail(mailInfo);
//发送文体格式
sms.sendHtmlMail(mailInfo);
//发送html格式
}
最后,给出朋友们几个注意的地方:
1、使用此代码你可以完成你的javamail的邮件发送功能。
三个类缺一不可。
2、这三个类我打包是用的com.util.mail包,如果不喜欢,你可以自己改,但三个类文件必须在同一个包中
3、不要使用你刚刚注册过的邮箱在程序中发邮件,如果你的163邮箱是刚注册不久,那你就不要使用“”。
因为你发不出去。
刚注册的邮箱是不会给你这种权限的,也就是你不能通过验证。
要使用你经常用的邮箱,而且时间比较长的。
4、另一个问题就是mailInfo.setMailServerHost("
与mailInfo.setFromAddress("
这两句话。
即如果你使用163smtp服务器,那么发送邮件地址就必须用163的邮箱,如果不的话,是不会发送成功的。
5、关于javamail验证错误的问题,网上的解释有很多,但我看见的只有一个。
就是我的第三个类。
你只要复制全了代码,我想是不会有问题的。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- javamail 发送 邮件 简单 实例
![提示](https://static.bingdoc.com/images/bang_tan.gif)