javamail發送郵件的簡單例子

在進行郵件發送的時候遇到了2個問題,

 java.lang.NoClassDefFoundError: com/sun/mail/util/BEncoderStream

 java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream

這2個問題是由於java本身的mail包和mail.jar和activation有衝突,解決這一問題的辦法就是去myeclipse中的plugins裏找到


下面主要就是代碼實現

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

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailDemo {
     // 郵箱服務器
     private String host = "smtp.163.com";
     // 這個是你的郵箱用戶名
     private String username = "**********@163.com";
     // 你的郵箱密碼
     private String password = "******";

     private String mail_head_name = "this is head of this mail";

     private String mail_head_value = "this is head of this mail";

     private String mail_to = "*****@qq.com";

     private String mail_from = "**********@163.com";

     private String mail_subject = "this is the subject of this test mail";

     private String mail_body = "this is the mail_body of this test mail";

     private String personalName = "我的郵件";
     
     private void send() throws Exception {
            try {
                Properties props = new Properties(); // 獲取系統環境
                Authenticator auth = new Email_Autherticator(username,password ); // 進行郵件服務器用戶認證
                props.put( "mail.smtp.host", host);
                props.put( "mail.smtp.auth", "true");
                Session session = Session.getDefaultInstance(props, auth);
                 // 設置session,和郵件服務器進行通訊。
                MimeMessage message = new MimeMessage(session);
                 // message.setContent("foobar, "application/x-foobar"); // 設置郵件格式
                message.setSubject( mail_subject); // 設置郵件主題
                message.setText( mail_body); // 設置郵件正文
                message.setHeader( mail_head_name, mail_head_value); // 設置郵件標題
                message.setSentDate( new Date()); // 設置郵件發送日期
                Address address = new InternetAddress( mail_from, personalName);
                message.setFrom(address); // 設置郵件發送者的地址
                Address toAddress = new InternetAddress(mail_to); // 設置郵件接收方的地址
                message.addRecipient(Message.RecipientType. TO, toAddress);
                Transport. send(message); // 發送郵件
                System. out.println( "send ok!");
           } catch (Exception ex) {
                 throw new Exception(ex.getMessage());
           }
     }
     
     public class Email_Autherticator extends Authenticator{
            public Email_Autherticator() {
                 super();
           }

            public Email_Autherticator(String user, String pwd) {
                 super();
                 username = user;
                 password = pwd;
           }

            public PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication( username, password);
           }
     }
     
     public static void main(String[] args) {
           EmailDemo demo = new EmailDemo();
            try {
                demo.send();
           } catch (Exception e) {
                e.printStackTrace();
           }
     }
}


發佈了25 篇原創文章 · 獲贊 3 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章