public void sendHtml
public void sendHtml(String from, String to, String cc, String bcc,
String subject, String msgText, String msgEncode, String[] fileNames)
throws Exception {
if (fileNames!=null) {
System.out.println("com.gepower.malautomation.util.MailSender Start.sendHtml:fileNames="+fileNames[0]);
}
System.out.println("com .util.MailSender .sendHtml:subject="+subject);
System.out.println("com. .util.MailSender .sendHtml:from="+from);
System.out.println("com. util.MailSender .sendHtml:to="+to);
System.out.println("com. util.MailSender .sendHtml:cc="+cc);
Session session = Session.getInstance(props, null);
Multipart mp = new MimeMultipart();
MimeMessage msg = new MimeMessage(session);
if (from != null) {
msg.setFrom(new InternetAddress(from));
} else {
msg.setFrom();
}
InternetAddress[] toAddrs = parseAddressList(to);
msg.setRecipients(Message.RecipientType.TO, toAddrs);
if (cc != null) {
InternetAddress[] ccAddrs = parseAddressList(cc);
msg.setRecipients(Message.RecipientType.CC, ccAddrs);
}
if (bcc != null) {
InternetAddress[] bccAddrs = parseAddressList(bcc);
msg.setRecipients(Message.RecipientType.BCC, bccAddrs);
}
if (subject != null) {
msg.setSubject(subject);
}
msg.setSentDate(new Date());
MimeBodyPart mbp1 = new MimeBodyPart();
if (msgText != null) {
if (msgEncode != null) {
mbp1.setContent(msgText, "text/html;charset=" + msgEncode);
} else {
mbp1.setText(msgText);
}
}
mp.addBodyPart(mbp1);
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++) {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileNames[i]);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
}
msg.setContent(mp);
Transport.send(msg);
System.out.println("com. .util.MailSender .sendHtml end :to="+to);
System.out.println("com. .util.MailSender .sendHtml: endcc="+cc);
}
file data size limmit
======================================
//check file size
if (document.malInitForm.malAttacheFile.value !="") {
var filePath;
filePath =document.malInitForm.malAttacheFile.value;
if (ShowSize(filePath)){
document.malInitForm.malAttacheFile.focus();
alert("Please upload attach file Size less than 5M.");
return false;}
}
//end check file size
function ShowSize(filePath)
{ var image=new Image();
var mySizeK , mySizeM
image.dynsrc=filePath;
mySizeK = image.fileSize/1024;
mySizeM = mySizeK /1024;
// alert(image.fileSize);
if (mySizeM <5){
// alert(filePath+"=<<==" + mySizeM+" M ");
return false; // check <<10M
}else {
// alert(filePath+"=>>10==" + mySizeM+" M ");
return true; //check >>10M
}
}
file data size limmit
======================================
public void sendHtml(String from, String to, String cc, String bcc,
String subject, String msgText, String msgEncode, String[] fileNames)
throws Exception {
if (fileNames!=null) {
System.out.println("com.gepower.malautomation.util.MailSender Start.sendHtml:fileNames="+fileNames[0]);
}
System.out.println("com .util.MailSender .sendHtml:subject="+subject);
System.out.println("com. .util.MailSender .sendHtml:from="+from);
System.out.println("com. util.MailSender .sendHtml:to="+to);
System.out.println("com. util.MailSender .sendHtml:cc="+cc);
Session session = Session.getInstance(props, null);
Multipart mp = new MimeMultipart();
MimeMessage msg = new MimeMessage(session);
if (from != null) {
msg.setFrom(new InternetAddress(from));
} else {
msg.setFrom();
}
InternetAddress[] toAddrs = parseAddressList(to);
msg.setRecipients(Message.RecipientType.TO, toAddrs);
if (cc != null) {
InternetAddress[] ccAddrs = parseAddressList(cc);
msg.setRecipients(Message.RecipientType.CC, ccAddrs);
}
if (bcc != null) {
InternetAddress[] bccAddrs = parseAddressList(bcc);
msg.setRecipients(Message.RecipientType.BCC, bccAddrs);
}
if (subject != null) {
msg.setSubject(subject);
}
msg.setSentDate(new Date());
MimeBodyPart mbp1 = new MimeBodyPart();
if (msgText != null) {
if (msgEncode != null) {
mbp1.setContent(msgText, "text/html;charset=" + msgEncode);
} else {
mbp1.setText(msgText);
}
}
mp.addBodyPart(mbp1);
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++) {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileNames[i]);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
}
msg.setContent(mp);
Transport.send(msg);
System.out.println("com. .util.MailSender .sendHtml end :to="+to);
System.out.println("com. .util.MailSender .sendHtml: endcc="+cc);
}
===============================
/** *********************************************************************************************
* File Name : MailSenderto.java
* Purpose :
* User :
* Date :
* Author :
********************************************************************************************** */
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Messa USER;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessa USER;
import javax.mail.internet.MimeMultipart;
//import com. USER.cmd.dd;
public class MailSenderto {
private Properties props = null;
public MailSenderto() {
props = System. USERtProperties();
props.put("mail.smtp.host", "122.72.79.252");
props.put("mail.smtp.port","25");
}
public void send(String from, String to, String cc, String bcc,
String subject, String msgText, String ms USERncode) throws Exception {
// USERt a Session object
Session session = Session. USERtInstance(props, null);
// construct the messa USER
Messa USER msg = new MimeMessa USER(session);
if (from != null) {
msg.setFrom(new InternetAddress(from));
} else {
msg.setFrom();
}
InternetAddress[] toAddrs = parseAddressList(to);
msg.setRecipients(Messa USER.RecipientType.TO, toAddrs);
if (cc != null) {
InternetAddress[] ccAddrs = parseAddressList(cc);
msg.setRecipients(Messa USER.RecipientType.CC, ccAddrs);
}
if (bcc != null) {
InternetAddress[] bccAddrs = parseAddressList(bcc);
msg.setRecipients(Messa USER.RecipientType.BCC, bccAddrs);
}
if (subject != null) {
msg.setSubject(subject);
}
if (msgText != null) {
if (ms USERncode != null) {
msg.setContent(msgText, "text/plain;charset=" + ms USERncode);
} else {
msg.setText(msgText);
}
}
msg.setSentDate(new Date());
Transport.send(msg);
}
public void send(String from, String to, String cc, String bcc,
String subject, String msgText, String ms USERncode, String[] fileNames)
throws Exception {
System.out.println("NiSiteListByUserCB.perform() Start...");
Session session = Session. USERtInstance(props, null);
Multipart mp = new MimeMultipart();
MimeMessa USER msg = new MimeMessa USER(session);
if (from != null) {
msg.setFrom(new InternetAddress(from));
} else {
msg.setFrom();
}
InternetAddress[] toAddrs = parseAddressList(to);
msg.setRecipients(Messa USER.RecipientType.TO, toAddrs);
if (cc != null) {
InternetAddress[] ccAddrs = parseAddressList(cc);
msg.setRecipients(Messa USER.RecipientType.CC, ccAddrs);
}
if (bcc != null) {
InternetAddress[] bccAddrs = parseAddressList(bcc);
msg.setRecipients(Messa USER.RecipientType.BCC, bccAddrs);
}
if (subject != null) {
msg.setSubject(subject);
}
msg.setSentDate(new Date());
MimeBodyPart mbp1 = new MimeBodyPart();
if (msgText != null) {
if (ms USERncode != null) {
mbp1.setContent(msgText, "text/plain;charset=" + ms USERncode);
} else {
mbp1.setText(msgText);
}
}
mp.addBodyPart(mbp1);
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++) {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileNames[i]);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds. USERtName());
mp.addBodyPart(mbp);
}
}
msg.setContent(mp);
Transport.send(msg);
}
private InternetAddress[] parseAddressList(String list) throws Exception {
Vector v = new Vector();
String token;
StringTokenizer st = new StringTokenizer(list, ",");
while (st.hasMoreTokens()) {
token = st.nextToken().trim();
v.addElement(new InternetAddress(token));
}
InternetAddress[] array = new InternetAddress[v.size()];
v.copyInto(array);
return array;
}
public void sendHtml(String from, String to, String cc, String bcc,
String subject, String msgText, String ms USERncode, String[] fileNames)
throws Exception {
if (fileNames!=null) {
System.out.println("com. USER.util.MailSender Start.sendHtml:fileNames="+fileNames[0]);
}
System.out.println("com. USER.util.MailSender .sendHtml:subject="+subject);
System.out.println("com. USER.util.MailSender .sendHtml:from="+from);
System.out.println("com. USER.util.MailSender .sendHtml:to="+to);
System.out.println("com. USER.util.MailSender .sendHtml:cc="+cc);
Session session = Session. USERtInstance(props, null);
Multipart mp = new MimeMultipart();
MimeMessa USER msg = new MimeMessa USER(session);
if (from != null) {
msg.setFrom(new InternetAddress(from));
} else {
msg.setFrom();
}
InternetAddress[] toAddrs = parseAddressList(to);
msg.setRecipients(Messa USER.RecipientType.TO, toAddrs);
if (cc != null) {
InternetAddress[] ccAddrs = parseAddressList(cc);
msg.setRecipients(Messa USER.RecipientType.CC, ccAddrs);
}
if (bcc != null) {
InternetAddress[] bccAddrs = parseAddressList(bcc);
msg.setRecipients(Messa USER.RecipientType.BCC, bccAddrs);
}
if (subject != null) {
msg.setSubject(subject);
}
msg.setSentDate(new Date());
MimeBodyPart mbp1 = new MimeBodyPart();
if (msgText != null) {
if (ms USERncode != null) {
mbp1.setContent(msgText, "text/html;charset=" + ms USERncode);
} else {
mbp1.setText(msgText);
}
}
mp.addBodyPart(mbp1);
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++) {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileNames[i]);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(fds. USERtName());
mp.addBodyPart(mbp);
}
}
msg.setContent(mp);
Transport.send(msg);
System.out.println("com. USER.util.MailSender .sendHtml end :to="+to);
System.out.println("com. USER.util.MailSender .sendHtml: endcc="+cc);
}
public void sendHtmlTO(String fromUser, String[] toList, String[] ccList, String[] bccList,
String subjectName, String messa USERTxt, String ms USERncode, String[] fileNames,String[] attachFileAsname )
throws Exception {
if (fileNames!=null ||attachFileAsname !=null) {
System.out.println("com. USER.util.MailSender Start.sendHtmlTO:fileNames="+fileNames[0]);
}
System.out.println("com. USER.util.MailSender .sendHtmlTO:subject="+subjectName);
System.out.println("com. USER.util.MailSender .sendHtmlTO:from="+fromUser);
System.out.println("com. USER.util.MailSender .sendHtmlTO:to="+toList);
System.out.println("com. USER.util.MailSender .sendHtmlTO:cc="+ccList);
System.out.println("com. USER.util.MailSender .sendHtmlTO:bccList="+bccList);
try {
EmailMessa USERInterface emailmessa USERinterface = EmailMessa USERFactory
.create();
emailmessa USERinterface.setFrom(fromUser);
// to mail address
if (toList !=null){
int toInt = toList.length;
for (int j = 0; j < toInt; j++)
emailmessa USERinterface.addTo((String) toList[j]);
}
// cc mail address
if ( ccList != null){
int ccInt = ccList.length;
for (int j = 0; j < ccInt; j++)
emailmessa USERinterface.addTo((String) ccList[j]);
}
// bcc mail address
if (bccList != null) {
int bccInt = bccList.length;
for (int j = 0; j < bccInt; j++)
emailmessa USERinterface.addTo((String) bccList[j]);
}
emailmessa USERinterface.setSubject(subjectName);
emailmessa USERinterface.setMessa USERText(messa USERTxt);
emailmessa USERinterface.setMimeType("text/html;charset="+ms USERncode);
System.out.println("MIME Type for email: text/html;"+ms USERncode);
System.out.println("Messa USER prepared sendHtmlTO ");
//////////////// attach files
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++) {
Attachment attachment = new Attachment(fileNames[i], attachFileAsname[i]);
emailmessa USERinterface.addAttachment(attachment);
}
}
// send mail
System.out.println("Before Mail Sent sendHtmlTO"+ USERtClass(). USERtName());
EmailMessa USERTransport.sendImmediately(emailmessa USERinterface);
System.out.println("After Mail sent sendHtmlTO"+ USERtClass(). USERtName());
} catch (Exception exception) {
System.out.println( USERtClass()
. USERtName()+ exception);
}
}
public static final void main(String[] args) {
String fromuser ="liangbin.liu@ USER.com";
String[] toList = null;
String[] ccList = null;
String[] bccList = null;
String toUser ="xiaoquan.wang@ USER.com";
String bccUsr = "xiaoquan.wang@ USER.com";
String subject = "test ";
// String[] filename = null;
String[] filenamePATH = new String[2];
String[] filename = new String[2];
// filenamePATH[0]="E: USER_PROJECTDEV_ USERWAL_DEVreadme.xls";
// filenamePATH[1]="E: USER_PROJECTDEV_ USERWAL_DEVreadme.xls";
String mess USERTxt = "text messa USER ";
toList = new String[2];
toList[0]=fromuser;
toList[1]=toUser;
ccList =new String[2];
ccList[0]=fromuser;
ccList[1]=toUser;
filenamePATH = null;
filename = null;
ccList = toList;
bccList =ccList;
// String fileAsName = "readme.txt";
MailSenderto mailSenderto = new MailSenderto();
try{
mailSenderto.sendHtml(fromuser, toUser, fromuser, bccUsr, subject, mess USERTxt, "UTF-8", null);
// mailSenderto.sendHtmlTO(fromuser,toList,ccList,bccList,subject,mess USERTxt,"UTF-8",filenamePATH,filename);
}
catch(Exception ex){
//Error mail
System.out.println("Exception in main() of DailyReminderToSiteLeaderCron:" + ex. USERtMessa USER());
} finally {
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/78156/viewspace-1020302/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- public void setModerationMinThreadValue(int value)thread
- 為什麼Java的main方法必須是public static void?JavaAI
- Android 主執行緒之旅——PSVM(public static void main)Android執行緒AI
- 找不到 main 方法, 請將 main 方法定義為: public static void main(String[] args)AI
- 你知道void和Void的區別嗎
- 簡述static關鍵字、void與void *(void指標)、函式指標指標函式
- Check the existence of public synonyms Remove the public synonymsREM
- 通過qsort(void * lineptr[], int left, int rifht, int (*comp)(void *, void *))解讀指標函式和void指標...指標函式
- NSObject +(void)load 和+(void)initialize 方法的理解Object
- TypeScript void 型別TypeScript型別
- JavaScript void 運算子JavaScript
- Javascript:void(0)和#JavaScript
- 【Oracle】-【同義詞】-public與非public同義詞Oracle
- 24·iOS 面試題·+(void)load; +(void)initialize; 有什麼用處?iOS面試題
- private void infoStatistics (SessionFactory sessionFactory) ;Session
- typedef void (*Fun) (void) 的理解——函式指標——typedef函式指標函式指標
- public interface View介面和public interface ViewResolver介面介紹View
- Oracle中的publicOracle
- public-image-mirror
- int存放到void指標指標
- 關於C/C++ void指標,使用void指標拷貝int 陣列C++指標陣列
- Azure Public IP DNS域名DNS
- SKYPE -- Public database projectsDatabaseProject
- win rac public ip 修改
- 修改Public/Interconnect IP
- JavaScript 中 void(0) 的含義JavaScript
- 【Linux開發技術之常見問題】一個建立執行緒時常見的問題:invalid conversion from `void*' to `void*(*)(void*)Linux執行緒
- var和public的區別
- Angular Public API 介面設計AngularAPI
- Public Private VIP的區別
- RAC 11.2.0.3 修改 Public IP
- CRS : PUBLIC網路卡的STANDBY
- 為什麼用「void 0」代替「undefined」Undefined
- javascript:void(0)的作用是什麼JavaScript
- href="javascript:void(0);" considered harmfulJavaScriptIDE
- jsjavascript:void(0)真正含義(轉)JSJavaScript
- 【Java小疑問】類和方法 前面新增public和不新增public的區別Java
- Oracle 12.2 RAC修改public ip address或public ip(subnet (netmask) or interface)Oracle