欢迎来到天天文库
浏览记录
ID:37858982
大小:60.00 KB
页数:7页
时间:2019-06-01
《java邮件发送》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、packageemail;publicclassTest{/***@paramargs*/publicstaticvoidmain(String[]args){//这个类主要是设置邮件MailSenderInfomailInfo=newMailSenderInfo();mailInfo.setMailServerHost("smtp.qq.com");mailInfo.setMailServerPort("25");mailInfo.setValidate(true);mailInfo.setUserName("470907597@qq.com");mailInf
2、o.setPassword("tandong5721.");//您的邮箱密码mailInfo.setFromAddress("470907597@qq.com");mailInfo.setToAddress("arycol@163.com");mailInfo.setSubject("ok");mailInfo.setContent("ok");//这个类主要来发送邮件SimpleMailSendersms=newSimpleMailSender();Booleanf=sms.sendTextMail(mailInfo);//发送文体格式if(f){System.
3、out.println("发送成功");}}}packageemail;importjavax.mail.Authenticator;importjavax.mail.PasswordAuthentication;publicclassMyAuthenticatorextendsAuthenticator{StringuserName=null;Stringpassword=null;publicMyAuthenticator(){}publicMyAuthenticator(Stringusername,Stringpassword){this.userName
4、=username;this.password=password;}protectedPasswordAuthenticationgetPasswordAuthentication(){returnnewPasswordAuthentication(userName,password);}}packageemail;importjava.util.Date;importjava.util.Properties;importjavax.mail.Address;importjavax.mail.BodyPart;importjavax.mail.Message;im
5、portjavax.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;/***简单邮件(不带附件
6、的邮件)发送器*/publicclassSimpleMailSender{/***以文本格式发送邮件**@parammailInfo待发送的邮件的信息*/publicbooleansendTextMail(MailSenderInfomailInfo){//判断是否需要身份认证MyAuthenticatorauthenticator=null;Propertiespro=mailInfo.getProperties();if(mailInfo.isValidate()){//如果需要身份认证,则创建一个密码验证器authenticator=newMyAuthent
7、icator(mailInfo.getUserName(),mailInfo.getPassword());}//根据邮件会话属性和密码验证器构造一个发送邮件的sessionSessionsendMailSession=Session.getDefaultInstance(pro,authenticator);try{//根据session创建一个邮件消息MessagemailMessage=newMimeMessage(sendMailSession);//创建邮件发送者地址Addressfrom=newInternetAddress(mailInfo.getF
8、romAd
此文档下载收益归作者所有