Java发送邮件javax.mail的实现方法
直接上源码:
1.邮件的基本信息(方便就直接设置为静态了)
下面最好是做为一个javabean
packagecom.lgf.Email; importjava.util.Date; publicclassEmailMessage { /** *邮件的信息,可以自己进行设置。 *为了方便直接全部设置为静态 */ //收件人邮箱 publicstaticStringTO="xxx@xxx.com"; //发件人邮箱 publicstaticStringFROM="xxx@xxx.com"; publicstaticStringFROM_NAME="xxx"; //抄送人 publicstaticStringCC="xxx@nxxx.com"; //暗送人 publicstaticStringBCC="xxx@nxxx.com"; //邮件的类型 publicstaticStringEmail_Content="text/plain"; //邮件标题 publicstaticStringEmail_Subject="TestEmailWithJavaMail"; //邮件头 publicstaticStringEmail_Header="ThisIsEmailHeader"; //内容 publicstaticStringEmail_Body="<ahref=\"http://www.baidu.com\">ThisIsEmailBody</a>"; //服务器比如QQ的可以设置为smtp.qq.com publicstaticStringEmail_Host="smtp.xxx.com"; //发送时间 publicstaticDatesendDate=newDate(); //是否需要验证用户名和密码 publicstaticbooleanvalidate=true; }
2.服务器验证(就是一般登入时候的用户名和密码)
packagecom.lgf.Email; importjavax.mail.Authenticator; importjavax.mail.PasswordAuthentication; /** *用户名和密码的验证 *@authorlgf * */ publicclassMailAuthenticatorextendsAuthenticator{ privateStringusername="xxxxx"; privateStringpassword="xxxxx"; publicMailAuthenticator(){ super(); } /** *设置验证的用户名和密码 */ publicMailAuthenticator(StringuserName,Stringpassword){ super(); this.username=userName; this.password=password; } protectedPasswordAuthenticationgetPasswordAuthentication() { returnnewPasswordAuthentication(this.username,this.password); } }
3.发送邮件前设置信息和步骤
packagecom.lgf.Email; importjava.util.Properties; importjavax.activation.DataHandler; importjavax.activation.DataSource; importjavax.activation.FileDataSource; importjavax.mail.Address; importjavax.mail.Authenticator; importjavax.mail.BodyPart; importjavax.mail.Message; 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; importcom.lgf.SendEmail.MailAuthenticator; publicclassSendEmail { /** *发送普通邮件 *@throwsException */ publicvoiddoSendNormalMail() { //获取系统环境 Propertiesprop=newProperties(); Authenticatorauth=null; //判断发送邮件是否需要验证 if(EmailMessage.validate) { //邮件服务器认证用户名和密码 auth=newMailAuthenticator(); } //添加必要的信息 prop.put("mail.smtp.host",EmailMessage.Email_Host); prop.put("mail.smtp.auth","true"); //设置对话和邮件服务器进行通讯 Sessionsession=Session.getDefaultInstance(prop,auth); //在控制台显示Debug信息 session.setDebug(true); //设置邮件对象 Messagemessage=newMimeMessage(session); try { //设置邮件主题 message.setSubject(EmailMessage.Email_Subject); //设置邮件标题 message.setHeader("Header",EmailMessage.Email_Header); //设置发送时间 message.setSentDate(EmailMessage.sendDate); //设置发信人地址和名字 Addressaddress=newInternetAddress(EmailMessage.FROM,EmailMessage.FROM_NAME); //把发件人信息添加到信息中 message.setFrom(address); //设置发件人地址 AddresstoAddress=newInternetAddress(EmailMessage.TO); //设置接收人地址 message.setRecipient(Message.RecipientType.TO,toAddress); //设置多个收件人地址 //message.addRecipient(Message.RecipientType.TO,newInternetAddress("xxx@xxx.com")); //设置邮件格式 message.setContent("Content",EmailMessage.Email_Content); //设置邮件内容必须在设置文件格式后面 message.setText(EmailMessage.Email_Body); //保存上面添加的信息 message.saveChanges(); //发送邮件 System.out.println("sendNormalEmail()开始发送邮件……"); Transport.send(message); System.out.println("发送成功!"); }catch(Exceptione) { System.out.println("出错"); e.printStackTrace(); } } /** *发送HTML格式的邮件 */ publicvoiddoSendHtmlMail() { //获取系统环境 Propertiesprop=newProperties(); Authenticatorauth=null; if(EmailMessage.validate) { //邮件服务器认证用户名和密码 auth=newMailAuthenticator(); } //添加必要的信息 prop.put("mail.smtp.host",EmailMessage.Email_Host); prop.put("mail.smtp.auth","true"); //设置对话和邮件服务器进行通讯 Sessionsession=Session.getDefaultInstance(prop,auth); //设置邮件对象 Messagemessage=newMimeMessage(session); try { //设置邮件主题 message.setSubject(EmailMessage.Email_Subject); //设置邮件标题 message.setHeader("Header",EmailMessage.Email_Header); //设置发送时间 message.setSentDate(EmailMessage.sendDate); //设置发信人地址和名字 Addressaddress=newInternetAddress(EmailMessage.FROM,EmailMessage.FROM_NAME); //把发件人信息添加到信息中 message.setFrom(address); //设置发件人地址 AddresstoAddress=newInternetAddress(EmailMessage.TO); //设置接收人地址 message.setRecipient(Message.RecipientType.TO,toAddress); //设置多个收件人地址 //message.addRecipient(Message.RecipientType.TO,newInternetAddress("xxx@xxx.com")); //设置发送信息的内容下面为发送hmml //设置邮件格式 EmailMessage.Email_Content="text/html;charset=utf-8"; message.setContent(EmailMessage.Email_Body,EmailMessage.Email_Content); //保存上面添加的信息 message.saveChanges(); //发送邮件 System.out.println("doSendHtmlMail()开始发送邮件……"); Transport.send(message); System.out.println("发送成功!"); }catch(Exceptione) { System.out.println("出错"); e.printStackTrace(); } } /** *发送带有附件格式的邮件 */ publicvoiddoSendAttachmentMail() { //获取系统环境 Propertiesprop=newProperties(); Authenticatorauth=null; if(EmailMessage.validate) { //邮件服务器认证用户名和密码 auth=newMailAuthenticator(); } //添加必要的信息 prop.put("mail.smtp.host",EmailMessage.Email_Host); prop.put("mail.smtp.auth","true"); //设置对话和邮件服务器进行通讯 Sessionsession=Session.getDefaultInstance(prop,auth); //设置邮件对象 Messagemessage=newMimeMessage(session); try { //设置邮件主题 message.setSubject(EmailMessage.Email_Subject); //设置邮件标题 message.setHeader("Header",EmailMessage.Email_Header); //设置发送时间 message.setSentDate(EmailMessage.sendDate); //设置发信人地址和名字 Addressaddress=newInternetAddress(EmailMessage.FROM,EmailMessage.FROM_NAME); //把发件人信息添加到信息中 message.setFrom(address); //设置发件人地址 AddresstoAddress=newInternetAddress(EmailMessage.TO); //设置接收人地址 message.setRecipient(Message.RecipientType.TO,toAddress); //设置多个收件人地址 //message.addRecipient(Message.RecipientType.TO,newInternetAddress("xxx@xxx.com")); //设置发送信息的内容下面为发送附件 message.setContent(EmailMessage.Email_Body,EmailMessage.Email_Content); BodyPartmessageBodyPart=newMimeBodyPart(); messageBodyPart.setText("bodypart"); Multipartmultipart=newMimeMultipart(); multipart.addBodyPart(messageBodyPart); messageBodyPart=newMimeBodyPart(); //设置上传的资源 DataSourcesource=newFileDataSource("E:\\3.doc"); //添加到 messageBodyPart.setDataHandler(newDataHandler(source)); //设置文件名称,记得后缀名 messageBodyPart.setFileName("test.doc"); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); //保存上面添加的信息 message.saveChanges(); //发送邮件 System.out.println("doSendAttachmentMail()开始发送邮件……"); Transport.send(message); System.out.println("发送成功!"); }catch(Exceptione) { System.out.println("出错"); e.printStackTrace(); } } /** *@paramargs */ publicstaticvoidmain(String[]args) { //newSendEmail().doSendNormalMail(); //newSendEmail().doSendHtmlMail(); newSendEmail().doSendAttachmentMail(); } }
以上就是Java发送邮件javax.mail的详细代码,希望对大家实现java邮件发送有所帮助。