求java编写的数据库报表,Email格式输出后发送到指定邮箱
发布网友
发布时间:2022-05-21 02:14
我来回答
共2个回答
热心网友
时间:2023-10-11 18:39
给你一段代码吧 邮箱是sina的邮箱时候 测试没问题
类Authenticator
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class PopupAuthenticator extends Authenticator {
String username=null;
String password=null;
public PopupAuthenticator(){}
public PasswordAuthentication performCheck(String user,String pass){
username = user;
password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
类SendMail.java
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendMail {
public static void main(String[] args) {
send("title", "content");
}
public static void send(String h, String b) {
try {
Properties p = new Properties(); // Propertiesp // System.getProperties();
p.put("mail.smtp.auth", "true");
p.put("mail.transport.protocol", "smtp");
p.put("mail.smtp.host", "smtp.sina.com");
p.put("mail.smtp.port", "25");
// 建立会话
PopupAuthenticator popAuthenticator=new PopupAuthenticator();
Session session = Session.getInstance(p,popAuthenticator);
MimeMessage msg = new MimeMessage(session); // 建立信息
msg.setFrom(new InternetAddress("abc@sina.com")); // 发件人
// msg.setRecipient(MimeMessage.RecipientType.TO,
// new InternetAddress("rewohs@139.com"));
Address[] address = new Address[] {
new InternetAddress("bbb@163.com")};
msg.setRecipients(MimeMessage.RecipientType.TO, address); // 收件人
msg.setSentDate(new Date()); // 发送日期
msg.setSubject(h); // 主题
msg.setText(b); // 内容
// 邮件服务器进行验证
Transport tran = session.getTransport("smtp");
tran.connect("smtp.sina.com", "aaa", "bbb");
// aaa是用户名,bbb是密码
tran.sendMessage(msg, msg.getAllRecipients()); // 发送
// System.out.println("邮件发送成功");
} catch (Exception e) {
e.printStackTrace();
}
}
}
热心网友
时间:2023-10-11 18:40
afafaa