java

位置:IT落伍者 >> java >> 浏览文章

在Java中发送邮件的一个相当完善的脚本


发布日期:2019年05月02日
 
在Java中发送邮件的一个相当完善的脚本

package comasqlbase;

import javautil*;

import javaxmail*;

import javaxmailinternet*;

import javaxactivation*;

public class MailSender {

public final static boolean sendMail(LoadLog sendlogString smtphostString mailfrom

boolean mailauthString mailuserString mailpassword

String mailtoString mailccString mailsubject

String mailmessageString mailattach)

{

MimeMessage mimeMsg;

MimeMultipart mp;

Session session;

String sep[]={;};

Properties props=new javautilProperties();

int i;

propsput(mailsmtphostsmtphost);

try{

session = SessiongetDefaultInstance(propsnull);

mimeMsg = new MimeMessage(session);

mp = new MimeMultipart();

if(mailauth)

propsput(mailsmtpauthtrue);

else

propsput(mailsmtpauthfalse);

if (sendlog!=null)

sendlogprintln(Mail Host Address: +smtphost);

}

catch(Exception e)

{

if (sendlog!=null)

sendlogprintln(egetMessage());

return false;

}

try

{

mimeMsgsetFrom(new InternetAddress(mailfrom));

if (sendlog!=null)

sendlogprintln(Mail From Address: +mailfrom);

}

catch(Exception e)

{

if (sendlog!=null)

sendlogprintln(egetMessage());

return false;

}

try{

javautilVector temp = WordsConvertgetWords(mailtosep);

if (tempsize()==)

{

if (sendlog!=null)

sendlogprintln(Mail Target Address Requried);

return false;

}

Address toaddress[] = new Address[tempsize()];

for(i=;itoaddress[i]=InternetAddress.parse(temp.elementAt(i).toString())[0];

mimeMsg.setRecipients(Message.RecipientType.TO,toaddress);

if (sendlog!=null)

sendlog.println("Mail To Address: "+mailto);

}

catch(Exception e)

{

if (sendlog!=null)

sendlog.println("Error Mail To,"+e);

return false;

}

if(mailcc != null && mailcc.length()>)

{

try{

javautilVector temp = WordsConvertgetWords(mailccsep);

if (tempsize()>)

{

Address ccaddress[] = new Address[tempsize()];

for(i=;isize();i++)

ccaddress[i]=InternetAddressparse(tempelementAt(i)toString())[];

mimeMsgsetRecipients(MessageRecipientTypeCCccaddress);

if (sendlog!=null)

sendlogprintln(Mail Cc Address: +mailcc);

}

}

catch(Exception e)

{

if (sendlog!=null)

sendlogprintln(egetMessage());

return false;

}

}

try

{

mimeMsgsetSubject(mailsubjectGB);

BodyPart bp = new MimeBodyPart();

bpsetContent(+

mailmessagetext/html;charset=GB);

mpaddBodyPart(bp);

}

catch(Exception e)

{

if (sendlog!=null)

sendlogprintln(egetMessage());

return false;

}

if(mailattach != null && mailattachlength()>)

{

try{

javautilVector temp = WordsConvertgetWords(mailattachsep);

for(i=;isize();i++)

{

MimeBodyPart bp = new MimeBodyPart();

FileDataSource fileds = new FileDataSource(tempelementAt(i)toString());

DataHandler dh = new DataHandler(fileds);

bpsetDisposition(PartATTACHMENT);

bpsetFileName(filedsgetName());

bpsetDataHandler(dh);

mpaddBodyPart(bp);

}

}

catch(Exception e)

{

if (sendlog!=null)

sendlogprintln(egetMessage());

return false;

}

}

try{

mimeMsgsetContent(mp);

mimeMsgsaveChanges();

Session mailSession = SessiongetInstance(propsnull);

Transport transport = mailSessiongetTransport(smtp);

nnect((String)propsget(mailsmtphost)mailusermailpassword);

transportsendMessage(mimeMsgmimeMsggetAllRecipients());

if (sendlog!=null)

sendlogprintln(Mail Successfully Sended!);

transportclose();

}

catch(Exception e)

{

if (sendlog!=null)

sendlogprintln(egetMessage());;

return false ;

}

return true;

}

}

               

上一篇:Java 程序编码规范与技巧

下一篇:JAVA进阶:提高SQL性能的几种方法