作为一个屌丝程序员不得不收藏的工具类 一 邮件验证类

来源:互联网 发布:拍a发b淘宝会被降权吗 编辑:程序博客网 时间:2024/04/30 12:53
import java.io.File;import java.io.UnsupportedEncodingException;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Authenticator;import javax.mail.BodyPart;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;import javax.mail.internet.MimeUtility;public class SendMial2 {public static void main(String[] args) {try{//发件人邮箱String userName="xxxxxxxx@qq.com";//发件人密码String password="xxxxxxx";//smtp服务器地址String smtp_server="smtp.qq.com";String from_mail_address=userName;//收件人邮箱String to_mail_address="xxxxxxxx@qq.com";//邮件标题String EmailTitle="Mail Test";//邮件体别名String bodyText="验证邮件";//附件路径String attachmentPath="C:\\Users\\Administrator\\Desktop\\mail.jar";//文字内容String contentText="<a href='http://www.baidu.com'>我是链接</a>";//附件图片路径String attachmentPicPath="C:\\Users\\Administrator\\Desktop\\大法师法师法.jpg";//身份验证Authenticator auth=new PopupAuthenticator(userName,password);Properties mailProps=new Properties();//设置smtp服务器地址mailProps.put("mail.smtp.host", smtp_server);//使用smtp身份验证mailProps.put("mail.smtp.auth", "true");//用户名mailProps.put("username", userName);//密码mailProps.put("password", password);//登录邮箱Session mailSession=Session.getDefaultInstance(mailProps, auth);//打印日志//mailSession.setDebug(true);MimeMessage message=new MimeMessage(mailSession);//设置发件人邮箱message.setFrom(new InternetAddress(from_mail_address));//设置收件人邮箱  RecipientType:cc表示抄送   bcc 表示暗送message.setRecipient(Message.RecipientType.TO, new InternetAddress(to_mail_address));//设置邮件标题message.setSubject(EmailTitle);//邮件体MimeMultipart multi=new MimeMultipart();BodyPart textBodyPart=new MimeBodyPart();//邮件体别名textBodyPart.setText(bodyText);//创建附件MimeBodyPart bodyPartAttch = createAttachMent(attachmentPath);//创建邮件的正文MimeBodyPart bodyPartContentAndPic = createContentAndPic(contentText,attachmentPicPath);//文本内容//添加附件multi.addBodyPart(bodyPartAttch);//添加文本内容multi.addBodyPart(bodyPartContentAndPic);//添加邮件体multi.addBodyPart(textBodyPart);//设置正文与附件之间的关系 相关的:related 混合:mixed 替代:alternativemulti.setSubType("mixed");//设置邮件的邮件体message.setContent(multi);//保存修改message.saveChanges();//发送邮件Transport.send(message);System.out.println("发送成功");}catch(Exception ex){System.err.println("邮件发送失败的原因是:"+ex.getMessage());System.err.print("具体的错误原因:");ex.printStackTrace(System.err);}}//创建附件public static MimeBodyPart createAttachMent(String path) throws MessagingException{MimeBodyPart mimeBodyPart = new MimeBodyPart();FileDataSource dataSource = new FileDataSource( new File(path));mimeBodyPart.setDataHandler(new DataHandler(dataSource));mimeBodyPart.setFileName(dataSource.getName());return mimeBodyPart;}//创建文本和图片public static MimeBodyPart createContentAndPic(String content,String path) throws MessagingException{MimeMultipart mimeMutiPart = new MimeMultipart("related");//图片MimeBodyPart picBodyPart = new MimeBodyPart();FileDataSource fileDataSource = new FileDataSource( new File(path));picBodyPart.setDataHandler(new DataHandler(fileDataSource));//picBodyPart.setFileName(fileDataSource.getName());try {//解决中文乱码问题picBodyPart.setFileName(MimeUtility.encodeText(fileDataSource.getName()));} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}picBodyPart.setHeader("Content-Location", path);mimeMutiPart.addBodyPart(picBodyPart);//文本MimeBodyPart contentBodyPart = new MimeBodyPart();//img的src要和setHeader中设置的值一样// setContent(“邮件的正文内容”,”设置邮件内容的编码方式”)contentBodyPart.setContent(content+"<a href='http://www.baidu.com'><img src='http://www.baidu.com/img/bdlogo.png'/></a>","text/html;charset=gbk");mimeMutiPart.addBodyPart(contentBodyPart);//图片和文本结合MimeBodyPart allBodyPart = new MimeBodyPart();allBodyPart.setContent(mimeMutiPart);return allBodyPart;}}class PopupAuthenticator extends Authenticator{private String username;private String password;public PopupAuthenticator(String username,String pwd){this.username=username;this.password=pwd;} /**     * 在 JavaMail 中,可以通过 extends Authenticator 抽象类,在子类中覆盖父类中的 getPasswordAuthentication()      * 方法,就可以实现以不同的方式来进行登录邮箱时的用户身份认证。JavaMail 中的这种设计是使用了策略模式(Strategy     * */public PasswordAuthentication getPasswordAuthentication(){return new PasswordAuthentication(this.username,this.password);}}
懒得抽方法了,有需要的同学自己抽吧

0 0
原创粉丝点击