java发送邮件 实例Word文档下载推荐.docx
《java发送邮件 实例Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《java发送邮件 实例Word文档下载推荐.docx(9页珍藏版)》请在冰豆网上搜索。
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;
}
MyAuthenticator.java
importjavax.mail.*;
publicclassMyAuthenticatorextendsAuthenticator{
StringuserName=null;
Stringpassword=null;
publicMyAuthenticator(){
publicMyAuthenticator(Stringusername,Stringpassword){
this.userName=username;
protectedPasswordAuthenticationgetPasswordAuthentication(){
returnnewPasswordAuthentication(userName,password);
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){
//判断是否需要身份认证
//如果需要身份认证,则创建一个密码验证器
if(mailInfo.isValidate()){
//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);
test.java
publicclasstest{
/**
*@paramargs
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
//这个类主要是设置邮件
MailSenderInfomailInfo=newMailSenderInfo();
mailInfo.setMailServerHost("
"
mailInfo.setMailServerPort("
mailInfo.setValidate(true);
mailInfo.setUserName("
yanyuanyuan101@"
mailInfo.setPassword("
100982"
//您的邮箱密码
mailInfo.setFromAddress("
//mailInfo.setToAddress("
315905059@"
mailInfo.setToAddress("
msn.leonard@"
mailInfo.setSubject("
设置邮箱标题如http:
//www.guihua.org中国桂花网"
mailInfo.setContent("
设置邮箱内容如http:
//www.guihua.org中国桂花网是中国最大桂花网站=="
//这个类主要来发送邮件
SimpleMailSendersms=newSimpleMailSender();
sms.sendTextMail(mailInfo);
//发送文体格式
sms.sendHtmlMail(mailInfo);
//发送html格式
}
绿色通道:
好文要顶关注我收藏该文与我联系
postedon2010-08-1915:
40leonard2010阅读(37)评论(0)编辑收藏