直接上代码:3个类,两个主要的类,一个测试类:
主类(Mail):
/**
* @Title: Mail.java
* @Package org.service.impl
* @Description: TODO该方法的主要作用:
* @author A18ccms A18ccms_gmail_com
* @date 2017-5-30 下午10:20:35
* @version V1.0
*/
package org.service.impl;import java.util.Date;
import java.util.Properties;import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;/** * * 项目名称:spring_Schop8 * 类名称:Mail * 类描述: 不用spring实现邮件发送* 创建人:Mu Xiongxiong * 创建时间:2017-5-30 下午10:20:35 * 修改人:Mu Xiongxiong * 修改时间:2017-5-30 下午10:20:35 * 修改备注: * @version * */
public class Mail {private String mailServer,from ,to , mailSubject,mailContent,username,password;public Mail(String mailServer, String from, String to, String mailSubject,String mailContent, String username, String password) {this.mailServer = mailServer;this.from = from;this.to = to;this.mailSubject = mailSubject;this.mailContent = mailContent;this.username = username;this.password = password;}public Mail() {username = "tina";password = "123"; mailServer = "mail.com";from = "tina@mail.com";to = "jerry@mail.com";mailSubject = "问好";mailContent = "好久没见,最近可好!";}public void send(){//设置服务器Properties prop = System.getProperties();prop.put("mail.smtp.host", mailServer);prop.put("mail.smtp.auth", "true");//产生Session服务EmailAuthenticator mailauth = new EmailAuthenticator(username, password);Session mailSession = Session.getInstance(prop,(Authenticator)mailauth);//封装message对象Message message = new MimeMessage(mailSession);try {message.setFrom(new InternetAddress(from)); //设置发送人message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); //设置主题message.setSubject(mailSubject); //设置标题message.setContent(mailContent,"text/html;charset=utf-8"); //设置内容message.setSentDate(new Date()); //设置日期//和粗昂就爱你Transport实例Transport tran = mailSession.getTransport("smtp");tran.send(message, message.getAllRecipients());tran.close();} catch (AddressException e) {// TODO 异常执行块!e.printStackTrace();} catch (MessagingException e) {// TODO 异常执行块!e.printStackTrace();} //}}
第二个主类(EmailAuthenticator):
package org.service.impl;import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;import org.hibernate.validator.constraints.Email;public class EmailAuthenticator extends Authenticator {private String username=null;private String userpass = null;public EmailAuthenticator(String username, String userpass) {super();this.username = username;this.userpass = userpass;}public PasswordAuthentication getPasswordAuthentication(){return new PasswordAuthentication(username, userpass);}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getUserpass() {return userpass;}public void setUserpass(String userpass) {this.userpass = userpass;}}
测试类:
/*** * @Title: testMail1* @Description: 该方法的主要作用:java实现底层的邮件发送* @param 设定文件 * @return 返回类型:void * @throws*/@Testpublic void testMail1(){Mail mail = new Mail();mail.send();System.out.println("发送成功");}
源码上完了,再看一下运行的结果吧:
可以看到发送人,发送时间,还有发送内容!已经发送成功!