2014-01-15 92 views
1

我無法從文件中讀取內容,並將其作爲電子郵件的正文發送給java。但是,如果我運行代碼來發送電子郵件而不從文件中讀取內容,它會成功發送電子郵件,但在從文件正文中讀取內容時會出現問題。這將是偉大的,如果你可以幫助我下面是我正在使用的代碼從文件中讀取內容併發送電子郵件

在此先感謝!

package com.example.tests; 

import java.io.BufferedReader; 
import java.io.DataInputStream; 
import java.io.FileInputStream; 
import java.io.InputStreamReader; 
import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 

public class sendEmail { 

    private static String USER_NAME = "[email protected]"; // GMail user name (just the part before "@gmail.com") 
    private static String PASSWORD = "xxxxxxx"; // GMail password 
    private static String RECIPIENT = "[email protected]"; 
    static String strLine = null; 
    public static void main(String[] args) { 
     String from = USER_NAME; 
     String pass = PASSWORD; 
     String[] to = { RECIPIENT }; // list of recipient email addresses 
     String subject = "Java send mail example"; 
     String body = strLine; 

     readfile(); 
     sendFromGMail(from, pass, to, subject, body); 
    } 

    private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) { 
     Properties props = System.getProperties(); 
     String host = "smtp.gmail.com"; 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.user", from); 
     props.put("mail.smtp.password", pass); 
     props.put("mail.smtp.port", "587"); 
     props.put("mail.smtp.auth", "true"); 

     Session session = Session.getDefaultInstance(props); 
     MimeMessage message = new MimeMessage(session); 

     try { 
      message.setFrom(new InternetAddress(from)); 
      InternetAddress[] toAddress = new InternetAddress[to.length]; 

      // To get the array of addresses 
      for(int i = 0; i < to.length; i++) { 
       toAddress[i] = new InternetAddress(to[i]); 
      } 

      for(int i = 0; i < toAddress.length; i++) { 
       message.addRecipient(Message.RecipientType.TO, toAddress[i]); 
      } 

      message.setSubject(subject); 
      message.setText(body); 
      Transport transport = session.getTransport("smtp"); 
      transport.connect(host, from, pass); 
      transport.sendMessage(message, message.getAllRecipients()); 
      transport.close(); 
     } 
     catch (AddressException ae) { 
      ae.printStackTrace(); 
     } 
     catch (MessagingException me) { 
      me.printStackTrace(); 
     } 
    } 


    public static void readfile(){  
     try{ 
      // Open the file that is the first 
      // command line parameter 
      FileInputStream fstream = new FileInputStream("C:\\Sample.txt"); 
      // Get the object of DataInputStream 
      DataInputStream in = new DataInputStream(fstream); 
      BufferedReader br = new BufferedReader(new InputStreamReader(in)); 

      //Read File Line By Line 
      while ((strLine = br.readLine()) != null) { 
       // Print the content on the console 
       System.out.println (strLine); 
      } 
      //Close the input stream 
      in.close(); 
     }catch (Exception e){//Catch exception if any 
      System.err.println("Error: " + e.getMessage()); 
     } 
    } 
} 

例外,我得到:

  Exception in thread "main" java.lang.NullPointerException 
     at org.apache.geronimo.mail.util.ASCIIUtil.isAscii(ASCIIUtil.java:47) 
     at javax.mail.internet.MimeMessage.setText(MimeMessage.java:939) 
     at javax.mail.internet.MimeMessage.setText(MimeMessage.java:932) 
     at com.example.tests.sendEmail.sendFromGMail(sendEmail.java:55) 
     at com.example.tests.sendEmail.main(sendEmail.java:25) 

回答

1

strLine爲空!

您正確讀取該文件,但cyclinf文件的最後assignement將爲空

strLine = br.readLine()) != null 

可能的疏漏。

更換

 //Read File Line By Line 
     while ((strLine = br.readLine()) != null) { 
      // Print the content on the console 
      System.out.println (strLine); 
     } 

 //Read File Line By Line 
     String line; 
     strLine=""; 
     while ((line = br.readLine()) != null) { 
      // Print the content on the console 
      strLine += line; 
     } 
     System.out.println (strLine); 

或更好地利用string buffer

 //Read File Line By Line 
     String line; 
     StringBuffer buffer = new StringBuffer(""); 
     while ((line = br.readLine()) != null) { 
      // Print the content on the console 
      buffer.append(line); 
     } 
     strLine = buffer .toSTring(); 
     System.out.println (strLine); 

然後調用

sendFromGMail(from, pass, to, subject, strLine); 
0

問題是,您的body變量始終爲空。

在主要功能,改變String body = strLine;String body = mailString;和使用另一個全局變量像mailStringreadfile功能

StringBuffer mailStirng = new StringBuffer(""); 

    public static void main(String[] args) { 
      String from = USER_NAME; 
      String pass = PASSWORD; 
      String[] to = { RECIPIENT }; // list of recipient email addresses 
      String subject = "Java send mail example"; 
      readfile(); 
      String body = mailStirng.toString(); 
      sendFromGMail(from, pass, to, subject, body); 
     } 

    public static void readfile(){  
      try{ 
       // Open the file that is the first 
       // command line parameter 
       FileInputStream fstream = new FileInputStream("C:\\Sample.txt"); 
       // Get the object of DataInputStream 
       DataInputStream in = new DataInputStream(fstream); 
       BufferedReader br = new BufferedReader(new InputStreamReader(in)); 

       //Read File Line By Line 
       while ((strLine = br.readLine()) != null) { 
        // Print the content on the console 
        mailString.append(strLine); 
       } 
       //Close the input stream 
       in.close(); 
      }catch (Exception e){//Catch exception if any 
       System.err.println("Error: " + e.getMessage()); 
      } 
     } 
1

好填補它,首先,有什麼@venergiac中提到...也,你main功能,你這樣做:

final String body = strLine; // body = strLine = null 
readfile();     // initalise strLine 
sendFromGMail(from, pass, to, subject, body); // use body 

基本上,你設置body = strLine以前初始登錄strLine。 您可能還想要切換這兩行(或直接使用strLine作爲參數sendFromGmail?)。

順便說一句,在Java中Code Convention for the Java Programming Language是使用駝峯任何類名:我認爲你應該使用它,並重新命名你的類sendEmailSendEmail :)

乾杯!

+0

耶! ......... – venergiac

相關問題