Java 通过163邮箱向qq邮箱发送邮件
程序员文章站
2024-03-20 10:46:04
...
ReadNamePwdFromXml.java
package emailSender;
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class ReadNamePwdFromXml {
static String getName(String path) {
String sender_name = null;
try {
//1. Create SAXReader Object;
SAXReader srd = new SAXReader();
//2. Refer xml file path to Document;
Document content = srd.read(new File("src/emailSender/use_info.xml"));
//3. Get rootelement;
Element rootElement = content.getRootElement();
//4. Add jaxen-1.1-beta-6.jar to build path, use Xpath to simplify process;
//Node nametag = rootElement.selectSingleNode("//name");
//String name = nametag.getText();
//System.out.println(name);
List<Node> nameList = rootElement.selectNodes("/info/sender/name");
sender_name = nameList.get(0).getText();
//List<Node> pwd = rootElement.selectNodes("/info/sender/pwd");
//String sender_pwd =pwd.get(0).getText();
System.out.println(sender_name);
//System.out.println(sender_pwd);
} catch (Exception e) {
e.printStackTrace();
}
return sender_name;
}
static String getPwd(String path){
String sender_pwd = null;
try {
//1. Create SAXReader Object;
SAXReader srd = new SAXReader();
//2. Refer xml file path to Document;
Document content = srd.read(new File("src/emailSender/use_info.xml"));
//3. Get rootelement;
Element rootElement = content.getRootElement();
//4. Add jaxen-1.1-beta-6.jar to build path, use Xpath to simplify process;
//Node nametag = rootElement.selectSingleNode("//name");
//String name = nametag.getText();
//System.out.println(name);
//List<Node> nameList = rootElement.selectNodes("/info/sender/name");
//String sender_name = nameList.get(0).getText();
List<Node> pwd = rootElement.selectNodes("/info/sender/pwd");
sender_pwd =pwd.get(0).getText();
//System.out.println(sender_name);
System.out.println(sender_pwd);
} catch (Exception e) {
e.printStackTrace();
}
return sender_pwd;
}
static String getReceiver(String path){
String receiver = null;
try {
//1. Create SAXReader Object;
SAXReader srd = new SAXReader();
//2. Refer xml file path to Document;
Document content = srd.read(new File("src/emailSender/use_info.xml"));
//3. Get rootelement;
Element rootElement = content.getRootElement();
//4. Add jaxen-1.1-beta-6.jar to build path, use Xpath to simplify process;
//Node nametag = rootElement.selectSingleNode("//name");
//String name = nametag.getText();
//System.out.println(name);
//List<Node> nameList = rootElement.selectNodes("/info/sender/name");
//String sender_name = nameList.get(0).getText();
List<Node> pwd = rootElement.selectNodes("/info/receiver/name");
receiver =pwd.get(0).getText();
//System.out.println(sender_name);
System.out.println(receiver);
} catch (Exception e) {
e.printStackTrace();
}
return receiver;
}
}
ReadContentFromText.java
package emailSender;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/**
*@author created by Kathy
*@date 2020年7月8日---下午4:22:12
*@description
*@version
*/
public class ReadContentFromText {
public static void main(String[] args){
String path = "src/emailSender/content.txt";
String content = getContent(path);
System.out.println(content);
}
static String getContent(String path){
try {
BufferedReader br = new BufferedReader(new FileReader(path));
StringBuilder sb = new StringBuilder();
String content = null;
String line;
while((line=br.readLine())!=null){
sb.append(line+"\r\n");
}
content = sb.toString();
System.out.println(content);
return content;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
use_info.xml
content.txt
Test.java
package emailSender;
public class Test{
public static void main(String[] args) {
SendEmail se = new SendEmail();
se.Send_Email();
}
}
下一篇: jsdom可编辑div