我無法從文件中讀取內容,並將其作爲電子郵件的正文發送給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)
耶! ......... – venergiac