java

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

Java Mail 深入浅出(2)


发布日期:2018年08月27日
 
Java Mail 深入浅出(2)

/**

* SendMailService 构造子注解

*/

public SendMail() {

super();

}

private void fillMail(Session sessionMimeMessage msg) throws IOException MessagingException{

String fileName = null;

Multipart mPart = new MimeMultipart();

if (mailFrom != null) {

msgsetFrom(new InternetAddress(mailFrom));

Systemoutprintln(发送人Mail地址+mailFrom);

} else {

Systemoutprintln(没有指定发送人邮件地址!);

return;

}

if (mailTo != null) {

InternetAddress[] address = InternetAddressparse(mailTo);

msgsetRecipients(MessageRecipientTypeTO address);

Systemoutprintln(收件人Mail地址+mailTo);

} else {

Systemoutprintln(没有指定收件人邮件地址!);

return;

}

if (mailccTo != null) {

InternetAddress[] ccaddress = InternetAddressparse(mailccTo);

Systemoutprintln(CCMail地址+mailccTo);

msgsetRecipients(MessageRecipientTypeCC ccaddress);

}

if (mailbccTo != null) {

InternetAddress[] bccaddress = InternetAddressparse(mailbccTo);

Systemoutprintln(BCCMail地址+mailbccTo);

msgsetRecipients(MessageRecipientTypeBCC bccaddress);

}

msgsetSubject(subject);

InternetAddress[] replyAddress = { new InternetAddress(mailFrom)};

msgsetReplyTo(replyAddress);

// create and fill the first message part

MimeBodyPart mBodyContent = new MimeBodyPart();

if (msgContent != null)

mBodyContentsetContent(msgContent messageContentMimeType);

else

mBodyContentsetContent( messageContentMimeType);

mPartaddBodyPart(mBodyContent);

// attach the file to the message

if (attachedFileList != null) {

for (Enumeration fileList = attachedFileListelements(); fileListhasMoreElements();) {

fileName = (String) fileListnextElement();

MimeBodyPart mBodyPart = new MimeBodyPart();

// attach the file to the message

FileDataSource fds = new FileDataSource(messageBasePath + fileName);

Systemoutprintln(Mail发送的附件为+messageBasePath + fileName);

mBodyPartsetDataHandler(new DataHandler(fds));

mBodyPartsetFileName(fileName);

mPartaddBodyPart(mBodyPart);

}

}

msgsetContent(mPart);

msgsetSentDate(new Date());

}

/**

* 此处插入方法说明

*/

public void init()

{

}

/**

* 发送e_mail返回类型为int

* 当返回值为说明邮件发送成功

* 当返回值为说明邮件发送失败

*/

public int sendMail() throws IOException MessagingException {

int loopCount;

Properties props = SystemgetProperties();

propsput(mailsmtphost smtpHost);

propsput(mailsmtpauth true);

MailAuthenticator auth = new MailAuthenticator();

Session session = SessiongetInstance(props auth);

sessionsetDebug(debug);

MimeMessage msg = new MimeMessage(session);

Transport trans = null;

try {

fillMail(sessionmsg);

// send the message

trans = sessiongetTransport(smtp);

try {

nnect(smtpHost MailAuthenticatorHUAWEI_MAIL_USER MailAuthenticatorHUAWEI_MAIL_PASSWORD);// HUAWEI_MAIL_PASSWORD);

} catch (AuthenticationFailedException e) {

eprintStackTrace();

Systemoutprintln(连接邮件服务器错误);

return ;

} catch (MessagingException e) {

Systemoutprintln(连接邮件服务器错误);

return ;

}

transsend(msg);

transclose();

} catch (MessagingException mex) {

Systemoutprintln(发送邮件失败);

mexprintStackTrace();

Exception ex = null;

if ((ex = mexgetNextException()) != null) {

Systemoutprintln(extoString());

exprintStackTrace();

}

return ;

} finally {

try {

if (trans != null && transisConnected())

transclose();

} catch (Exception e) {

Systemoutprintln(etoString());

}

}

Systemoutprintln(发送邮件成功!);

return ;

}

public void setAttachedFileList(javautilVector filelist)

{

attachedFileList = filelist;

}

public void setDebug(boolean debugFlag)

{

debug=debugFlag;

}

public void setMailAccount(String strAccount) {

mailAccount = strAccount;

}

public void setMailbccTo(String bccto) {

mailbccTo = bccto;

}

public void setMailccTo(String ccto) {

mailccTo = ccto;

}

public void setMailFrom(String from)

{

mailFrom=from;

}

public void setMailPass(String strMailPass) {

mailPass = strMailPass;

}

public void setMailTo(String to)

{

mailTo=to;

}

public void setMessageBasePath(String basePath)

{

messageBasePath=basePath;

}

public void setMessageContentMimeType(String mimeType)

{

messageContentMimeType = mimeType;

}

public void setMsgContent(String content)

{

msgContent=content;

}

public void setSMTPHost(String host)

{

smtpHost=host;

}

public void setSubject(String sub)

{

subject=sub;

}

public static void main(String[] argv) throws Exception

{

for(int i = ;i<;i++) {

SendMail sm = new SendMail();

smsetSMTPHost(SMTP地址);

smsetMailFrom(发送地址);

smsetMailTo(目标地址);

smsetMsgContent(内容);

smsetSubject(标题);

smsendMail();

}

}

}

               

上一篇:Java在处理大数据的时候一些小技巧

下一篇:Java,Beans 和 关系数据库 的 镜像