java smtp协议_Java采用SMTP协议发送邮件

下载后解压,将mail.jar和activation.jar放到project里的lib文件夹中,为其配置环境变量,或在myEclipse里的"Java Build Path"中将其添加到"Libraries"。

project里有三个类:入口类(TestJavaMail)、邮件信息类(MailBean)和发送邮件类(SendMail)

MailBean.java

None.gifpackagetest3;

None.gif

None.gifimportjava.util.Vector;

None.gif

ExpandedBlockStart.gifpublicclassMailBean{

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.pngprivateString to;//收件人6a9c071a08f1dae2d3e1c512000eef41.pngprivateString from;//发件人6a9c071a08f1dae2d3e1c512000eef41.pngprivateString host;//SMTP主机6a9c071a08f1dae2d3e1c512000eef41.pngprivateString username;//发件人的用户名6a9c071a08f1dae2d3e1c512000eef41.pngprivateString password;//发件人的密码6a9c071a08f1dae2d3e1c512000eef41.pngprivateString subject;//邮件主题6a9c071a08f1dae2d3e1c512000eef41.pngprivateString content;//邮件正文6a9c071a08f1dae2d3e1c512000eef41.pngVectorfile;//多个附件6a9c071a08f1dae2d3e1c512000eef41.pngprivateString filename;//附件的文件名6a9c071a08f1dae2d3e1c512000eef41.pngExpandedSubBlockStart.gifpublicString getTo(){

6a9c071a08f1dae2d3e1c512000eef41.pngreturnto;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicvoidsetTo(String to){

6a9c071a08f1dae2d3e1c512000eef41.pngthis.to=to;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicString getFrom(){

6a9c071a08f1dae2d3e1c512000eef41.pngreturnfrom;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicvoidsetFrom(String from){

6a9c071a08f1dae2d3e1c512000eef41.pngthis.from=from;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicString getHost(){

6a9c071a08f1dae2d3e1c512000eef41.pngreturnhost;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicvoidsetHost(String host){

6a9c071a08f1dae2d3e1c512000eef41.pngthis.host=host;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicString getUsername(){

6a9c071a08f1dae2d3e1c512000eef41.pngreturnusername;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicvoidsetUsername(String username){

6a9c071a08f1dae2d3e1c512000eef41.pngthis.username=username;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicString getPassword(){

6a9c071a08f1dae2d3e1c512000eef41.pngreturnpassword;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicvoidsetPassword(String password){

6a9c071a08f1dae2d3e1c512000eef41.pngthis.password=password;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicString getSubject(){

6a9c071a08f1dae2d3e1c512000eef41.pngreturnsubject;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicvoidsetSubject(String subject){

6a9c071a08f1dae2d3e1c512000eef41.pngthis.subject=subject;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicString getContent(){

6a9c071a08f1dae2d3e1c512000eef41.pngreturncontent;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicvoidsetContent(String content){

6a9c071a08f1dae2d3e1c512000eef41.pngthis.content=content;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicString getFilename(){

6a9c071a08f1dae2d3e1c512000eef41.pngreturnfilename;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicvoidsetFilename(String filename){

6a9c071a08f1dae2d3e1c512000eef41.pngthis.filename=filename;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicVectorgetFile(){

6a9c071a08f1dae2d3e1c512000eef41.pngreturnfile;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png    

ExpandedSubBlockStart.gifpublicvoidattachFile(String fileName){

6a9c071a08f1dae2d3e1c512000eef41.pngif(file==null)

6a9c071a08f1dae2d3e1c512000eef41.png            file=newVector();

6a9c071a08f1dae2d3e1c512000eef41.png        file.addElement(fileName);

ExpandedSubBlockEnd.gif    }ExpandedBlockEnd.gif}None.gif

SendMail.java

None.gifpackagetest3;

None.gif

None.gifimportjava.util.Date;

None.gifimportjava.util.Enumeration;

None.gifimportjava.util.Properties;

None.gifimportjava.util.Vector;

None.gif

None.gifimportjavax.activation.DataHandler;

None.gifimportjavax.activation.FileDataSource;

None.gifimportjavax.mail.Authenticator;

None.gifimportjavax.mail.Message;

None.gifimportjavax.mail.MessagingException;

None.gifimportjavax.mail.Multipart;

None.gifimportjavax.mail.PasswordAuthentication;

None.gifimportjavax.mail.Session;

None.gifimportjavax.mail.Transport;

None.gifimportjavax.mail.internet.InternetAddress;

None.gifimportjavax.mail.internet.MimeBodyPart;

None.gifimportjavax.mail.internet.MimeMessage;

None.gifimportjavax.mail.internet.MimeMultipart;

None.gifimportjavax.mail.internet.MimeUtility;

None.gif

ExpandedBlockStart.gifpublicclassSendMail{

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicString toChinese(String text){

ExpandedSubBlockStart.giftry{

6a9c071a08f1dae2d3e1c512000eef41.png            text=MimeUtility.encodeText(newString(text.getBytes(),"GB2312"),"GB2312","B");

ExpandedSubBlockStart.gif        }catch(Exception e){

6a9c071a08f1dae2d3e1c512000eef41.png            e.printStackTrace();

ExpandedSubBlockEnd.gif        }6a9c071a08f1dae2d3e1c512000eef41.pngreturntext;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicbooleansendMail(MailBean mb){

6a9c071a08f1dae2d3e1c512000eef41.png        String host=mb.getHost();

6a9c071a08f1dae2d3e1c512000eef41.pngfinalString username=mb.getUsername();

6a9c071a08f1dae2d3e1c512000eef41.pngfinalString password=mb.getPassword();

6a9c071a08f1dae2d3e1c512000eef41.png        String from=mb.getFrom();

6a9c071a08f1dae2d3e1c512000eef41.png        String to=mb.getTo();

6a9c071a08f1dae2d3e1c512000eef41.png        String subject=mb.getSubject();

6a9c071a08f1dae2d3e1c512000eef41.png        String content=mb.getContent();

6a9c071a08f1dae2d3e1c512000eef41.png        String fileName=mb.getFilename();

6a9c071a08f1dae2d3e1c512000eef41.png        Vectorfile=mb.getFile();

6a9c071a08f1dae2d3e1c512000eef41.png        

6a9c071a08f1dae2d3e1c512000eef41.png        

6a9c071a08f1dae2d3e1c512000eef41.png        Properties props=System.getProperties();

6a9c071a08f1dae2d3e1c512000eef41.png        props.put("mail.smtp.host", host);//设置SMTP的主机6a9c071a08f1dae2d3e1c512000eef41.pngprops.put("mail.smtp.auth","true");//需要经过验证6a9c071a08f1dae2d3e1c512000eef41.pngExpandedSubBlockStart.gif        Session session=Session.getInstance(props,newAuthenticator(){

ExpandedSubBlockStart.gifpublicPasswordAuthentication getPasswordAuthentication(){

6a9c071a08f1dae2d3e1c512000eef41.pngreturnnewPasswordAuthentication(username, password);

ExpandedSubBlockEnd.gif            }ExpandedSubBlockEnd.gif        });

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.giftry{

6a9c071a08f1dae2d3e1c512000eef41.png            MimeMessage msg=newMimeMessage(session);

6a9c071a08f1dae2d3e1c512000eef41.png            msg.setFrom(newInternetAddress(from));

ExpandedSubBlockStart.gif            InternetAddress[] address={newInternetAddress(to)};

6a9c071a08f1dae2d3e1c512000eef41.png            msg.setRecipients(Message.RecipientType.TO, address);

6a9c071a08f1dae2d3e1c512000eef41.png            msg.setSubject(toChinese(subject));

6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.png            Multipart mp=newMimeMultipart();

6a9c071a08f1dae2d3e1c512000eef41.png            MimeBodyPart mbpContent=newMimeBodyPart();

6a9c071a08f1dae2d3e1c512000eef41.png            mbpContent.setText(content);

6a9c071a08f1dae2d3e1c512000eef41.png            mp.addBodyPart(mbpContent);

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gif/*往邮件中添加附件*/6a9c071a08f1dae2d3e1c512000eef41.png            Enumerationefile=file.elements();

ExpandedSubBlockStart.gifwhile(efile.hasMoreElements()){

6a9c071a08f1dae2d3e1c512000eef41.png                MimeBodyPart mbpFile=newMimeBodyPart();

6a9c071a08f1dae2d3e1c512000eef41.png                fileName=efile.nextElement().toString();

6a9c071a08f1dae2d3e1c512000eef41.png                FileDataSource fds=newFileDataSource(fileName);

6a9c071a08f1dae2d3e1c512000eef41.png                mbpFile.setDataHandler(newDataHandler(fds));

6a9c071a08f1dae2d3e1c512000eef41.png                mbpFile.setFileName(toChinese(fds.getName()));

6a9c071a08f1dae2d3e1c512000eef41.png                mp.addBodyPart(mbpFile);

ExpandedSubBlockEnd.gif            }6a9c071a08f1dae2d3e1c512000eef41.png

6a9c071a08f1dae2d3e1c512000eef41.png            msg.setContent(mp);

6a9c071a08f1dae2d3e1c512000eef41.png            msg.setSentDate(newDate());

6a9c071a08f1dae2d3e1c512000eef41.png            Transport.send(msg);

6a9c071a08f1dae2d3e1c512000eef41.png            

ExpandedSubBlockStart.gif        }catch(MessagingException me){

6a9c071a08f1dae2d3e1c512000eef41.png            me.printStackTrace();

6a9c071a08f1dae2d3e1c512000eef41.pngreturnfalse;

ExpandedSubBlockEnd.gif        }6a9c071a08f1dae2d3e1c512000eef41.pngreturntrue;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedBlockEnd.gif}None.gif

None.gif

TestJavaMail.java

None.gifpackagetest3;

None.gif

ExpandedBlockStart.gifpublicclassTestJavaMail{

6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedSubBlockStart.gifpublicstaticvoidmain(String[] args){

6a9c071a08f1dae2d3e1c512000eef41.png        

6a9c071a08f1dae2d3e1c512000eef41.png        MailBean mb=newMailBean();

6a9c071a08f1dae2d3e1c512000eef41.png        mb.setHost("smtp.163.com");//设置SMTP主机(163),若用126,则设为:smtp.126.com6a9c071a08f1dae2d3e1c512000eef41.pngmb.setUsername("linyiteng1220");//设置发件人邮箱的用户名6a9c071a08f1dae2d3e1c512000eef41.pngmb.setPassword("******");//设置发件人邮箱的密码,需将*号改成正确的密码6a9c071a08f1dae2d3e1c512000eef41.pngmb.setFrom("linyiteng1220@163.com");//设置发件人的邮箱6a9c071a08f1dae2d3e1c512000eef41.pngmb.setTo("linyiteng1220@126.com");//设置收件人的邮箱6a9c071a08f1dae2d3e1c512000eef41.pngmb.setSubject("测试_JavaMail");//设置邮件的主题6a9c071a08f1dae2d3e1c512000eef41.pngmb.setContent("本邮件中包含三个附件,请检查!");//设置邮件的正文6a9c071a08f1dae2d3e1c512000eef41.png6a9c071a08f1dae2d3e1c512000eef41.png        mb.attachFile("E:\工作报告(林乙腾).doc");//往邮件中添加附件6a9c071a08f1dae2d3e1c512000eef41.pngmb.attachFile("E:\test.txt");

6a9c071a08f1dae2d3e1c512000eef41.png        mb.attachFile("E:\test.xls");

6a9c071a08f1dae2d3e1c512000eef41.png        

6a9c071a08f1dae2d3e1c512000eef41.png        SendMail sm=newSendMail();

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("正在发送邮件...");

6a9c071a08f1dae2d3e1c512000eef41.png        

6a9c071a08f1dae2d3e1c512000eef41.pngif(sm.sendMail(mb))//发送邮件6a9c071a08f1dae2d3e1c512000eef41.pngSystem.out.println("发送成功!");

6a9c071a08f1dae2d3e1c512000eef41.pngelse6a9c071a08f1dae2d3e1c512000eef41.png            System.out.println("发送失败!");

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png    

ExpandedBlockEnd.gif}None.gif

如果你用myEclipse进行开发的话,运行时可能会出现以下的错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream

解决方法:移除myEclipse自带的EE包

err.JPG

但你可能还需要用到EE包里的servlet相关类,那可以从Tomcat的lib文件夹中复制servlet-api.jar到project中,用它来代替EE包,一切OK!

另外,个人测试时发现无法通过新注册的网易邮箱来发邮件,以前旧的就没有问题,不知道是什么原因。

如果要通过gmail发邮件的话,请在代码Properties props = System.getProperties();后再添加以下两行代码:

props.put("mail.transport.protocol", "smtp");

props.put("mail.smtp.starttls.enable","true");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值