springMVC中傳送郵件可傳送附件

2021-06-23 01:23:17 字數 2496 閱讀 3979

// 以html傳送

bodypart bodypart = new mimebodypart();

bodypart.setcontent(content, "text/html; charset=utf-8");

multipart multipart = new mimemultipart();

multipart.addbodypart(bodypart);

// 單純傳送文字的用settext即可

// message.settext(content);

string mailid = uuid.randomuuid().tostring();

mapfilemap = multipartrequest.getfilemap();

string destdir = request.getservletcontext().getrealpath("/") + constant.attachment_path + sender + "/" + mailid + "/";

if(filemap.size() > 0)

stringbuilder attachment = new stringbuilder();

for (entryentry : filemap.entryset()) catch (ioexception e)

// 附件還沒上傳完就傳送可能有問題

filedatasource filedatasource = new filedatasource(destfile);

bodypart = new mimebodypart();

bodypart.setdatahandler(new datahandler(filedatasource));

bodypart.setfilename(realfilename);

multipart.addbodypart(bodypart);

size += destfile.length();

}message.setcontent(multipart);

transport.send(message, username, password);

mapmap = new hashmap();

map.put("mailid", mailid);

map.put("recipient", recipient);

map.put("sender", sender);

map.put("subject", subject);

map.put("content", content);

map.put("time", dateutils.getdatetime());

map.put("size", size);

map.put("attachment", attachment.length() > 0 ? attachment.substring(1) : "");

int i = mybatisutils.insert("mail.mysql.insertmail", map);

if(i == 0)

} catch (messagingexception e)

return "success";

}

不含附件的簡單傳送

// 以html傳送

bodypart bodypart = new mimebodypart();

bodypart.setcontent(content, "text/html; charset=utf-8");

multipart multipart = new mimemultipart();

multipart.addbodypart(bodypart);

// 單純傳送文字的用settext即可

// message.settext(content);

message.setcontent(multipart);

transport.send(message, username, password);

} catch (messagingexception e)

return "success";

}參考:

獲取含上傳檔案表單中的非檔案字段

如何傳送附件

C 中傳送郵件

using system using system.collections.generic using system.componentmodel using system.data using system.drawing using system.linq using system.text u...

Linux中傳送告警郵件

1.準備乙個測試郵箱賬號 如fr 163.com,作為發件人。注意 保證郵箱的pop3 smtp是開啟狀態 如下 並且開通授權碼,記下自己郵箱的授權碼。set smtp auth password 剛剛的授權碼 set smtp auth login 3.測試一下 echo test content...

mvc專案中傳送郵件

1 使用qq的smtp 收發郵件 登入qq 設定 賬戶管理 選擇如下圖 設定完成後,需要在web.config中配置 c 後台傳送郵件處理 我寫的乙個類檔案中 public static class mail client.usedefaultcredentials false private st...