用javamail来发邮件
工程项目中所用源码,转载就注明出处!
说实话,好多东西我也不懂,!!!!!!!!!!
以下是发邮件的主体代码
package com.innet.ESP.common.mail; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.Multipart; 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; public class MailSender { //String ccAddress,String bccAddress, public static void sendEmail(String subject,String content,String fromAddress,String toAddress,String filePath,Date sendTime){ try{ Properties props=new Properties(); props.put("mail.smtp.host", Constant.mailServer); //设置smtp服务器地址 props.put("mail.smtp.port", Constant.mailPort); //设置smtp服务器端口 props.put("mail.smtp.auth", Constant.authenticState); //设置smtp服务器验证状态 // 验证用户 PopupAuthenticator popAuthenticator= new PopupAuthenticator(); popAuthenticator.performCheck(Constant.mailUserName,Constant.mailPassword); Session session=Session.getDefaultInstance(props,popAuthenticator); //获取邮件会话session // 设置邮件信息 MimeMessage msg=new MimeMessage(session); if(sendTime!=null){ //设置发送日期 msg.setSentDate(sendTime); } if(fromAddress!=null&&!fromAddress.equalsIgnoreCase("")){ //设置发件人地址 msg.setFrom(new InternetAddress(fromAddress)); //发件人地址 msg.setSender(new InternetAddress(fromAddress)); } if(subject!=null&&!subject.equalsIgnoreCase("")){ //设置邮件主题 msg.setSubject(subject,"gb2312"); //邮件主题 } msg.setText("测试用werrrrrrrrrrrrrrrrrrtwetwetwe", "UTF-8"); //邮件文本内容 InternetAddress[] toAdressArr=null; InternetAddress[] ccAdressArr=null; InternetAddress[] bccAdressArr=null; if(toAddress!=null&&!toAddress.equalsIgnoreCase("")){ //设置收件人 toAdressArr=new InternetAddress[Constant.addressSize]; toAdressArr[0]=new InternetAddress(toAddress); //可设置多个收件人 // toAdressArr[1]=new InternetAddress("[email protected]"); msg.setRecipients(Message.RecipientType.TO, toAdressArr); //收件人地址(地址集合) } /*if(ccAddress!=null&&!ccAddress.equalsIgnoreCase("")){ //设置抄送人 ccAdressArr=new InternetAddress[Constant.addressSize]; ccAdressArr[0]=new InternetAddress(ccAddress); //可设置多个抄送人 msg.setRecipients(Message.RecipientType.CC, ccAdressArr); //抄送地址(地址集合) } if(bccAddress!=null&&!bccAddress.equalsIgnoreCase("")){ //设置暗送人 bccAdressArr=new InternetAddress[Constant.addressSize]; bccAdressArr[0]=new InternetAddress(bccAddress); //可设置多个暗送人 msg.setRecipients(Message.RecipientType.BCC, bccAdressArr); //暗送地址(地址集合) }*/ // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象 Multipart mp=new MimeMultipart(); if(content!=null&&!content.equalsIgnoreCase("")){ BodyPart bp = new MimeBodyPart(); bp.setContent(content,"text/html;charset=gb2312"); //邮件内容 mp.addBodyPart(bp); } if(filePath!=null&&!filePath.equalsIgnoreCase("")){ //设置邮件附件 BodyPart bp = new MimeBodyPart(); FileDataSource fileds = new FileDataSource(filePath); bp.setDataHandler(new DataHandler(fileds)); bp.setFileName(fileds.getName()); mp.addBodyPart(bp); } msg.setContent(mp); msg.saveChanges(); //保存邮件信息 Transport transport=session.getTransport("smtp"); //获取邮件发送载体 System.out.println("---->准备发送邮件,请稍后........"); transport.connect(Constant.mailServer, Constant.mailUserName, Constant.mailPassword); //获取邮箱连接 transport.sendMessage(msg, msg.getAllRecipients()); //发送邮件 System.out.println("---->邮件发送完毕!........"); transport.close(); }catch(Exception e){ System.out.println("发送邮件失败!"+e.getMessage()); } } }
//这个是用户验证
package com.innet.ESP.common.mail; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; public class PopupAuthenticator extends Authenticator{ public PopupAuthenticator() { } //javamail的用户名秘码验证 public PasswordAuthentication performCheck(String userName, String password) { return new PasswordAuthentication(userName, password); } }
还有就是配置文件,这个是单独写出来,免得换服务邮箱的时候出错
注意这是一个类
public class Constant { public final static String mailServer="smtp.qq.com"; //这里写smtp服务器的地址 此处为QQ的smtp 地址可更改 public final static String mailPort="25"; //这里是端口号的定 义 public final static String mailUserName=""; //你的邮箱地址,用于发送最件比如24344234(用QQ邮箱 发送的时候这里就写你的QQ号) public final static String mailPassword=""; //你邮箱的秘码(用QQ邮箱发送的时候就是你的QQ秘码) public final static String authenticState="true"; //smtp服务器验证状态 public final static int addressSize=1; //不用更改 public final static int delayTime=5000; //不用更改 public static String serverMailName=; //你的邮箱地址比如[email protected] }
然后就是调用了写一个main方法,里边写如下代码
String cc = "你的用户名是:admin</tr>你的秘码是:admin"; String subject="title"; String content="你的用户名是:admin<br>你的秘码是:admin"; String fromAddress=Constant .serverMailName String toAddress="[email protected]";*/ String filePath="d:\\test.txt"; Date sendTime = new Date(); TextSend send = new TextSend()*/ MailSender.sendEmail(subject, content, fromAddress, toAddress, filePath,sendTime);
这样发送一个带附件的邮件进入我的邮箱,
如果出错的话一般就是jar包的问题,上传一个javamail的jar包 ,如果发生错误请及时联系我,
读本人博客代码如有任何疑问或代码不能执行,请速联系本人