欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java mail 发邮件

程序员文章站 2022-06-13 17:49:48
...

 

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class SendMail {  
 
 private static SendMail instance = null;

 private SendMail() {

 }

 public static SendMail getInstance() {
  if (instance == null) {
   instance = new SendMail();
  }
  return instance;
 }
 
    public void send(String sb) {
        try {
         String to[]={"aa@bb.com"};
           Properties p = new Properties(); //Properties p = System.getProperties();  
            p.put("mail.smtp.auth", "true");  
            p.put("mail.transport.protocol", "smtp");  
            p.put("mail.smtp.host", "fuwuqiming");  
            p.put("mail.smtp.port", "25");  
            //建立会话  
            Session session = Session.getInstance(p);  
            Message msg = new MimeMessage(session); //建立信息  
   
            msg.setFrom(new InternetAddress("administrator@bb.com")); //发件人  
           
            String toList = getMailList(to);
            InternetAddress[] iaToList = new InternetAddress().parse(toList);
           
            msg.setRecipients(Message.RecipientType.TO,iaToList); //收件人  
   
            msg.setSentDate(new Date()); // 发送日期  
            msg.setSubject("(info)exception"); // 主题  
            //msg.setText("注意,这是测试程序发的,请不要回复!"); //内容  
           
            Multipart mp = new MimeMultipart();
            MimeBodyPart mbp = new MimeBodyPart();
              
            // 设定邮件内容的类型为 text/plain 或 text/html
            mbp.setContent(sb.toString(),"text/html;charset=GB2312");
            mp.addBodyPart(mbp);
            msg.setContent(mp);
           
            // 邮件服务器进行验证  
            Transport tran = session.getTransport("smtp");  
            tran.connect("fuwuqiming", "bb299", "xx@03");  
            // bluebit_cn是用户名,xiaohao是密码  
            tran.sendMessage(msg, msg.getAllRecipients()); // 发送  
            System.out.println("邮件发送成功");  
   
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
   
    private String getMailList(String[] mailArray){
       
        StringBuffer toList = new StringBuffer();
    int length = mailArray.length;
        if(mailArray!=null && length <2){
             toList.append(mailArray[0]);
        }else{
             for(int i=0;i<length;i++){
                     toList.append(mailArray[i]);
                     if(i!=(length-1)){
                         toList.append(",");
                     }

             }
         }
     return toList.toString();

 }
   
    public String getException(Throwable e) {

        StringBuffer bs = new StringBuffer();
        StackTraceElement[] a = e.getStackTrace();
        bs.append("<br> 信息: " + e.fillInStackTrace() + "");
        for (int i = 0; i < a.length; i++) {
            bs.append("<br>    在" + a[i].getClassName() + "类(" + a[i].getFileName() + ":" + a[i].getLineNumber() + "行,"
                    + a[i].getMethodName() + "()方法)");
        }
        return bs.toString();

    }
   
    public static void main(String[] args) {
     SendMail o = new SendMail();
     o.send("asdfasdfasdf11111111112");
 }


}

相关标签: Java HTML