zoukankan      html  css  js  c++  java
  • Java实现邮件代理发送

    使用java实现邮件发送

    首先需要添加jar文件
    mailapi.jar
    stmp.jar

    1
    import java.util.Properties; 2 3 import javax.mail.Address; 4 import javax.mail.Message; 5 import javax.mail.MessagingException; 6 import javax.mail.Session; 7 import javax.mail.Transport; 8 import javax.mail.internet.AddressException; 9 import javax.mail.internet.InternetAddress; 10 import javax.mail.internet.MimeMessage; 11 12 /** 13 * 邮件发送代理线程 14 * @author helingfeng 15 * 16 */ 17 public class EmailAgent implements Runnable{ 18 19 private String username; 20 private String title; 21 private String content; 22 23 public EmailAgent(){ 24 // 25 } 26 public EmailAgent(String email,String title,String content){ 27 this.username = email; 28 this.title = title; 29 this.content = content; 30 } 31 32 public String getUsername() { 33 return username; 34 } 35 36 public void setUsername(String username) { 37 this.username = username; 38 } 39 40 public String getTitle() { 41 return title; 42 } 43 44 public void setTitle(String title) { 45 this.title = title; 46 } 47 48 public String getContent() { 49 return content; 50 } 51 52 public void setContent(String content) { 53 this.content = content; 54 } 55 public void run() { 56 Properties props = new Properties(); 57 //配置协议 58 props.setProperty("mail.smtp.auth", "true"); 59 props.setProperty("mail.transport.protocol","smtp"); 60 61 //创建会话 62 Session session = Session.getInstance(props); 63 session.setDebug(true); 64 65 Message message = null; 66 Transport transport = null; 67 68 try { 69 //创建信息对象 70 message = new MimeMessage(session); 71 message.setSubject(title); 72 message.setText(content); 73 //参数(用户名) 74 message.setFrom(new InternetAddress("857190327@qq.com")); 75 //创建传输对象 76 transport = session.getTransport(); 77 //参数 (STMP服务器、端口、用户名、密码) 78 transport.connect("smtp.qq.com", 587, "857190327@qq.com", "密码"); 79 transport.sendMessage(message, new Address[]{new InternetAddress(username)}); 80 } catch (AddressException e) { 81 e.printStackTrace(); 82 } catch (MessagingException e) { 83 e.printStackTrace(); 84 }finally{ 85 try{ 86 if(transport.isConnected()){ 87 transport.close(); 88 transport = null; 89 } 90 }catch(MessagingException e1){ 91 e1.printStackTrace(); 92 } 93 } 94 } 95 }
  • 相关阅读:
    Spring MVC 支持 RESTful 风格编程
    SpringMVC 目标方法返回 json 格式数据
    SpringMVC 文件上传
    使用Eclipse 创建 Maven 项目
    SpringMVC 环境搭建
    SpringMVC运行原理
    Linux(centos)下SVN服务器的搭建及简单配置和使用
    Linux 后台执行脚本命令
    C++入门教程,C++基础教程 更新中...
    Mac开发之HID通讯开发
  • 原文地址:https://www.cnblogs.com/helingfeng/p/4460467.html
Copyright © 2011-2022 走看看