`
- 浏览:
95984 次
- 性别:
- 来自:
北京
-
-
packagemail;
-
-
importjava.util.*;
-
importjava.io.*;
-
importjavax.mail.*;
-
importjavax.mail.internet.*;
-
importjavax.activation.*;
-
publicclassMail{
-
-
privateStringdisplayName;
-
privateStringto;
-
privateStringfrom;
-
privateStringsmtpServer;
-
privateStringusername;
-
privateStringpassword;
-
privateStringsubject;
-
privateStringcontent;
-
privatebooleanifAuth;
-
privateStringfilename="";
-
privateVectorfile=newVector();
-
-
-
-
-
-
publicvoidsetSmtpServer(StringsmtpServer){
-
this.smtpServer=smtpServer;
-
}
-
-
-
-
-
publicvoidsetFrom(Stringfrom){
-
this.from=from;
-
}
-
-
-
-
publicvoidsetDisplayName(StringdisplayName){
-
this.displayName=displayName;
-
}
-
-
-
-
-
publicvoidsetIfAuth(booleanifAuth){
-
this.ifAuth=ifAuth;
-
}
-
-
-
-
-
publicvoidsetUserName(Stringusername){
-
this.username=username;
-
}
-
-
-
-
-
publicvoidsetPassword(Stringpassword){
-
this.password=password;
-
}
-
-
-
-
-
publicvoidsetTo(Stringto){
-
this.to=to;
-
}
-
-
-
-
-
publicvoidsetSubject(Stringsubject){
-
this.subject=subject;
-
}
-
-
-
-
-
publicvoidsetContent(Stringcontent){
-
this.content=content;
-
}
-
-
-
-
-
publicvoidaddAttachfile(Stringfname){
-
file.addElement(fname);
-
}
-
-
publicMail(){
-
-
}
-
-
-
-
-
publicMail(StringsmtpServer,Stringfrom,StringdisplayName,Stringusername,Stringpassword,Stringto,Stringsubject,Stringcontent){
-
this.smtpServer=smtpServer;
-
this.from=from;
-
this.displayName=displayName;
-
this.ifAuth=true;
-
this.username=username;
-
this.password=password;
-
this.to=to;
-
this.subject=subject;
-
this.content=content;
-
}
-
-
-
-
-
publicMail(StringsmtpServer,Stringfrom,StringdisplayName,Stringto,Stringsubject,Stringcontent){
-
this.smtpServer=smtpServer;
-
this.from=from;
-
this.displayName=displayName;
-
this.ifAuth=false;
-
this.to=to;
-
this.subject=subject;
-
this.content=content;
-
}
-
-
-
-
-
publicHashMapsend(){
-
HashMapmap=newHashMap();
-
map.put("state","success");
-
Stringmessage="邮件发送成功!";
-
Sessionsession=null;
-
Propertiesprops=System.getProperties();
-
props.put("mail.smtp.host",smtpServer);
-
if(ifAuth){
-
props.put("mail.smtp.auth","true");
-
SmtpAuthsmtpAuth=newSmtpAuth(username,password);
-
session=Session.getDefaultInstance(props,smtpAuth);
-
}else{
-
props.put("mail.smtp.auth","false");
-
session=Session.getDefaultInstance(props,null);
-
}
-
session.setDebug(true);
-
Transporttrans=null;
-
try{
-
Messagemsg=newMimeMessage(session);
-
try{
-
Addressfrom_address=newInternetAddress(from,displayName);
-
msg.setFrom(from_address);
-
}catch(java.io.UnsupportedEncodingExceptione){
-
e.printStackTrace();
-
}
-
InternetAddress[]address={newInternetAddress(to)};
-
msg.setRecipients(Message.RecipientType.TO,address);
-
msg.setSubject(subject);
-
Multipartmp=newMimeMultipart();
-
MimeBodyPartmbp=newMimeBodyPart();
-
mbp.setContent(content.toString(),"text/html;charset=gb2312");
-
mp.addBodyPart(mbp);
-
if(!file.isEmpty()){
-
Enumerationefile=file.elements();
-
while(efile.hasMoreElements()){
-
mbp=newMimeBodyPart();
-
filename=efile.nextElement().toString();
-
FileDataSourcefds=newFileDataSource(filename);
-
mbp.setDataHandler(newDataHandler(fds));
-
mbp.setFileName(fds.getName());
-
mp.addBodyPart(mbp);
-
}
-
file.removeAllElements();
-
}
-
msg.setContent(mp);
-
msg.setSentDate(newDate());
-
-
msg.saveChanges();
-
trans=session.getTransport("smtp");
-
trans.connect(smtpServer,username,password);
-
trans.sendMessage(msg,msg.getAllRecipients());
-
trans.close();
-
-
}catch(AuthenticationFailedExceptione){
-
map.put("state","failed");
-
message="邮件发送失败!错误原因:/n"+"身份验证错误!";
-
e.printStackTrace();
-
}catch(MessagingExceptione){
-
message="邮件发送失败!错误原因:/n"+e.getMessage();
-
map.put("state","failed");
-
e.printStackTrace();
-
Exceptionex=null;
-
if((ex=e.getNextException())!=null){
-
System.out.println(ex.toString());
-
ex.printStackTrace();
-
}
-
}
-
-
map.put("message",message);
-
returnmap;
-
}
-
-
}
SmtpAuth.java 代码
-
packagemail;
-
-
publicclassSmtpAuthextendsjavax.mail.Authenticator{
-
privateStringusername,password;
-
-
publicSmtpAuth(Stringusername,Stringpassword){
-
this.username=username;
-
this.password=password;
-
}
-
protectedjavax.mail.PasswordAuthenticationgetPasswordAuthentication(){
-
returnnewjavax.mail.PasswordAuthentication(username,password);
-
}
-
}
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
里面详细的介绍了JavaMail发邮件(包括单个附件和多个附件)的机制,还有JavaMail正常使用所需要的JAR包,源程序例子经过本人详细且严格的测试,大家下载下来就可以直接使用啦。
06 使用JavaMail发送带附件的邮件.exe06 使用JavaMail发送带附件的邮件.exe
介绍了如何利用javamail实现带附件的邮件发送,附有代码
该资源是一个javamail发送邮件的例子,里面集成了别人的文件发送的功能,单次发送邮件只能上传一个附件。是一个测试方法,拿过来改一改邮件的地址和密码就可以使用了
javaMail发送邮件 发送成功 收不到邮件 或收到邮件无主题无收件人乱码 查看debug 邮件处理过程发现邮件头没有编译, 内容是base64编码,所以邮箱收不到或者收到的是base64编码后的一堆乱码,
JavaMail邮件发送(带附件)
主要介绍了java中javamail发送带附件的邮件实现方法,较为详细的分析了JavaMail发送邮件的用法,是非常实用的技巧,需要的朋友可以参考下
此源码是使用javamail发送邮件,可以实现静默发送邮件,可以发送附件资源等。有相应的使用说明,快捷好用
配置好用户名密码以及SMTP服务器就可以直接运行。已包含相关jar
JavaMail发送和接受邮件的一个小例子, 含文本,图片, 附件等复杂格式
附件是所需要的包文件 博文链接:https://xagcdx.iteye.com/blog/1039425
javaMail 用附件路径,文件流 byte[] 或 base64 字符串 实现发送附件功能
利用JavaMail发送邮件的实例,可以直接运行
javamail 自动发送邮件 发送html格式邮件 发送带有附件的邮件
基于javamail1.4实现邮件发送,支持多收件人、抄送人、多附件等
javamail 可带附件demo源码,其中包含所需要的JAR包(位置:jars文件夹内)
本人最新写了一个邮件收发的例子,可以处理中文的 包括发送附件,附件标题也可以是中文的,可以给大 家参考下!希望能给大家一些帮助!
本资源是一个集合发送图片,附件,内容,HTML等得JAVAMAIL代码,并且可以同时发送多个附件多个图片,只需填充VO类,即可直接调用并发送邮件,例子已经写好,只要填充上必要值即可,经163,qq,sina测试没有问题,有...
javamail发送邮件,带附件。发邮件可以看看。
用javaMail实现公司内网邮箱邮件发送,包括邮件附件,邮件发送人和抄送人