Java发送邮件

本文详细介绍了Java中使用SMTP协议发送邮件的基本方法及带有授权的发送方式,包括不验证的邮件发送与带授权的邮件发送过程,适用于邮件系统的初学者与开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 iLife's 博客http://blog.youkuaiyun.com/fei1502816 




  1. import java.io.UnsupportedEncodingException;  
  2. import java.util.Date;  
  3. import java.util.Properties;  
  4. import javax.mail.Address;  
  5. import javax.mail.Message;  
  6. import javax.mail.MessagingException;  
  7. import javax.mail.Session;  
  8. import javax.mail.Transport;  
  9. import javax.mail.internet.AddressException;  
  10. import javax.mail.internet.InternetAddress;  
  11. import javax.mail.internet.MimeMessage;  
  12. /** 
  13.  *  邮件发送 
  14.  */  
  15. public class SendMail {  
  16.   
  17.  /** 
  18.   * 不需要验证的邮件发送 
  19.   * String fromMail 发件人地址 
  20.   * String password 发件人密码  
  21.   * String toMail 收件人地址 
  22.   * String messageText 发送的消息 
  23.   * String title 发送的标题 
  24.   * String serviceName 使用的邮件服务器 
  25.   * @throws Exception 
  26.   */  
  27.  public static void setMessage(String fromMail, String toMail,  
  28.    String messageText, String serviceName) throws Exception {  
  29.   Properties props = System.getProperties();  
  30.   props.put("mail.smtp.host", serviceName); //设置smtp的服务器地址:该邮件服务器不需要身份验证  
  31.   props.put("mail.smtp.auth""false"); //设置smtp服务器要身份验证:缺省设置为false  
  32.   
  33.   Address from = new InternetAddress(fromMail);  
  34.   Address to = new InternetAddress(toMail);  
  35.   
  36.   Session session = Session.getDefaultInstance(props, null);  
  37.   Message message = new MimeMessage(session);  
  38.   message.setFrom(from);  
  39.   message.addRecipient(Message.RecipientType.TO, to);  
  40.   message.setText(messageText);  
  41.   
  42.   Transport.send(message);  
  43.     
  44.     
  45.   
  46.  }  
  47.    
  48.   /** 
  49.    * 带授权的发送邮件 
  50.    * String fromMail 发件人地址 
  51.    * String password 发件人密码  
  52.    * String toMail 收件人地址 
  53.    * String messageText 发送的消息 
  54.    * String title 发送的标题 
  55.    * String serviceName 使用的邮件服务器 
  56.    * @throws Exception  
  57.    */  
  58.   public static void setMessageWithAuthentica(String fromMail,String password,String toMail,String messageText,String title,String serviceName) throws Exception{  
  59.   Properties props = new Properties();  
  60.   props.put("mail.smtp.host",serviceName);  //设置smtp的服务器地址是smtp.sohu.com  
  61.   props.put("mail.transport.protocol""smtp");  
  62.   props.put("mail.smtp.auth","true");         //设置smtp服务器要身份验证。  
  63.     
  64.   String formMailName=fromMail.split("@")[0];  
  65.   String toMailName=toMail.split("@")[0];  
  66.      
  67.   PopupAuthenticator auth = new PopupAuthenticator(formMailName,fromMail);   
  68.   Session session = Session.getDefaultInstance(props,auth);  
  69.      
  70.   session.setDebug(true);  
  71.   
  72.   // 发送人地址  
  73.   Transport transport = null;  
  74.   try {  
  75.    Address addressFrom = new InternetAddress(fromMail, formMailName);  
  76.    // 收件人地址  
  77.    Address[] addressTo =new Address[]{new InternetAddress(toMail, toMailName)};  
  78.       
  79.    // 抄送地址  
  80.    // Address addressCopy = new InternetAddress("xxx@gmail.com", "xxx密码");  
  81.    Message message = new MimeMessage(session);  
  82.    message.setText(messageText);  
  83.    message.setSubject(title);  
  84.    message.setFrom(addressFrom);  
  85.    message.addRecipients(Message.RecipientType.TO,addressTo);  
  86.    // message.addRecipient(Message.RecipientType.CC,addressCopy);  
  87.    message.saveChanges();  
  88.    // session.setDebug(true);  
  89.    transport = session.getTransport("smtp");  //创建连接  
  90.    transport.connect(serviceName, fromMail, password);//连接服务器  
  91.    transport.sendMessage(message, addressTo); //发送信息  
  92.    transport.close();   //关闭连接    
  93.   } catch (UnsupportedEncodingException e) {  
  94.    e.printStackTrace();  
  95.    Loggers.error("发送邮件错误:SendMail--setMessageWithAuthentica--UnsupportedEncodingException:"+e);  
  96.   } catch (MessagingException e) {  
  97.    e.printStackTrace();  
  98.    Loggers.error("发送邮件错误:SendMail--setMessageWithAuthentica--MessagingException"+e);  
  99.   } finally {  
  100.    if(transport != null & transport.isConnected()){  
  101.     transport.close();  
  102.    }  
  103.   }  
  104.   }  
  105.     
  106.     
  107.   public static void setMsg(String toMail, String title, String content)throws MessagingException {  
  108.   Properties props = new Properties();  
  109.   props.put("mail.smtp.host""smtp.163.com");  
  110.   props.put("mail.smtp.auth""true");  
  111.   Session s = Session.getInstance(props);  
  112.   s.setDebug(true);  
  113.   
  114.   MimeMessage message = new MimeMessage(s);  
  115.   Transport transport = null;  
  116.   try {  
  117.    InternetAddress from = new InternetAddress("xxx@163.com");  
  118.    message.setFrom(from);  
  119.    InternetAddress to = new InternetAddress(toMail);  
  120.    message.setRecipient(Message.RecipientType.TO, to);  
  121.    message.setSubject(title);  
  122.    message.setText(content);  
  123.    message.setSentDate(new Date());  
  124.    message.saveChanges();  
  125.    transport = s.getTransport("smtp");  
  126.    transport.connect("smtp.163.com""xxx""xxx");  
  127.    transport.sendMessage(message, message.getAllRecipients());  
  128.    transport.close();  
  129.   } catch (AddressException e) {  
  130.    e.printStackTrace();  
  131.    Loggers.error("发送邮件错误:SendMail--setMsg--AddressException:" + e);  
  132.   } catch (MessagingException e) {  
  133.    e.printStackTrace();  
  134.    Loggers.error("发送邮件错误:SendMail--setMsg--MessagingException:" + e);  
  135.   } finally {  
  136.    if (transport != null & transport.isConnected()) {  
  137.     transport.close();  
  138.    }  
  139.   }  
  140.  }  
  141.     
  142.   public static void main(String[] args){  
  143.    try {  
  144.     SendMail.setMessageWithAuthentica("您的邮箱""您的密码""对方邮箱""邮件内容""邮件标题""服务器地址");  
  145.   } catch (Exception e) {  
  146.    e.printStackTrace();  
  147.   }  
  148.   }  
  149. }  

  1. import javax.mail.Authenticator;  
  2. import javax.mail.PasswordAuthentication;  
  3.   
  4. public class PopupAuthenticator extends Authenticator {  
  5.     private String uname;//用户名  
  6.   
  7.     private String password;//密码  
  8.   
  9.     //属性方法  
  10.     public String getUname() {  
  11.         return uname;  
  12.     }  
  13.   
  14.     public void setUname(String uname) {  
  15.         this.uname = uname;  
  16.     }  
  17.   
  18.     public String getPassword() {  
  19.         return password;  
  20.     }  
  21.   
  22.     public void setPassword(String password) {  
  23.         this.password = password;  
  24.     }  
  25.   
  26.     //构造方法  
  27.     public PopupAuthenticator() {  
  28.   
  29.     }  
  30.   
  31.     public PopupAuthenticator(String uname, String password) {  
  32.         this.uname = uname;  
  33.         this.password = password;  
  34.     }  
  35.   
  36.     public PasswordAuthentication getPasswordAuthentication() {  
  37.         String username = uname; //邮箱登录帐号  
  38.         String pwd = password; //登录密码  
  39.         return new PasswordAuthentication(username, pwd);  
  40.     }  
  41. }  




  1. import java.io.UnsupportedEncodingException;  
  2. import java.util.Date;  
  3. import java.util.Properties;  
  4. import javax.mail.Address;  
  5. import javax.mail.Message;  
  6. import javax.mail.MessagingException;  
  7. import javax.mail.Session;  
  8. import javax.mail.Transport;  
  9. import javax.mail.internet.AddressException;  
  10. import javax.mail.internet.InternetAddress;  
  11. import javax.mail.internet.MimeMessage;  
  12. /** 
  13.  *  邮件发送 
  14.  */  
  15. public class SendMail {  
  16.   
  17.  /** 
  18.   * 不需要验证的邮件发送 
  19.   * String fromMail 发件人地址 
  20.   * String password 发件人密码  
  21.   * String toMail 收件人地址 
  22.   * String messageText 发送的消息 
  23.   * String title 发送的标题 
  24.   * String serviceName 使用的邮件服务器 
  25.   * @throws Exception 
  26.   */  
  27.  public static void setMessage(String fromMail, String toMail,  
  28.    String messageText, String serviceName) throws Exception {  
  29.   Properties props = System.getProperties();  
  30.   props.put("mail.smtp.host", serviceName); //设置smtp的服务器地址:该邮件服务器不需要身份验证  
  31.   props.put("mail.smtp.auth""false"); //设置smtp服务器要身份验证:缺省设置为false  
  32.   
  33.   Address from = new InternetAddress(fromMail);  
  34.   Address to = new InternetAddress(toMail);  
  35.   
  36.   Session session = Session.getDefaultInstance(props, null);  
  37.   Message message = new MimeMessage(session);  
  38.   message.setFrom(from);  
  39.   message.addRecipient(Message.RecipientType.TO, to);  
  40.   message.setText(messageText);  
  41.   
  42.   Transport.send(message);  
  43.     
  44.     
  45.   
  46.  }  
  47.    
  48.   /** 
  49.    * 带授权的发送邮件 
  50.    * String fromMail 发件人地址 
  51.    * String password 发件人密码  
  52.    * String toMail 收件人地址 
  53.    * String messageText 发送的消息 
  54.    * String title 发送的标题 
  55.    * String serviceName 使用的邮件服务器 
  56.    * @throws Exception  
  57.    */  
  58.   public static void setMessageWithAuthentica(String fromMail,String password,String toMail,String messageText,String title,String serviceName) throws Exception{  
  59.   Properties props = new Properties();  
  60.   props.put("mail.smtp.host",serviceName);  //设置smtp的服务器地址是smtp.sohu.com  
  61.   props.put("mail.transport.protocol""smtp");  
  62.   props.put("mail.smtp.auth","true");         //设置smtp服务器要身份验证。  
  63.     
  64.   String formMailName=fromMail.split("@")[0];  
  65.   String toMailName=toMail.split("@")[0];  
  66.      
  67.   PopupAuthenticator auth = new PopupAuthenticator(formMailName,fromMail);   
  68.   Session session = Session.getDefaultInstance(props,auth);  
  69.      
  70.   session.setDebug(true);  
  71.   
  72.   // 发送人地址  
  73.   Transport transport = null;  
  74.   try {  
  75.    Address addressFrom = new InternetAddress(fromMail, formMailName);  
  76.    // 收件人地址  
  77.    Address[] addressTo =new Address[]{new InternetAddress(toMail, toMailName)};  
  78.       
  79.    // 抄送地址  
  80.    // Address addressCopy = new InternetAddress("xxx@gmail.com", "xxx密码");  
  81.    Message message = new MimeMessage(session);  
  82.    message.setText(messageText);  
  83.    message.setSubject(title);  
  84.    message.setFrom(addressFrom);  
  85.    message.addRecipients(Message.RecipientType.TO,addressTo);  
  86.    // message.addRecipient(Message.RecipientType.CC,addressCopy);  
  87.    message.saveChanges();  
  88.    // session.setDebug(true);  
  89.    transport = session.getTransport("smtp");  //创建连接  
  90.    transport.connect(serviceName, fromMail, password);//连接服务器  
  91.    transport.sendMessage(message, addressTo); //发送信息  
  92.    transport.close();   //关闭连接    
  93.   } catch (UnsupportedEncodingException e) {  
  94.    e.printStackTrace();  
  95.    Loggers.error("发送邮件错误:SendMail--setMessageWithAuthentica--UnsupportedEncodingException:"+e);  
  96.   } catch (MessagingException e) {  
  97.    e.printStackTrace();  
  98.    Loggers.error("发送邮件错误:SendMail--setMessageWithAuthentica--MessagingException"+e);  
  99.   } finally {  
  100.    if(transport != null & transport.isConnected()){  
  101.     transport.close();  
  102.    }  
  103.   }  
  104.   }  
  105.     
  106.     
  107.   public static void setMsg(String toMail, String title, String content)throws MessagingException {  
  108.   Properties props = new Properties();  
  109.   props.put("mail.smtp.host""smtp.163.com");  
  110.   props.put("mail.smtp.auth""true");  
  111.   Session s = Session.getInstance(props);  
  112.   s.setDebug(true);  
  113.   
  114.   MimeMessage message = new MimeMessage(s);  
  115.   Transport transport = null;  
  116.   try {  
  117.    InternetAddress from = new InternetAddress("xxx@163.com");  
  118.    message.setFrom(from);  
  119.    InternetAddress to = new InternetAddress(toMail);  
  120.    message.setRecipient(Message.RecipientType.TO, to);  
  121.    message.setSubject(title);  
  122.    message.setText(content);  
  123.    message.setSentDate(new Date());  
  124.    message.saveChanges();  
  125.    transport = s.getTransport("smtp");  
  126.    transport.connect("smtp.163.com""xxx""xxx");  
  127.    transport.sendMessage(message, message.getAllRecipients());  
  128.    transport.close();  
  129.   } catch (AddressException e) {  
  130.    e.printStackTrace();  
  131.    Loggers.error("发送邮件错误:SendMail--setMsg--AddressException:" + e);  
  132.   } catch (MessagingException e) {  
  133.    e.printStackTrace();  
  134.    Loggers.error("发送邮件错误:SendMail--setMsg--MessagingException:" + e);  
  135.   } finally {  
  136.    if (transport != null & transport.isConnected()) {  
  137.     transport.close();  
  138.    }  
  139.   }  
  140.  }  
  141.     
  142.   public static void main(String[] args){  
  143.    try {  
  144.     SendMail.setMessageWithAuthentica("您的邮箱""您的密码""对方邮箱""邮件内容""邮件标题""服务器地址");  
  145.   } catch (Exception e) {  
  146.    e.printStackTrace();  
  147.   }  
  148.   }  
  149. }  

  1. import javax.mail.Authenticator;  
  2. import javax.mail.PasswordAuthentication;  
  3.   
  4. public class PopupAuthenticator extends Authenticator {  
  5.     private String uname;//用户名  
  6.   
  7.     private String password;//密码  
  8.   
  9.     //属性方法  
  10.     public String getUname() {  
  11.         return uname;  
  12.     }  
  13.   
  14.     public void setUname(String uname) {  
  15.         this.uname = uname;  
  16.     }  
  17.   
  18.     public String getPassword() {  
  19.         return password;  
  20.     }  
  21.   
  22.     public void setPassword(String password) {  
  23.         this.password = password;  
  24.     }  
  25.   
  26.     //构造方法  
  27.     public PopupAuthenticator() {  
  28.   
  29.     }  
  30.   
  31.     public PopupAuthenticator(String uname, String password) {  
  32.         this.uname = uname;  
  33.         this.password = password;  
  34.     }  
  35.   
  36.     public PasswordAuthentication getPasswordAuthentication() {  
  37.         String username = uname; //邮箱登录帐号  
  38.         String pwd = password; //登录密码  
  39.         return new PasswordAuthentication(username, pwd);  
  40.     }  
  41. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值