一段发送邮件的代码

  1. MailLet mail = new MailLet();   
  2.   mail.setSmtpServer("yyyyyyyyyyyyyyyyy");   
  3.   mail.setPort(80);   
  4.   mail.setIfAuth(true);   
  5.   mail.setFrom("xxxxxxx");   
  6.   mail.setDisplayName("yyyyyyyyyyyyyyy");   
  7.   mail.setUserName("xxxxxxxxxx");   
  8.   mail.setPassword("xxxxxxxxxxxxxxxxxxxx");   
  9.   mail.setTo(mailTo);   
  10.   mail.setSubject(template.getSubject());   
  11.   mail.setContent(template.getContent());   
  12.   mail.setContentType("text/plain");   
  13.   mail.setCharset(CHARSET);   
  14.   mail.send();  
  1. package mail;   
  2. import java.util.Date;   
  3. import java.util.Enumeration;   
  4. import java.util.HashMap;   
  5. import java.util.Properties;   
  6. import java.util.Vector;   
  7. import javax.activation.FileDataSource;   
  8. import javax.mail.Address;   
  9. import javax.mail.AuthenticationFailedException;   
  10. import javax.mail.Message;   
  11. import javax.mail.MessagingException;   
  12. import javax.mail.Multipart;   
  13. import javax.mail.Session;   
  14. import javax.mail.Transport;   
  15. import javax.mail.internet.InternetAddress;   
  16. import javax.mail.internet.MimeBodyPart;   
  17. import javax.mail.internet.MimeMessage;   
  18. import javax.mail.internet.MimeMultipart;   
  19. public class MailLet {   
  20.   // 定义发件人、收件人、SMTP服务器、用户名、密码、主题、内容等   
  21.   private String displayName;   
  22.   private String to;   
  23.   private String from;   
  24.   private String smtpServer;   
  25.   private String username;   
  26.   private String password;   
  27.   private String subject;   
  28.   private String content;   
  29.   private boolean ifAuth; // 服务器是否要身份认证   
  30.   private String filename = "";   
  31.   private Vector file = new Vector(); // 用于保存发送附件的文件名的集合   
  32.   private String contentType = "text/html";   
  33.   private String charset = "utf-8";   
  34.   public String getContentType() {   
  35.     return contentType;   
  36.   }   
  37.   public void setContentType(String contentType) {   
  38.     this.contentType = contentType;   
  39.   }   
  40.   public String getCharset() {   
  41.     return charset;   
  42.   }   
  43.   public void setCharset(String charset) {   
  44.     this.charset = charset;   
  45.   }   
  46.   /**  
  47.    * 设置SMTP服务器地址  
  48.    */  
  49.   public void setSmtpServer(String smtpServer) {   
  50.     this.smtpServer = smtpServer;   
  51.   }   
  52.   /**  
  53.    * 设置发件人的地址  
  54.    */  
  55.   public void setFrom(String from) {   
  56.     this.from = from;   
  57.   }   
  58.   /**  
  59.    * 设置显示的名称  
  60.    */  
  61.   public void setDisplayName(String displayName) {   
  62.     this.displayName = displayName;   
  63.   }   
  64.   /**  
  65.    * 设置服务器是否需要身份认证  
  66.    */  
  67.   public void setIfAuth(boolean ifAuth) {   
  68.     this.ifAuth = ifAuth;   
  69.   }   
  70.   /**  
  71.    * 设置E-mail用户名  
  72.    */  
  73.   public void setUserName(String username) {   
  74.     this.username = username;   
  75.   }   
  76.   /**  
  77.    * 设置E-mail密码  
  78.    */  
  79.   public void setPassword(String password) {   
  80.     this.password = password;   
  81.   }   
  82.   /**  
  83.    * 设置接收者  
  84.    */  
  85.   public void setTo(String to) {   
  86.     this.to = to;   
  87.   }   
  88.   /**  
  89.    * 设置主题  
  90.    */  
  91.   public void setSubject(String subject) {   
  92.     this.subject = subject;   
  93.   }   
  94.   /**  
  95.    * 设置主体内容  
  96.    */  
  97.   public void setContent(String content) {   
  98.     this.content = content;   
  99.   }   
  100.   public MailLet() {   
  101.   }   
  102.   public MailLet(String smtpServer, String username, String password, String from) {   
  103.     this.setSmtpServer(smtpServer);   
  104.     this.from = from;   
  105.   }   
  106.   /**  
  107.    * 初始化SMTP服务器地址、发送者E-mail地址、用户名、密码、接收者、主题、内容  
  108.    */  
  109.   public MailLet(String smtpServer, String from, String displayName, String username,   
  110.       String password, String to, String subject, String content) {   
  111.     this.smtpServer = smtpServer;   
  112.     this.from = from;   
  113.     this.displayName = displayName;   
  114.     this.ifAuth = true;   
  115.     this.username = username;   
  116.     this.password = password;   
  117.     this.to = to;   
  118.     this.subject = subject;   
  119.     this.content = content;   
  120.   }   
  121.   /**  
  122.    * 初始化SMTP服务器地址、发送者E-mail地址、接收者、主题、内容  
  123.    */  
  124.   public MailLet(String smtpServer, String from, String displayName, String to, String subject,   
  125.       String content) {   
  126.     this.smtpServer = smtpServer;   
  127.     this.from = from;   
  128.     this.displayName = displayName;   
  129.     this.ifAuth = false;   
  130.     this.to = to;   
  131.     this.subject = subject;   
  132.     this.content = content;   
  133.   }   
  134.   private int port = 25;   
  135.   public int getPort() {   
  136.     return port;   
  137.   }   
  138.   public void setPort(int port) {   
  139.     this.port = port;   
  140.   }   
  141.   /**  
  142.    * 发送邮件  
  143.    */  
  144.   public boolean send() {   
  145.     HashMap<String, String> map = new HashMap<String, String>();   
  146.     map.put("state""success");   
  147.     String message = "邮件发送成功!";   
  148.     Session session = null;   
  149.     Properties props = System.getProperties();   
  150.     props.put("mail.smtp.host", smtpServer);   
  151.     props.put("mail.smtp.port", port);   
  152.     try {   
  153.       if (ifAuth) { // 服务器需要身份认证   
  154.         props.put("mail.smtp.auth""true");   
  155.         SmtpAuth smtpAuth = new SmtpAuth(username, password);   
  156.         session = Session.getInstance(props, smtpAuth);   
  157.       } else {   
  158.         props.put("mail.smtp.auth""false");   
  159.         session = Session.getDefaultInstance(props, null);   
  160.       }   
  161.       session.setDebug(false);   
  162.       Transport trans = null;   
  163.       Message msg = new MimeMessage(session);   
  164.       try {   
  165.         Address from_address = new InternetAddress(from, displayName);   
  166.         msg.setFrom(from_address);   
  167.       } catch (java.io.UnsupportedEncodingException e) {   
  168.         e.printStackTrace();   
  169.       }   
  170.       InternetAddress[] address = { new InternetAddress(to) };   
  171.       msg.setRecipients(Message.RecipientType.TO, address);   
  172.       msg.setSubject(subject);   
  173.       Multipart mp = new MimeMultipart();   
  174.       MimeBodyPart mbp = new MimeBodyPart();   
  175.       mbp.setContent(content.toString(), getContentType() + ";charset=" + getCharset());   
  176.       mp.addBodyPart(mbp);   
  177.       if (!file.isEmpty()) {// 有附件   
  178.         Enumeration efile = file.elements();   
  179.         while (efile.hasMoreElements()) {   
  180.           mbp = new MimeBodyPart();   
  181.           filename = efile.nextElement().toString(); // 选择出每一个附件名   
  182.           FileDataSource fds = new FileDataSource(filename); // 得到数据源   
  183.           // mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart   
  184.           mbp.setContent("12121212""text/html");   
  185.           mbp.setFileName(MimeUtility.encodeText(fds.getName(), getCharset(),"B"))); // 得到文件名同样至入BodyPart   
  186.           mp.addBodyPart(mbp);   
  187.         }   
  188.         file.removeAllElements();   
  189.       }   
  190.       msg.setContent(mp); // Multipart加入到信件   
  191.       msg.setSentDate(new Date()); // 设置信件头的发送日期   
  192.       // 发送信件   
  193.       msg.saveChanges();   
  194.       trans = session.getTransport("smtp");   
  195.       trans.connect(smtpServer, username, password);   
  196.       trans.sendMessage(msg, msg.getAllRecipients());   
  197.       trans.close();   
  198.     } catch (AuthenticationFailedException e) {   
  199.       map.put("state""failed");   
  200.       message = "邮件发送失败!错误原因:/n" + "身份验证错误!";   
  201.       e.printStackTrace();   
  202.       return false;   
  203.     } catch (MessagingException e) {   
  204.       message = "邮件发送失败!错误原因:/n" + e.getMessage();   
  205.       map.put("state""failed");   
  206.       e.printStackTrace();   
  207.       Exception ex = null;   
  208.       if ((ex = e.getNextException()) != null) {   
  209.         System.out.println(ex.toString());   
  210.         ex.printStackTrace();   
  211.       }   
  212.       return false;   
  213.     }catch(Exception ex) {   
  214.       ex.printStackTrace();   
  215.       return false;   
  216.     }   
  217.     // System.out.println("/n提示信息:"+message);   
  218.     map.put("message", message);   
  219.     return true;   
  220.   }   
  221. }  
  1. package mail;      
  2.       
  3. public class SmtpAuth extends javax.mail.Authenticator {       
  4.     private String username,password;       
  5.       
  6.     public SmtpAuth(String username,String password){       
  7.         this.username = username;        
  8.         this.password = password;        
  9.     }       
  10.     protected javax.mail.PasswordAuthentication getPasswordAuthentication() {       
  11.         return new javax.mail.PasswordAuthentication(username,password);       
  12.     }       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值