public void sendHtml

liuliangbin發表於2009-04-10

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);
}

[@more@]

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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章