「java发送表格邮件」邮件表格怎么发送
今天给各位分享java发送表格邮件的知识,其中也会对邮件表格怎么发送进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java 发送邮件,内容是要从数据库中读取的数据并列成表格的状态发送出去
- 2、java 发送邮件
- 3、求java编写的数据库报表,Email格式输出后发送到指定邮箱
- 4、java如何实现复制excel中内容并粘贴到邮件发
- 5、java实现发送邮件功能
- 6、怎么样使用JavaMail发送和接收邮件
java 发送邮件,内容是要从数据库中读取的数据并列成表格的状态发送出去
public boolean sendTextMail(MailSenderInfo mailInfo) {
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证,则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = null;
//sendMailSession = Session.getDefaultInstance(pro,authenticator); //获取默认可能报错
sendMailSession = Session.getInstance(pro,authenticator);//新创建一个session
if (sendMailSession==null){
System.out.println("无法获取邮件邮件Session");
}
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO,to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
//添加附件
// if(mailInfo.getAttachFileNames()!=null || mailInfo.getAttachFileNames().length0){
// Multipart mp = new MimeMultipart();
// MimeBodyPart mbp=null;
// for(String fileName:mailInfo.getAttachFileNames()){
// mbp=new MimeBodyPart();
// FileDataSource fds=new FileDataSource(fileName); //得到数据源
// mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart
// mbp.setFileName(fds.getName()); //得到文件名同样至入BodyPart
// mp.addBodyPart(mbp);
// }
// mailMessage.setContent(mp);
// }
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
public class MailSenderInfo {
// 发送邮件的服务器的IP和端口
private String mailServerHost;
private String mailServerPort = "25";
// 邮件发送者的地址
private String fromAddress;
// 邮件接收者的地址
private String toAddress;
// 登陆邮件发送服务器的用户名和密码
private String userName;
private String password;
// 是否需要身份验证
private boolean validate = false;
// 邮件主题
private String subject;
// 邮件的文本内容
private String content;
// 邮件附件的文件名
private String[] attachFileNames;
//邮件抄送人
private ListString ccUserList;
/** *//**
* 获得邮件会话属性
*/
public Properties getProperties(){
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
return p;
}
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String textContent) {
this.content = textContent;
}
public ListString getCcUserList(){
return ccUserList;
}
public void setCcUserList(ListString ccUserList){
this.ccUserList = ccUserList;
}
}
public static void main(String[] args) {
// 这个类主要是设置邮件
MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.setMailServerHost("smtp.163.com");
mailInfo.setMailServerPort("25");
mailInfo.setValidate(true);
mailInfo.setUserName("zhengzhanzong@163.com");
mailInfo.setPassword("zzzong0828");// 您的邮箱密码
mailInfo.setFromAddress("");
//接受方信息
mailInfo.setToAddress("");
mailInfo.setSubject("邮箱标题 ");
mailInfo.setContent("设置邮箱内容 如 中国桂花网 是中国最大桂花网站==");
String[] files=new String[]{"D:/1.txt","D:/2.txt","D:/3.txt"};
mailInfo.setAttachFileNames(files);
// 这个类主要来发送邮件
SimpleMailSender sms = new SimpleMailSender();
sms.sendTextMail(mailInfo);// 发送文体格式
//sms.sendHtmlMail(mailInfo);// 发送html格式
}
这样发
java 发送邮件
要两个java文件 还有一个mail.jar是不是只能用javamail谁也不敢说
第一个:
public class Constant {
public static final String mailAddress ="用户名@163.com";
public static final String mailCount ="用户名";
public static final String mailPassword ="密码";
public static final String mailServer ="smtp.163.com";
//pukeyouxintest,
}
第二个:
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
/**
* 发送简单邮件
* @param str_from:发件人地址
* @param str_to:收件人地址
* @param str_title:邮件标题
* @param str_content:邮件正文
*/
public static void send(String str_from,String str_to,String str_title,String str_content) {
// str_content="a href=''html元素/a"; //for testing send html mail!
try {
//建立邮件会话
Properties props=new Properties(); //用来在一个文件中存储键-值对的,其中键和值是用等号分隔的,
//存储发送邮件服务器的信息
props.put("mail.smtp.host",Constant.mailServer);
//同时通过验证
props.put("mail.smtp.auth","true");
//根据属性新建一个邮件会话
Session s=Session.getInstance(props);
s.setDebug(true); //有他会打印一些调试信息。
//由邮件会话新建一个消息对象
MimeMessage message=new MimeMessage(s);
//设置邮件
InternetAddress from= new InternetAddress(str_from); //pukeyouxintest2@163.com
message.setFrom(from); //设置发件人的地址
//
// //设置收件人,并设置其接收类型为TO
InternetAddress to=new InternetAddress(str_to); //pukeyouxintest3@163.com
message.setRecipient(Message.RecipientType.TO, to);
//设置标题
message.setSubject(str_title); //java学习
//设置信件内容
// message.setText(str_content); //发送文本邮件 //你好吗?
message.setContent(str_content, "text/html;charset=gbk"); //发送HTML邮件 //b你好/bbrp大家好/p
//设置发信时间
message.setSentDate(new Date());
//存储邮件信息
message.saveChanges();
//发送邮件
Transport transport=s.getTransport("smtp");
//以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码
transport.connect(Constant.mailServer,Constant.mailCount,Constant.mailPassword);
//发送邮件,其中第二个参数是所有已设好的收件人地址
transport.sendMessage(message,message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//测试用的,你吧你想写的内容写上去就行
send(Constant.mailAddress,"收件人邮箱","标题","b内容/b");
}
}
然后把mail.jar导入,就可以了,我用的是163 的,其他的吧相应的服务器改一下就行了
求java编写的数据库报表,Email格式输出后发送到指定邮箱
给你一段代码吧 邮箱是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();
}
}
}
java如何实现复制excel中内容并粘贴到邮件发
主要是用到java里面的i/o流。代码例子如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* java读写文件,复制文件
* 读取d:/1.txt文件内容,写入f:/text.txt文件中.
* @author young
*
*/
public class FileWriterTest {
// 读写文件
public static void rwFile(){
FileWriter fw = null;
BufferedReader br = null;
try {
fw = new FileWriter("f:\\text.txt", true);
br = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\1.txt"), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("文件内容: " + line);
fw.write(line);
fw.flush();
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
rwFile();
}
}
首先在D盘新建文件1.txt,输入任意内容。然后执行java代码即可。
java实现发送邮件功能
要实现邮件发送功能需要导入包:mail.jar
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package org.demo.action;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.demo.form.DemoForm;
public class DemoAction extends Action {
private static final String CONTENT_TYPE = "test/html;charset=GB2312";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DemoForm demoForm = (DemoForm) form;
System.out.println("标题是" + demoForm.getBiaoti());
System.out.println("内容是" + demoForm.getNeirong());
try {
response.setContentType(CONTENT_TYPE);
String smtphost = "smtp.nj.headware.cn"; // 发送邮件服务器
String user = "q0000015369"; // 邮件服务器登录用户名
String password = "Queshuwen26"; // 邮件服务器登录密码
String from = "q0000015369@nj.headware.cn"; //
String to = "c0000016205@nj.headware.cn"; // 收件人邮件地址
String subject = demoForm.getBiaoti(); // 邮件标题
String body = demoForm.getNeirong(); // 邮件内容
Properties props = new Properties();
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.auth", "true");
Session ssn = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(ssn);
InternetAddress fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(body);
Transport transport = ssn.getTransport("smtp");
transport.connect(smtphost, user, password);
transport.sendMessage(message, message
.getRecipients(Message.RecipientType.TO));
// transport.send(message);
transport.close();
return mapping.findForward("succ");
} catch (Exception e) {
e.printStackTrace();
return mapping.findForward("fail");
}
}
}
怎么样使用JavaMail发送和接收邮件
public class MailTest {
//发送的邮箱 内部代码只适用qq邮箱
private static final String USER = "xxxxx@qq.com";
//授权密码 通过QQ邮箱设置-账户-POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务-开启POP3/SMTP服务获取
private static final String PWD = "xxxxx";
private String[] to;
private String[] cc;//抄送
private String[] bcc;//密送
private String[] fileList;//附件
private String subject;//主题
private String content;//内容,可以用html语言写
public void sendMessage() throws Exception {
// 配置发送邮件的环境属性
final Properties props = new Properties();
//下面两段代码是设置ssl和端口,不设置发送不出去。
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
// 表示SMTP发送邮件,需要进行身份验证
props.setProperty("mail.transport.protocol", "smtp");// 设置传输协议
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");//QQ邮箱的服务器 如果是企业邮箱或者其他邮箱得更换该服务器地址
// 发件人的账号
props.put("mail.user", USER);
// 访问SMTP服务时需要提供的密码
props.put("mail.password", PWD);
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用环境属性和授权信息,创建邮件会话
Session mailSession = Session.getInstance(props, authenticator);
// 创建邮件消息
MimeMessage message = new MimeMessage(mailSession);
BodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
// 设置发件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
//发送
if (to != null) {
String toList = getMailList(to);
InternetAddress[] iaToList = new InternetAddress().parse(toList);
message.setRecipients(RecipientType.TO, iaToList); // 收件人
}
//抄送
if (cc != null) {
String toListcc = getMailList(cc);
InternetAddress[] iaToListcc = new InternetAddress().parse(toListcc);
message.setRecipients(RecipientType.CC, iaToListcc); // 抄送人
}
//密送
if (bcc != null) {
String toListbcc = getMailList(bcc);
InternetAddress[] iaToListbcc = new InternetAddress().parse(toListbcc);
message.setRecipients(RecipientType.BCC, iaToListbcc); // 密送人
}
message.setSentDate(new Date()); // 发送日期 该日期可以随意写,你可以写上昨天的日期(效果很特别,亲测,有兴趣可以试试),或者抽象出来形成一个参数。
message.setSubject(subject); // 主题
message.setText(content); // 内容
//显示以html格式的文本内容
messageBodyPart.setContent(content,"text/html;charset=utf-8");
multipart.addBodyPart(messageBodyPart);
//保存多个附件
if(fileList!=null){
addTach(fileList, multipart);
}
message.setContent(multipart);
// 发送邮件
Transport.send(message);
}
public void setTo(String[] to) {
this.to = to;
}
public void setCc(String[] cc) {
this.cc = cc;
}
public void setBcc(String[] bcc) {
this.bcc = bcc;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setContent(String content) {
this.content = content;
}
public void setFileList(String[] fileList) {
this.fileList = fileList;
}
private String getMailList(String[] mailArray) {
StringBuffer toList = new StringBuffer();
int length = mailArray.length;
if (mailArray != null length 2) {
toList.append(mailArray[0]);
} else {
for (int i = 0; i length; i++) {
toList.append(mailArray[i]);
if (i != (length - 1)) {
toList.append(",");
}
}
}
return toList.toString();
}
//添加多个附件
public void addTach(String fileList[], Multipart multipart) throws Exception {
for (int index = 0; index fileList.length; index++) {
MimeBodyPart mailArchieve = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileList[index]);
mailArchieve.setDataHandler(new DataHandler(fds));
mailArchieve.setFileName(MimeUtility.encodeText(fds.getName(),"UTF-8","B"));
multipart.addBodyPart(mailArchieve);
}
}
//以下是演示demo
public static void main(String args[]) {
MailTest mail = new MailTest();
mail.setSubject("java邮件");
mail.setContent("你好 这是第一个java 程序发送邮件");
//收件人 可以发给其他邮箱(163等) 下同
mail.setTo(new String[] {"xxxxx@qq.com"});
//抄送
// mail.setCc(new String[] {"xxxxx@qq.com","xxxxx@qq.com"});
//密送
//mail.setBcc(new String[] {"xxxxx@qq.com","xxxxx@qq.com"});
//发送附件列表 可以写绝对路径 也可以写相对路径(起点是项目根目录)
// mail.setFileList(new String[] {"D:\\aa.txt"});
//发送邮件
try {
mail.sendMessage();
System.out.println("发送邮件成功!");
} catch (Exception e) {
System.out.println("发送邮件失败!");
e.printStackTrace();
}
}
}
关于java发送表格邮件和邮件表格怎么发送的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-27,除非注明,否则均为
原创文章,转载请注明出处。