導航:首頁 > 編程語言 > javamail發送附件

javamail發送附件

發布時間:2021-10-25 17:35:03

1. 用java來發送帶附件的郵件

用java來發送帶附件的郵件要翻譯的。

2. javamail發送帶Excel附件郵件

我這里有個可用的。也來自網路,測了下可以。。

3. javaMail實現郵件轉發,同時轉發的郵件應該包含附件,怎麼實現

看去吧
http://www.blogjava.net/stevenjohn/archive/2011/10/18/361492.html
http://www.blogjava.net/action/archive/2006/04/24/42822.html

4. Javamail發送附件,為什麼將附件內容直接顯示在郵件正文中

DataSource source = new FileDataSource(new File("E:\\path.txt"));
body.setDataHandler(new DataHandler(source));
你好,復在後面在加上附製件文件名的設置:
body.setFileName(MimeUtility.encodeText(文件名));//設置附件文件名
例如:
body.setFileName(MimeUtility.encodeText(new File("E:\\path.txt").getName()));

ps:郵箱會根據附件的MIME類型自動打開附件,設置一個文件名可以將附件流控製成文件,不讓郵箱自動打開

5. java sendmail 怎麼發送郵件帶超大附件,求大神幫幫

和伺服器有關的,伺服器如果拒絕就沒法了 ,,,,還和連接時間長短的限制有關

~~~~~~

6. 怎麼用java發送帶附件的郵件代碼詳解

