寻求答案:如何用JAVA程序发送邮件附件(非JAVAMAIL类),主要是发送附件格式 如题,寻求达人解答..最好有代码例子,小弟在此不胜感激 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用javaMail //添加附件 DataSource source = null;一般的javamil发送附件的代码如下: bodypart = new mimebodypart(); source = new filedatasource(file); bodypart.setdatahandler(new datahandler(source )); bodypart.setfilename(MimeUtility.encodeText(filename)); multipart.addbodypart(bodypart); 我不想用JAVAMAIL,,想自己从底层来实现一个MAIL... socket可以发邮件,用telnet,不过不知道怎么发附件 mail就是socket udp方式的,肯定超级麻烦,你要非想实现的话http://www.cn-doc.com/_soft_java_tech_doc/2005_08_19_22/20050819222518969.htm太bt了,比我预想的还bt点,楼主自己看吧,作者只是犯了个小错误,不影响你. http://blog.csdn.net/axman/archive/2007/01/19/1487853.aspx这是我一行一行敲出来的,没有任何一行是参考任何人的 看到了8楼介绍的文章中,其实我只看他的一行就知道是什么水平了:sendbase64data(new stringbuffer(base64encode(readfile(mailattachfile[i]))));发送30到50M附近正常吧?让他处理试试?100m呢?300m呢?对于发送过程只要理解smtp的人完全是小菜一碟.重要的就是要能正确处理内容的长短和附件的大小.以前8M内存的机器难道就不通用发100M的附件了?而sendbase64data(new stringbuffer(base64encode(readfile(mailattachfile[i]))));发100m的附件他根本发不出去.早内存溢出了. 呵呵,楼上前辈别太认真,smtp通讯标准文档留一份给小弟有空看看.8楼的文章确实有你说的问题,不过重在知道了标准,否则不知道标准,再好的代码发不出去,不是. 用javaMail //添加附件 DataSource source = null; 一般的javamil发送附件的代码如下: bodypart = new mimebodypart(); source = new filedatasource(file); bodypart.setdatahandler(new datahandler(source )); bodypart.setfilename(MimeUtility.encodeText(filename)); multipart.addbodypart(bodypart); 我这里运行通过的代码package com.mail.util;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.BodyPart;import javax.mail.Folder;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.Session;import javax.mail.Store;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;import org.springframework.mail.javamail.JavaMailSenderImpl;import org.springframework.mail.javamail.MimeMessageHelper;import com.mail.pojo.InterMail;/** * * @author liguohui 这个类是收发邮件的核心类 * */public class MailUtil { private String from; // 发件人 private String to; // 收件人 private String host; // 服务器 private String url; private boolean isAttatch; private boolean isHtml; private String text; private String path; private String encode; public String getEncode() { if(encode==null||encode.length()<1) return "utf-8"; return encode; } public void setEncode(String encode) { this.encode = encode; } public MailUtil(){ } public static MailUtil newInstance(){ return new MailUtil(); } public Message[] receiveMail(String username,String password){ Session session = this.getSession("pop"); try { Store store = session.getStore("pop3"); store.connect(host, username, password); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_WRITE); Message[] message = folder.getMessages(); return message; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /*public Message[] readSended(String username,String password){Session session = this.getSession("pop"); try { Store store = session.getStore("pop3"); store.connect(host, username, password); Folder folder = store.getFolder("OUTBOX"); folder.open(Folder.READ_WRITE); Message[] message = folder.getMessages(); return message; } catch (Exception e) { e.printStackTrace(); } return null; }*/ public void sendMail(String from, String to, String password,String subject) { this.setFrom(from); this.setTo(to); host = "smtp."; host += (from.split("@"))[1]; this.setHost(host); Session session = this.getSession("smtp"); JavaMailSenderImpl message = new JavaMailSenderImpl(); message.setSession(session); message.setUsername(from); message.setHost(this.getHost()); message.setPassword(password); MimeMessage mime = message.createMimeMessage(); try { //mime.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); //增加附件 //mime.setContent(this.getBodyPart()); //mime.saveChanges(); MimeMessageHelper helper = new MimeMessageHelper(mime,true,this.getEncode()); StringBuffer sb = new StringBuffer(); String html = this.getConnectionResource(url); if(html != null){ //增加html文件 sb.append(html); sb.append("<hr>"); //增加文本文件 sb.append(text); helper.setText(sb.toString(),true); }else{ //增加文本文件 sb.append(text); helper.setText(sb.toString()); } File file=new File(path); helper.setSubject(subject); if(file.exists()){ helper.addInline(file.getName(), file); helper.addAttachment(file.getName(), file); } helper.setReplyTo(from); helper.setTo(to); helper.setFrom(from); message.send(mime); mime.saveChanges(); System.out.println("OK"); } catch (Exception e) { e.printStackTrace(); } } public Multipart getBodyPart() { if(path == null || path.length() < 0){ return null; } Multipart mp = new MimeMultipart(); try { BodyPart bp = new MimeBodyPart(); FileDataSource source = new FileDataSource(path); DataHandler handler = new DataHandler(source); bp.setDataHandler(handler); bp.setFileName(source.getName()); mp.addBodyPart(bp); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return mp; } public String getConnectionResource(String sUrl){ if(sUrl == null){ return null; } StringBuffer sb = new StringBuffer(); try { URL url = new URL(sUrl); URLConnection conn = url.openConnection(); InputStream ins = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(ins)); String line = br.readLine(); while(line != null){ sb.append(line); line = br.readLine(); } } catch (Exception e) { e.printStackTrace(); } return sb.toString(); } public Session getSession(String protocal){ Properties pro = System.getProperties(); pro.put("mail."+protocal+".auth", "true"); pro.put("mail."+protocal+".host", host); Session session = Session.getDefaultInstance(pro,null); return session; } /*public Transport getTransport(Session session){ Transport tran = null; try { tran = session.getTransport("smtp"); } catch (Exception e) { e.printStackTrace(); } return tran; } */ public String getHost(){ return this.host; } // 设置发件人 public void setFrom(String from) { if(from == null){ this.from = "[email protected]"; }else{ this.from = from; } } public void setTo(String to) { if(to == null){ this.to = "[email protected]"; }else{ this.to = to; } } //默认主机为126.com public void setHost(String host) { if(host != null){ this.host = host; }else{ this.host = "smtp.126.com"; } } public void setUrl(String url) { this.url = url; } public boolean isAttatch() { return isAttatch; } public void setAttatch(boolean isAttatch) { this.isAttatch = isAttatch; } public boolean isHtml() { return isHtml; } public void setHtml(boolean isHtml) { this.isHtml = isHtml; } public String getText() { return text; } public void setText(String text) { this.text = text; } public void setPath(String path){ this.path = path; } public String getPath(){ return this.path; } public Message[] receiveMails(InterMail mail) { String username = mail.getMailName()+"@"+mail.getMailServeName(); this.setHost("pop."+mail.getMailServeName()); System.out.println("host=+++++"+host); return this.receiveMail(username, mail.getPassword()); } public static void main(String[] arg){ MailUtil mail = new MailUtil(); mail.setText("this is my mail"); mail.setUrl("http://www.163.com"); mail.setPath("D:\\mail.doc"); mail.sendMail("[email protected]", "[email protected]", "123456", "my mail"); }} 初学者求程序如何表达 请教一下,我对接口这样理解对不对: 求救SWING自定义按钮 JAVA API 对文本语法的检查 JAVA里面输出一个double型数据,怎么精确到小数点后6位 为什么jdk安装不自动配置环境变量? servlet遭遇:这是什么错误 struts的action返回值问题 用java做一个批量下载网页(象FlashGet那样)的工具需要哪些技术知识,有没有相关的sourcecode可参考? 求助:如何将byte[]类型的数据存储为二进制文件(如.gif文件), 如何在英文的windowns下编译含有中文字符的.java? 如何设置快捷键?
DataSource source = null;一般的javamil发送附件的代码如下:
bodypart = new mimebodypart();
source = new filedatasource(file);
bodypart.setdatahandler(new datahandler(source ));
bodypart.setfilename(MimeUtility.encodeText(filename));
multipart.addbodypart(bodypart);
这是我一行一行敲出来的,没有任何一行是参考任何人的
sendbase64data(new stringbuffer(base64encode(readfile(mailattachfile[i]))));发送30到50M附近正常吧?让他处理试试?100m呢?300m呢?对于发送过程只要理解smtp的人完全是小菜一碟.重要的就是要能正确处理内容的长短和附件的大小.
以前8M内存的机器难道就不通用发100M的附件了?
而sendbase64data(new stringbuffer(base64encode(readfile(mailattachfile[i]))));
发100m的附件他根本发不出去.早内存溢出了.
8楼的文章确实有你说的问题,不过重在知道了标准,否则不知道标准,再好的代码发不出去,不是.
DataSource source = null; 一般的javamil发送附件的代码如下:
bodypart = new mimebodypart();
source = new filedatasource(file);
bodypart.setdatahandler(new datahandler(source ));
bodypart.setfilename(MimeUtility.encodeText(filename));
multipart.addbodypart(bodypart);
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;import com.mail.pojo.InterMail;/**
*
* @author liguohui 这个类是收发邮件的核心类
*
*/
public class MailUtil {
private String from; // 发件人
private String to; // 收件人
private String host; // 服务器
private String url;
private boolean isAttatch;
private boolean isHtml;
private String text;
private String path;
private String encode; public String getEncode() {
if(encode==null||encode.length()<1)
return "utf-8";
return encode;
} public void setEncode(String encode) {
this.encode = encode;
} public MailUtil(){
} public static MailUtil newInstance(){
return new MailUtil();
}
public Message[] receiveMail(String username,String password){
Session session = this.getSession("pop");
try {
Store store = session.getStore("pop3");
store.connect(host, username, password);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
Message[] message = folder.getMessages();
return message;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/*public Message[] readSended(String username,String password){
Session session = this.getSession("pop");
try {
Store store = session.getStore("pop3");
store.connect(host, username, password);
Folder folder = store.getFolder("OUTBOX");
folder.open(Folder.READ_WRITE);
Message[] message = folder.getMessages();
return message;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}*/
public void sendMail(String from, String to, String password,String subject) {
this.setFrom(from);
this.setTo(to);
host = "smtp.";
host += (from.split("@"))[1];
this.setHost(host);
Session session = this.getSession("smtp");
JavaMailSenderImpl message = new JavaMailSenderImpl();
message.setSession(session);
message.setUsername(from);
message.setHost(this.getHost());
message.setPassword(password);
MimeMessage mime = message.createMimeMessage();
try {
//mime.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//增加附件
//mime.setContent(this.getBodyPart());
//mime.saveChanges();
MimeMessageHelper helper = new MimeMessageHelper(mime,true,this.getEncode());
StringBuffer sb = new StringBuffer();
String html = this.getConnectionResource(url);
if(html != null){
//增加html文件
sb.append(html);
sb.append("<hr>");
//增加文本文件
sb.append(text);
helper.setText(sb.toString(),true);
}else{
//增加文本文件
sb.append(text);
helper.setText(sb.toString());
}
File file=new File(path);
helper.setSubject(subject);
if(file.exists()){
helper.addInline(file.getName(), file);
helper.addAttachment(file.getName(), file);
}
helper.setReplyTo(from);
helper.setTo(to);
helper.setFrom(from);
message.send(mime);
mime.saveChanges();
System.out.println("OK");
} catch (Exception e) {
e.printStackTrace();
}
}
public Multipart getBodyPart() {
if(path == null || path.length() < 0){
return null;
}
Multipart mp = new MimeMultipart();
try {
BodyPart bp = new MimeBodyPart();
FileDataSource source = new FileDataSource(path);
DataHandler handler = new DataHandler(source);
bp.setDataHandler(handler);
bp.setFileName(source.getName());
mp.addBodyPart(bp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mp;
}
public String getConnectionResource(String sUrl){
if(sUrl == null){
return null;
}
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(sUrl);
URLConnection conn = url.openConnection();
InputStream ins = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
String line = br.readLine();
while(line != null){
sb.append(line);
line = br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
public Session getSession(String protocal){
Properties pro = System.getProperties();
pro.put("mail."+protocal+".auth", "true");
pro.put("mail."+protocal+".host", host);
Session session = Session.getDefaultInstance(pro,null);
return session;
}
/*public Transport getTransport(Session session){
Transport tran = null;
try {
tran = session.getTransport("smtp");
} catch (Exception e) {
e.printStackTrace();
}
return tran;
}
*/
public String getHost(){
return this.host;
} // 设置发件人
public void setFrom(String from) {
if(from == null){
this.from = "[email protected]";
}else{
this.from = from;
}
} public void setTo(String to) {
if(to == null){
this.to = "[email protected]";
}else{
this.to = to;
}
} //默认主机为126.com
public void setHost(String host) {
if(host != null){
this.host = host;
}else{
this.host = "smtp.126.com";
}
} public void setUrl(String url) {
this.url = url;
} public boolean isAttatch() {
return isAttatch;
} public void setAttatch(boolean isAttatch) {
this.isAttatch = isAttatch;
} public boolean isHtml() {
return isHtml;
} public void setHtml(boolean isHtml) {
this.isHtml = isHtml;
} public String getText() {
return text;
} public void setText(String text) {
this.text = text;
}
public void setPath(String path){
this.path = path;
}
public String getPath(){
return this.path;
} public Message[] receiveMails(InterMail mail) {
String username = mail.getMailName()+"@"+mail.getMailServeName();
this.setHost("pop."+mail.getMailServeName());
System.out.println("host=+++++"+host);
return this.receiveMail(username, mail.getPassword());
}
public static void main(String[] arg){
MailUtil mail = new MailUtil();
mail.setText("this is my mail");
mail.setUrl("http://www.163.com");
mail.setPath("D:\\mail.doc");
mail.sendMail("[email protected]", "[email protected]", "123456", "my mail");
}
}