posts - 24,  comments - 25,  trackbacks - 0

          很久沒來更新了,這次是關于發送郵件和附件的。

          import java.io.File;
          import java.util.Properties;

          import javax.activation.DataHandler;
          import javax.activation.FileDataSource;
          import javax.mail.BodyPart;
          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 SendEmail
          {
              
          private static String host     = ""// SMTP服務器地址

              
          private static String user     = ""// 用戶名

              
          private static String password = "";  // 密碼

              
          private static String from     = ""// 發件人

              
          private static String to       = "";   // 收件人
              
              
          /**
               * 
               * 
          @param subject 郵件主題
               * 
          @param content 郵件內容
               * 
          @param filename 附件
               
          */

              
          public static void send(String subject, String content, String filename)
              
          {
                  Properties props 
          = new Properties();
                  BodyPart bp 
          = new MimeBodyPart();
                  Multipart mp 
          = new MimeMultipart();
                  
                  props.put(
          "mail.smtp.host", host);//指定SMTP服務器
                  props.put("mail.transpost.protocol""smtp");
                  props.put(
          "mail.smtp.port""25");
                  props.put(
          "mail.smtp.auth""true");//指定是否需要SMTP驗證
                  
                  
          try
                  
          {
                      Session mailSession 
          = Session.getDefaultInstance(props);
                      mailSession.setDebug(
          true);//是否在控制臺顯示debug信息

                      Message message 
          = new MimeMessage(mailSession);
                      message.setFrom(
          new InternetAddress(from));//發件人
                      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));//收件人
                      
          //message.setRecipients(Message.RecipientType.CC, (Address[]) InternetAddress.parse(to));//抄送
                      message.setSubject(subject);//郵件主題
                      
          //message.setContent(content);//郵件正文無需附件時可以使用
                      bp.setContent(content, "text/html;charset=GB2312");//郵件正文
                      mp.addBodyPart(bp);
                      
          // 附件
                      if (!StringUtil.isBlank(filename))
                      
          {
                          File file 
          = new File(filename);
                          
          if (file.exists())
                          
          {
                              BodyPart mdp 
          = new MimeBodyPart();
                              FileDataSource fileds 
          = new FileDataSource(file);
                              mdp.setDataHandler(
          new DataHandler(fileds));
                              mdp.setFileName(fileds.getName());
                              mp.addBodyPart(mdp);
                          }

                      }

                      message.setContent(mp);
                      message.saveChanges();

                      Transport transport 
          = mailSession.getTransport("smtp");
                      transport.connect(host, user, password);
                      transport.sendMessage(message, message.getAllRecipients());
                      transport.close();
                  }

                  
          catch (Exception e)
                  
          {
                      System.out.println(e);
                      
          return;
                  }

              }


              
          public static void main(String[] args)
              
          {
                  SendEmail.send(
          "測試郵件""這個是測試內容!""D:\\update.xls");
              }

          }


          posted on 2010-11-15 17:17 Jarry 閱讀(177) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 五台县| 白玉县| 兴仁县| 南宫市| 古浪县| 井研县| 榆中县| 伊金霍洛旗| 高碑店市| 沅陵县| 遂宁市| 渑池县| 义马市| 仪陇县| 元谋县| 茂名市| 龙山县| 昌图县| 翁牛特旗| 怀柔区| 共和县| 惠州市| 通化县| 广州市| 临清市| 吉安市| 民权县| 思茅市| 永春县| 施秉县| 斗六市| 孟津县| 道孚县| 余姚市| 郯城县| 温宿县| 门头沟区| 鄂托克前旗| 隆回县| 郴州市| 洪湖市|