packageemail;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.PrintWriter;
importjava.io.UnsupportedEncodingException;
importjava.net.Socket;
importjava.nio.charset.Charset;
importjava.text.SimpleDateFormat;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importsun.misc.BASE64Encoder;
publicclassMail{
privatestaticfinalStringLINE_END=" ";
privatebooleanisDebug=true;
=true;
privateStringhost;
privateStringfrom;
privateList<String>to;
privateList<String>cc;
privateList<String>bcc;
privateStringsubject;
privateStringuser;
privateStringpassword;
privateStringcontentType;
privateStringboundary;
privateStringboundaryNextPart;
;
privateStringcharset;
;
privateStringcontent;
;
;
privateList<MailPart>partSet;
privatestaticMap<String,String>contentTypeMap;
static{
//MIMEMediaTypes
contentTypeMap=newHashMap<String,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");
}
{
publicMailPart(){
}
}
publicMail(){
defaultAttachmentContentType="application/octet-stream";
simpleDatePattern="yyyy-MM-ddHH:mm:ss";
boundary="--=_NextPart_zlz_3907_"+System.currentTimeMillis();
boundaryNextPart="--"+boundary;
contentTransferEncoding="base64";
contentType="multipart/alternative";
charset=Charset.defaultCharset().name();
partSet=newArrayList<MailPart>();
to=newArrayList<String>();
cc=newArrayList<String>();
bcc=newArrayList<String>();
}
(StringfileName){
Stringret=null;
if(null!=fileName){
intflag=fileName.lastIndexOf(".");
if(0<=flag&&flag<fileName.length()-1){
fileName=fileName.substring(flag+1);
}
ret=contentTypeMap.get(fileName);
}
if(null==ret){
ret=defaultAttachmentContentType;
}
returnret;
}

privateStringtoBase64(Stringstr,Stringcharset){
if(null!=str){
try{
returntoBase64(str.getBytes(charset));
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}
}
return"";
}
privateStringtoBase64(byte[]bs){
returnnewBASE64Encoder().encode(bs);
}
privateStringtoBase64(Stringstr){
returntoBase64(str,Charset.defaultCharset().name());
}
privateStringgetAllParts(){
intpartCount=partSet.size();
StringBuildersbd=newStringBuilder(LINE_END);
for(inti=partCount-1;i>=0;i--){
Mailattachment=partSet.get(i);
StringattachmentContent=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();
returnsbd.toString();
}
privatevoidaddContent(){
if(null!=content){
MailPartpart=newMailPart();
part.setContent(toBase64(content));
part.setContentType("text/plain;charset=""+charset+""");
partSet.add(part);
}
}
privateStringlistToMailString(List<String>mailAddressList){
StringBuildersbd=newStringBuilder();
if(null!=mailAddressList){
intlistSize=mailAddressList.size();
for(inti=0;i<listSize;i++){
if(0!=i){
sbd.append(";");
}
sbd.append("<").append(mailAddressList.get(i)).append(">");
}
}
returnsbd.toString();
}
privateList<String>getrecipient(){
List<String>list=newArrayList<String>();
list.addAll(to);
list.addAll(cc);
list.addAll(bcc);
returnlist;
}
publicvoidaddAttachment(StringfilePath){
addAttachment(filePath,null);
}
publicvoidaddTo(StringmailAddress){
this.to.add(mailAddress);
}
publicvoidaddCc(StringmailAddress){
this.cc.add(mailAddress);
}
publicvoidaddBcc(StringmailAddress){
this.bcc.add(mailAddress);
}
publicvoidaddAttachment(StringfilePath,Stringcharset){
if(null!=filePath&&filePath.length()>0){
Filefile=newFile(filePath);
try{
addAttachment(file.getName(),newFileInputStream(file),
charset);
}catch(FileNotFoundExceptione){
System.out.println("錯誤:"+e.getMessage());
System.exit(1);
}
}
}

7. javamail 附件有大小限制嗎,我發了50m的附件發送成功了,發送60m就不行了,請問在哪設置,小弟感激不盡

通常是伺服器的限制,如果是web程序可以看看是否發送等待中連接超時了

8. javamail如何收附件

參考代碼如下:
import javax.mail.*;
import java.util.*;
import java.io.*;

public class ReceiveMail {

//處理任何一種郵件都需要的方法
private void handle(Message msg) throws Exception {
System.out.println("郵件主題:" + msg.getSubject());
System.out.println("郵件作者:" + msg.getFrom()[0].toString());
System.out.println("發送日期:" + msg.getSentDate());
}

//處理文本郵件
private void handleText(Message msg) throws Exception {
this.handle(msg);
System.out.println("郵件內容:"+msg.getContent());
}

//處理Multipart郵件,包括了保存附件的功能
private static void handleMultipart(Message msg) throws Exception {
String disposition;
BodyPart part;

Multipart mp = (Multipart) msg.getContent();
//Miltipart的數量,用於除了多個part,比如多個附件
int mpCount = mp.getCount();
for (int m = 0; m < mpCount; m++) {
this.handle(msg);
part = mp.getBodyPart(m);
disposition = part.getDisposition();
//判斷是否有附件
if (disposition != null && disposition.equals(Part.ATTACHMENT))
{
//這個方法負責保存附件
saveAttach(part);
} else {
//不是附件,就只顯示文本內容
System.out.println(part.getContent());
}
}
}

private static void saveAttach(BodyPart part) throws Exception {
//得到未經處理的附件名字
String temp = part.getFileName();
//除去發送郵件時,對中文附件名編碼的頭和尾,得到正確的附件名
//(請參考發送郵件程序SendMail的附件名編碼部分)
String s = temp.substring(8, temp.indexOf("?="));
//文件名經過了base64編碼,下面是解碼
String fileName = base64Decoder(s);
System.out.println("有附件:" + fileName);

InputStream in = part.getInputStream();
FileOutputStream writer = new FileOutputStream(new File(
"保存附件的本地路徑"+ "\\"+fileName));
byte[] content = new byte[255];
int read = 0;
while ((read = in.read(content)) != -1) {
writer.write(content);
}
writer.close();
in.close();
}
//base64解碼
private static String base64Decoder(String s) throws Exception {
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
byte[] b = decoder.decodeBuffer(s);
return (new String(b));
}

public static void receive(String receiverMailBoxAddress, String username,String password) {
//本人用的是yahoo郵箱,故接受郵件使用yahoo的pop3郵件伺服器
String host = "pop.mail.yahoo.com.cn";
try {
//連接到郵件伺服器並獲得郵件
Properties prop = new Properties();
prop.put("mail.pop3.host", host);
Session session = Session.getDefaultInstance(prop);
Store store = session.getStore("pop3");
store.connect(host, username, password);

Folder inbox = store.getDefaultFolder().getFolder("INBOX");
//設置inbox對象屬性為可讀寫,這樣可以控制在讀完郵件後直接刪除該附件
inbox.open(Folder.READ_WRITE);

Message[] msg = inbox.getMessages();

FetchProfile profile = new FetchProfile();
profile.add(FetchProfile.Item.ENVELOPE);
inbox.fetch(msg, profile);

for (int i = 0; i < msg.length; i++) {
//標記此郵件的flag標志對象的DELETED位為true,可以在讀完郵件後直接刪除該附件,具體執行時間是在調用
//inbox.close()方法的時候
msg[i].setFlag(Flags.Flag.DELETED, true);
handleMultipart(msg[i]);
System.out.println("****************************");
}
if (inbox != null) {
//參數為true表明閱讀完此郵件後將其刪除,更多的屬性請參考mail.jar的API
inbox.close(true);
}
if (store != null) {
store.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

9. JavaMail 發送郵件附件的時候,如何把程序中讀出來的IO流直接發出去,而不是保存成臨時文件

這個需要根據你實際的代碼進行改造啊,貼上代碼

10. 你好,javamail發送郵件時在頁面上得到附件路徑問題你最終解決了嗎我不知道該怎麼獲得附件的路徑

沒明白,是路徑亂碼嗎? 是的話,就需要加密 base64,查一下關於javamail base64(好像是encode編碼)的就好了

閱讀全文

與javamail發送附件相關的資料

熱點內容
調試程序演算法的目的 瀏覽:916
上拉菜單js 瀏覽:520
精油按摩影視 瀏覽:651
ftp如何設置文件共享屬性 瀏覽:258
關於年獸好看的動漫電影有哪些 瀏覽:830
嫩模電影在線觀看泰國版 瀏覽:469
十大封禁愛情電影觀看 瀏覽:22
fm文件復制到u盤 瀏覽:773
換源網站有哪些 瀏覽:243
歐美哪部大尺度電影里的胸最好看 瀏覽:943
e網路頻率 瀏覽:832
win10用什麼優化大師 瀏覽:112
淘寶出售壓縮文件 瀏覽:220
2015中關村大數據日 瀏覽:448
電影是21比9解析度 瀏覽:625
穿越紅軍小說 瀏覽:764
linux目錄中創建新文件許可權 瀏覽:739
某500mhz計算機執行標准測試程序 瀏覽:277
大數據開發就業怎麼樣 瀏覽:683
怎麼打開蘋果手機的4g網路 瀏覽:826

友情鏈接