java发送邮件表格(java向邮箱发送邮件)

华为云服务器特价优惠火热进行中!

2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。

合作流程:
1、点击链接注册/关联华为云账号:点击跳转
2、添加客服微信号:cloud7591,确定产品方案、价格方案、服务支持方案等;
3、客服协助购买,并拉微信技术服务群,享受一对一免费技术支持服务;
技术专家在金蝶、华为、腾讯原厂有多年工作经验,并已从事云计算服务8年,可对域名、备案、网站搭建、系统部署、AI人工智能、云资源规划等上云常见问题提供更专业靠谱的服务,对相应产品提供更优惠的报价和方案,欢迎咨询。

今天给各位分享java发送邮件表格的知识,其中也会对java向邮箱发送邮件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

微信号:cloud7591
如需了解更多,欢迎添加客服微信咨询。
复制微信号

本文目录一览:

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发送带附件的邮件代码详解

package email;  

import java.io.BufferedReader;   

import java.io.File;  

import java.io.FileInputStream;  

import java.io.FileNotFoundException;  

import java.io.IOException;  

import java.io.InputStream;  

import java.io.InputStreamReader;  

import java.io.PrintWriter;  

import java.io.UnsupportedEncodingException;  

import java.net.Socket;  

import java.nio.charset.Charset;  

import java.text.SimpleDateFormat;  

import java.util.ArrayList;  

import java.util.Date;  

import java.util.HashMap;  

import java.util.List;  

import java.util.Map;  

import sun.misc.BASE64Encoder;  

public class Mail {  

private static final String LINE_END = "\r\n";  

private boolean isDebug = true;  

private boolean isAllowReadSocketInfo = true;  

private String host;  

private String from;  

private ListString to;  

private ListString cc;  

private ListString bcc;  

private String subject;  

private String user;  

private String password;  

private String contentType;  

private String boundary;  

private String boundaryNextPart;  

private String contentTransferEncoding;  

private String charset;  

private String contentDisposition;  

private String content;  

private String simpleDatePattern;  

private String defaultAttachmentContentType;  

private ListMailPart partSet;  

private static MapString, String contentTypeMap;  

static {  

// MIME Media Types  

contentTypeMap = new HashMapString, String();  

contentTypeMap.put("xls", "application/vnd.ms-excel");  

contentTypeMap.put("xlsx", "application/vnd.ms-excel");  

contentTypeMap.put("xlsm", "application/vnd.ms-excel");  

contentTypeMap.put("xlsb", "application/vnd.ms-excel");  

contentTypeMap.put("doc", "application/msword");  

contentTypeMap.put("dot", "application/msword");  

contentTypeMap.put("docx", "application/msword");  

contentTypeMap.put("docm", "application/msword");  

contentTypeMap.put("dotm", "application/msword");  

}  

private class MailPart extends Mail {  

public MailPart() {  

}  

}  

public Mail() {  

defaultAttachmentContentType = "application/octet-stream";  

simpleDatePattern = "yyyy-MM-dd HH:mm:ss";  

boundary = "--=_NextPart_zlz_3907_" + System.currentTimeMillis();  

boundaryNextPart = "--" + boundary;  

contentTransferEncoding = "base64";  

contentType = "multipart/alternative";  

charset = Charset.defaultCharset().name();  

partSet = new ArrayListMailPart();  

to = new ArrayListString();  

cc = new ArrayListString();  

bcc = new ArrayListString();  

}  

private String getPartContentType(String fileName) {  

String ret = null;  

if (null != fileName) {  

int flag = fileName.lastIndexOf(".");  

if (0 = flag  flag  fileName.length() - 1) {  

fileName = fileName.substring(flag + 1);  

}  

ret = contentTypeMap.get(fileName);  

}  

if (null == ret) {  

ret = defaultAttachmentContentType;  

}  

return ret;  

}  

private String toBase64(String str, String charset) {  

if (null != str) {  

try {  

return toBase64(str.getBytes(charset));  

} catch (UnsupportedEncodingException e) {  

e.printStackTrace();  

}  

}  

return "";  

}  

private String toBase64(byte[] bs) {  

return new BASE64Encoder().encode(bs);  

}  

private String toBase64(String str) {  

return toBase64(str, Charset.defaultCharset().name());  

}  

private String getAllParts() {  

int partCount = partSet.size();  

StringBuilder sbd = new StringBuilder(LINE_END);  

for (int i = partCount - 1; i = 0; i--) {  

Mail attachment = partSet.get(i);  

String attachmentContent = attachment.getContent();  

if (null != attachmentContent  0  attachmentContent.length()) {  

sbd.append(getBoundaryNextPart()).append(LINE_END);  

sbd.append("Content-Type: ");  

sbd.append(attachment.getContentType());  

sbd.append(LINE_END);  

sbd.append("Content-Transfer-Encoding: ");  

sbd.append(attachment.getContentTransferEncoding());  

sbd.append(LINE_END);  

if (i != partCount - 1) {  

sbd.append("Content-Disposition: ");  

sbd.append(attachment.getContentDisposition());  

sbd.append(LINE_END);  

}  

sbd.append(LINE_END);  

sbd.append(attachment.getContent());  

sbd.append(LINE_END);  

}  

}  

sbd.append(LINE_END);  

sbd.append(LINE_END);  

partSet.clear();  

return sbd.toString();  

}  

private void addContent() {  

if (null != content) {  

MailPart part = new MailPart();  

part.setContent(toBase64(content));  

part.setContentType("text/plain;charset=\"" + charset + "\"");  

partSet.add(part);  

}  

}  

private String listToMailString(ListString mailAddressList) {  

StringBuilder sbd = new StringBuilder();  

if (null != mailAddressList) {  

int listSize = mailAddressList.size();  

for (int i = 0; i  listSize; i++) {  

if (0 != i) {  

sbd.append(";");  

}  

sbd.append("").append(mailAddressList.get(i)).append("");  

}  

}  

return sbd.toString();  

}  

private ListString getrecipient() {  

ListString list = new ArrayListString();  

list.addAll(to);  

list.addAll(cc);  

list.addAll(bcc);  

return list;  

}  

public void addAttachment(String filePath) {  

addAttachment(filePath, null);  

}  

public void addTo(String mailAddress) {  

this.to.add(mailAddress);  

}  

public void addCc(String mailAddress) {  

this.cc.add(mailAddress);  

}  

public void addBcc(String mailAddress) {  

this.bcc.add(mailAddress);  

}  

public void addAttachment(String filePath, String charset) {  

if (null != filePath  filePath.length()  0) {  

File file = new File(filePath);  

try {  

addAttachment(file.getName(), new FileInputStream(file),  

charset);  

} catch (FileNotFoundException e) {  

System.out.println("错误:" + e.getMessage());  

System.exit(1);  

}  

}  

}

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向邮箱发送邮件、java发送邮件表格的信息别忘了在本站进行查找喔。

发布于 2023-04-05 19:04:42
收藏
分享
海报
76
目录

    忘记密码?

    图形验证码

    复制成功
    微信号: cloud7591
    如需了解更多,欢迎添加客服微信咨询。
    我知道了