gpt4 book ai didi

java - 如何用Java从数据库发送电子邮件?

转载 作者:行者123 更新时间:2023-12-02 04:31:03 27 4
gpt4 key购买 nike

我正在开发我的项目,我想在 java 应用程序中从我的数据库发送电子邮件。我已经与我的数据库建立了连接,我可以在其中提取电子邮件、主题、正文和附件。我还创建了发送与电子邮件服务器连接的电子邮件的代码。我想要的是将这两个代码组合在一起,我希望数据库中的信息填充到我的发送电子邮件代码中。这是我用 Java 连接数据库的代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Connect {

public static void main(String[] args) {

Connection conn = null;
String dbName = "Employee";
String serverip="100.00.000.000";
String serverport="3316";
String url = "jdbc:sqlserver://"+serverip+"\\SQLEXPRESS:"+serverport+";databaseName="+dbName+"";
Statement stmt = null;
ResultSet result = null;
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String databaseUserName = "sys";
String databasePassword = "123";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, databaseUserName, databasePassword);
stmt = conn.createStatement();
result = null;
String emailTo,emailSubject,emailBody,emailAttachments;
result = stmt.executeQuery("Select * From Employees");

while (result.next()) {
emailTo =result.getString("emailTo");
emailSubject = result.getString("emailSubject");
emailBody = result.getString("emailBody");
emailAttachments = result.getString("emailAttachments");
System.out.println(emailTo + " /" + emailSubject + " /" + emailBody + " /" + emailAttachments);
}

conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

这是我发送电子邮件的代码:

import java.util.Properties;     
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Send {

public static void main(String[] args) {

final String username = "work@gmail.com";
final String password = "1234";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "100.00.000.000");
props.put("mail.smtp.port", "20");

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("work@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("Here I want to use emailTo from my Database"));
message.setSubject("Here emailSubject");
message.setText("Here emailBody");
message.setAttachment("Here emailAttachments");

Transport.send(message);

System.out.println("Success");

} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}

如果有人可以帮助合并此内容,请告诉我。提前致谢。

最佳答案

你好,你总是必须尝试解耦你的代码并寻找最佳实践,我在你的源代码中做了一些修改,以便使用 dao 和解耦你的类,还有很多其他的东西你必须修改但这是一个很好的开始方式。检查下一个链接以了解一些 OOP 概念 http://docs.oracle.com/javase/tutorial/java/concepts/

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

class Employee {
private String emailTo;
private String emailSubject;
private String emailBody;
private String emailAttachments;

public Employee() {
// TODO Auto-generated constructor stub
}

public Employee(String emailTo, String emailSubject, String emailBody,
String emailAttachments) {
super();
this.emailTo = emailTo;
this.emailSubject = emailSubject;
this.emailBody = emailBody;
this.emailAttachments = emailAttachments;
}

public String getEmailTo() {
return emailTo;
}

public void setEmailTo(String emailTo) {
this.emailTo = emailTo;
}

public String getEmailSubject() {
return emailSubject;
}

public void setEmailSubject(String emailSubject) {
this.emailSubject = emailSubject;
}

public String getEmailBody() {
return emailBody;
}

public void setEmailBody(String emailBody) {
this.emailBody = emailBody;
}

public String getEmailAttachments() {
return emailAttachments;
}

public void setEmailAttachments(String emailAttachments) {
this.emailAttachments = emailAttachments;
}

}

class EmployeeDao {
private Connection con;

private static final String GET_EMPLOYEES = "Select * From Employees";

private void connect() throws InstantiationException,
IllegalAccessException, ClassNotFoundException, SQLException {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
.newInstance();
con = DriverManager
.getConnection("jdbc:sqlserver://100.00.000.000\\SQLEXPRESS:3316;databaseName=Employee");
}

public List<Employee> getEmployees() throws Exception {
connect();
PreparedStatement ps = con.prepareStatement(GET_EMPLOYEES);
ResultSet rs = ps.executeQuery();
List<Employee> result = new ArrayList<Employee>();
while (rs.next()) {
result.add(new Employee(rs.getString("emailTo"), rs
.getString("emailSubject"), rs.getString("emailBody"), rs
.getString("emailAttachments")));
}
disconnect();
return result;
}

private void disconnect() throws SQLException {
if (con != null) {
con.close();
}
}
}

class EmailSender {
private Session session;

private void init() {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "100.00.000.000");
props.put("mail.smtp.port", "20");

session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("work@gmail.com", "1234");
}
});
}

public void sendEmail(Employee e) throws MessagingException {
init();
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("work@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(e.getEmailTo()));
message.setSubject(e.getEmailSubject());
message.setText(e.getEmailBody());
Transport.send(message);
}
public void sendEmail(List<Employee> employees) throws MessagingException{
for (Employee employee : employees) {
sendEmail(employee);
}
}
}

public class Main {
public static void main(String[] args) throws Exception {
EmployeeDao dao=new EmployeeDao();
List<Employee> list=dao.getEmployees();
EmailSender sender=new EmailSender();
sender.sendEmail(list);
}
}

关于java - 如何用Java从数据库发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31458753/

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