gpt4 book ai didi

java - 从文件中读取内容并发送电子邮件

转载 作者:行者123 更新时间:2023-11-30 04:04:13 26 4
gpt4 key购买 nike

我在从文件中读取内容并将其作为 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 = "zxxzz@gmail.com"; // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "xxxxxxx"; // GMail password
private static String RECIPIENT = "zxxzz@gmail.com";
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)

最佳答案

strLine 为空!

您正在正确读取文件,但循环该文件时最后一个分配将为空

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);

关于java - 从文件中读取内容并发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21147496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com