A. java發送html格式郵件是否能有js
不要帶js的,如果是想用HTML做發送的內容及布局的話,那就不要用js。
如果你是在發送郵件的時候將HTML文件作為附件的話,那是可以有js的。
B. java 發送郵件,內容是要從資料庫中讀取的數據並列成表格的狀態發送出去
publicbooleansendTextMail(MailSenderInfomailInfo){
//判斷是否需要身份認證
MyAuthenticatorauthenticator=null;
Propertiespro=mailInfo.getProperties();
if(mailInfo.isValidate()){
//如果需要身份認證,則創建一個密碼驗證器
authenticator=newMyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
//根據郵件會話屬性和密碼驗證器構造一個發送郵件的session
SessionsendMailSession=null;
//sendMailSession=Session.getDefaultInstance(pro,authenticator);//獲取默認可能報錯
sendMailSession=Session.getInstance(pro,authenticator);//新創建一個session
if(sendMailSession==null){
System.out.println("無法獲取郵件郵件Session");
}
try{
//根據session創建一個郵件消息
MessagemailMessage=newMimeMessage(sendMailSession);
//創建郵件發送者地址
Addressfrom=newInternetAddress(mailInfo.getFromAddress());
//設置郵件消息的發送者
mailMessage.setFrom(from);
//創建郵件的接收者地址,並設置到郵件消息中
Addressto=newInternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO,to);
//設置郵件消息的主題
mailMessage.setSubject(mailInfo.getSubject());
//設置郵件消息發送的時間
mailMessage.setSentDate(newDate());
//設置郵件消息的主要內容
StringmailContent=mailInfo.getContent();
mailMessage.setText(mailContent);//添加附件
//if(mailInfo.getAttachFileNames()!=null||mailInfo.getAttachFileNames().length>0){
//Multipartmp=newMimeMultipart();
//MimeBodyPartmbp=null;
//for(StringfileName:mailInfo.getAttachFileNames()){
//mbp=newMimeBodyPart();
//FileDataSourcefds=newFileDataSource(fileName);//得到數據源
//mbp.setDataHandler(newDataHandler(fds));//得到附件本身並至入BodyPart
//mbp.setFileName(fds.getName());//得到文件名同樣至入BodyPart
//mp.addBodyPart(mbp);
//}
//mailMessage.setContent(mp);
//}
//發送郵件
Transport.send(mailMessage);
returntrue;
}catch(MessagingExceptionex){
ex.printStackTrace();
}
returnfalse;
}
publicclassMailSenderInfo{
//發送郵件的伺服器的IP和埠
privateStringmailServerHost;
privateStringmailServerPort="25";
//郵件發送者的地址
privateStringfromAddress;
//郵件接收者的地址
privateStringtoAddress;
//登陸郵件發送伺服器的用戶名和密碼
privateStringuserName;
privateStringpassword;
//是否需要身份驗證
privatebooleanvalidate=false;
//郵件主題
privateStringsubject;
//郵件的文本內容
privateStringcontent;
//郵件附件的文件名
privateString[]attachFileNames;
//郵件抄送人
privateList<String>ccUserList;
/***//**
*獲得郵件會話屬性
*/
publicPropertiesgetProperties(){
Propertiesp=newProperties();
p.put("mail.smtp.host",this.mailServerHost);
p.put("mail.smtp.port",this.mailServerPort);
p.put("mail.smtp.auth",validate?"true":"false");
returnp;
}
publicStringgetMailServerHost(){
returnmailServerHost;
}
publicvoidsetMailServerHost(StringmailServerHost){
this.mailServerHost=mailServerHost;
}
publicStringgetMailServerPort(){
returnmailServerPort;
}
publicvoidsetMailServerPort(StringmailServerPort){
this.mailServerPort=mailServerPort;
}
publicbooleanisValidate(){
returnvalidate;
}
publicvoidsetValidate(booleanvalidate){
this.validate=validate;
}
publicString[]getAttachFileNames(){
returnattachFileNames;
}
publicvoidsetAttachFileNames(String[]fileNames){
this.attachFileNames=fileNames;
}
publicStringgetFromAddress(){
returnfromAddress;
}
publicvoidsetFromAddress(StringfromAddress){
this.fromAddress=fromAddress;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicStringgetToAddress(){
returntoAddress;
}
publicvoidsetToAddress(StringtoAddress){
this.toAddress=toAddress;
}
publicStringgetUserName(){
returnuserName;
}
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
publicStringgetSubject(){
returnsubject;
}
publicvoidsetSubject(Stringsubject){
this.subject=subject;
}
publicStringgetContent(){
returncontent;
}
publicvoidsetContent(StringtextContent){
this.content=textContent;
}
publicList<String>getCcUserList(){
returnccUserList;
}
publicvoidsetCcUserList(List<String>ccUserList){
this.ccUserList=ccUserList;
}
}
publicstaticvoidmain(String[]args){
//這個類主要是設置郵件
MailSenderInfomailInfo=newMailSenderInfo();
mailInfo.setMailServerHost("smtp.163.com");
mailInfo.setMailServerPort("25");
mailInfo.setValidate(true);
mailInfo.setUserName("[email protected]");
mailInfo.setPassword("zzzong0828");//您的郵箱密碼
mailInfo.setFromAddress("");
//接受方信息
mailInfo.setToAddress("");
mailInfo.setSubject("郵箱標題");
mailInfo.setContent("設置郵箱內容如http://www.guihua.org中國桂花網是中國最大桂花網站==");
String[]files=newString[]{"D:/1.txt","D:/2.txt","D:/3.txt"};
mailInfo.setAttachFileNames(files);
//這個類主要來發送郵件
SimpleMailSendersms=newSimpleMailSender();
sms.sendTextMail(mailInfo);//發送文體格式
//sms.sendHtmlMail(mailInfo);//發送html格式
}
這樣發