最近在研究一个给注册用户发送邮件激活账户的小功能
用到了一个邮件发送功能,由于项目使用ssh,所以就想到用spring和javamail来实现
下面我直接贴上主要代码
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper;
public class emailsender { private JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); private Properties javaMailProperties = new Properties(); private MimeMessageHelper help; private MimeMessage message; private static Map map= new HashMap(); //创建会话对象 static{ Properties prop = new Properties(); try{ System.out.println("sasas"); //读取属性文件a.properties //InputStream in = new BufferedInputStream (new FileInputStream("src/email.properties")); 应用程序指定 InputStream in = new BufferedInputStream (new FileInputStream(new File("").getAbsolutePath()+"\\workspace\\Enterprise_message_board\\build\\classes\\email.properties")); //加载属性列表 prop.load(in); //map = prop; Iterator<String> it=prop.stringPropertyNames().iterator(); while(it.hasNext()){ String key=it.next(); map.put(key,prop.getProperty(key)); System.out.println(key+":"+map.get(key)); } in.close(); // //这里随便记下 保存(写入)属性到b.properties文件 // FileOutputStream fos = new FileOutputStream("b.properties", true);//true表示追加打开 // prop.setProperty("phone", "10086"); // prop.store(fos, "The New properties file"); // fos.close(); }catch(Exception e){ System.out.println(e); } } //初始化发送信息以及服务器参数 public emailsender(){ System.out.println("这里标记1"); this.mailSender.setHost((String) map.get("host")); this.mailSender.setPort((int)(Integer.parseInt((String) map.get("port")))); this.mailSender.setUsername((String) map.get("username")); this.mailSender.setPassword((String) map.get("password")); System.out.println(map.get("mail.smtp.auth")); this.javaMailProperties.put("mail.smtp.auth", map.get("mail.smtp.auth")); this.javaMailProperties.put("mail.smtp.starttls.enable",map.get("mail.smtp.starttls.enable")); //将参数properties,加入到初始化信息中 this.mailSender.setJavaMailProperties(this.javaMailProperties); //创建会话对象 this.message = this.mailSender.createMimeMessage(); try { this.help = new MimeMessageHelper(this.message, true, "UTF-8"); this.help.setFrom((String)map.get("fromer")); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } //设置发送者邮箱 } public void senderemail(String content,String[] email){ //需要发送的邮箱号码数据 try { //发送给谁 this.help.setTo(email); //发送的主题 this.help.setSubject("测试java的邮件系统"); //发送的主题 this.help.setText(content, true); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("正在发送邮件,请稍后!。。。。。。"); mailSender.send(message); System.out.println("邮件发送成功"); } }其中需要说明的是,配置文件的路劲,大家可以根据自己需求修改吧。
还有就是发送的email的地址是数组,即可以实现批量发送邮件
而邮件的内容可以是html代码哦
测试代码,我也帖以一下吧:
public static void main(String[] args) throws MessagingException { emailsender es = new emailsender(); String[] email={"XXX@qq.com","XXX@qq.com","XXX@qq.com"}; es.senderemail("测试批量发送邮件哦<br><a href='https://www.bugkong.com/'>028888.net</a>", email); }
配置文件email的内容如下:
host = smtp.qq.com port = 25 username = XXXX@028888.net password = 密码 mail.smtp.auth = true 需要验证 mail.smtp.starttls.enable = true fromer = XXXX@028888.net
再次说明下,在ssh框架中,主要的jar包请直接放在lib目录下。
如果是本地java application测试,则可以放在lib下的其他文件夹下,或其他文件夹下。
本实例用到主要jar包括spring的一个jar包,这个jar通常已经被你导入了
javamail的6个jar包
所有的7个jar包您可以点击这里下载 1snu
爆款云服务器s6 2核4G 低至0.46/天,具体规则查看活动详情