发送邮件通知的程序.供其它程序调用!

发送邮件通知的程序.供其它程序调用!
以前整理的一段代码,来源于网上,大家可以使用它连接到普通的邮件服务器或gmail邮件服务器发送邮件通知等.
现在还只是发送文本邮件,有兴趣的可以再看一下如何发送超文本邮件,带附件等.
//Author:Adrian 20060707

package asdf.asdf.mail;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
//使用gmail的邮件服务器
public  class EmailNoticeEx{
   public static String sendSSLGmail(String fromName,String fromEmail,String smtpServer,String smtpUser,String smtpPassword,String recipient_name,String recipients[],String subject,String message,String encoding){
  try{
   boolean debug = false;
   java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
   // Set the host smtp address
   //  设置系统属性
   Properties props = new Properties();//获得系统属性对象
   props.put("mail.transport.protocol", "smtp");    
   props.put("mail.host", smtpServer);//设置SMTP主机

   MyAuthenticator auth = new MyAuthenticator(smtpUser, smtpPassword);
   //获得邮件会话对象
   Session mailSession = Session.getDefaultInstance(props, auth);
   mailSession.setDebug(debug);
   javax.mail.internet.MimeMessage mimeMsg; //MIME邮件对象
   
   //创建MIME邮件对象
   mimeMsg = new javax.mail.internet.MimeMessage(mailSession);
   // create a message
   //Message msg = new MimeMessage(session);

   // set the from and to address
   InternetAddress addressFrom = new InternetAddress(fromEmail,fromName);
   mimeMsg.setFrom(addressFrom);

   InternetAddress[] addressTo = new InternetAddress[recipients.length];
   for (int i = 0; i < recipients.length; i++) {
       addressTo[i] = new InternetAddress(recipients[i]);
   }
   mimeMsg.setRecipients(Message.RecipientType.TO, addressTo);
   // Setting the Subject and Content Type
   mimeMsg.setSubject(subject,encoding);
   mimeMsg.setContent(message, "text/plain");
   mimeMsg.setText(message,encoding);
   Transport.send(mimeMsg);
  }catch(Exception e){
      e.printStackTrace();
      return e.getMessage();
  }
  return null;
 }
//使用普通的邮件服务器  
   public static String sendMail(String fromName,String fromEmail,String smtpServer,String smtpUser,String smtpPassword,String recipient_name,String recipients[],String subject,String message,String encoding){
          try{
           boolean debug = false;
           // Set the host smtp address
           //  设置系统属性
           Properties props = new Properties();//获得系统属性对象
           props.put("mail.transport.protocol", "smtp");
           props.put("mail.smtp.starttls.enable","true");
           props.put("mail.smtp.host", smtpServer);//设置SMTP主机
           props.put("mail.smtp.auth", "true");
           
           MyAuthenticator auth = new MyAuthenticator(smtpUser, smtpPassword);
           //获得邮件会话对象
           Session mailSession = Session.getDefaultInstance(props, auth);
           mailSession.setDebug(debug);
           javax.mail.internet.MimeMessage mimeMsg; //MIME邮件对象
           
           //创建MIME邮件对象
           mimeMsg = new javax.mail.internet.MimeMessage(mailSession);
           // create a message
           //Message msg = new MimeMessage(session);

           // set the from and to address
           InternetAddress addressFrom = new InternetAddress(fromEmail,fromName);
           mimeMsg.setFrom(addressFrom);

           InternetAddress[] addressTo = new InternetAddress[recipients.length];
           for (int i = 0; i < recipients.length; i++) {
               addressTo[i] = new InternetAddress(recipients[i]);
           }
           mimeMsg.setRecipients(Message.RecipientType.TO, addressTo);
           // Setting the Subject and Content Type
           mimeMsg.setSubject(subject,encoding);
           mimeMsg.setContent(message, "text/plain");
           mimeMsg.setText(message,encoding);
           Transport.send(mimeMsg);
          }catch(Exception e){
              e.printStackTrace();
              return e.getMessage();
          }
          return null;
         }
           

 
}// end class

// smtp需要验证时候的验证类
class MyAuthenticator
   extends javax.mail.Authenticator {
 private String strUser;
 private String strPwd;
 public MyAuthenticator(String user, String password) {
   this.strUser = user;
   this.strPwd = password;
 }

 protected PasswordAuthentication getPasswordAuthentication() {
   return new PasswordAuthentication(strUser, strPwd);
 }
}

(本文作者: EasyJF开源团队   navIme2 欢迎转载,转载请保留作者声明,谢谢!) 

你可能感兴趣的:(发送邮件通知的程序.供其它程序调用!)