發送郵件

本文介绍了一个使用Java实现的邮件发送类的设计与实现细节。该类利用JavaMail API完成邮件构建并支持设置发件人、收件人、抄送人等基本功能,同时还能够指定邮件主题与正文内容。通过简单的配置即可实现邮件发送任务。
摘要由CSDN通过智能技术生成
package com.foxconn.logic; import java.util.Date; import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; import javax.activation.DataSource; public class MailSender { public String body; public String subject; public String from; public String host; public String to[]; public String cc[]; public String bcc[]; public boolean toflag; public boolean ccflag; public boolean bccflag; public MailSender() { toflag = false; ccflag = false; bccflag = false; } public void setFrom(String s) { from = s; } public void setTo(String s) { to = s.split(","); toflag = true; } public void setCc(String s) { cc = s.split(","); ccflag = true; } public void setBcc(String s) { bcc = s.split(","); bccflag = true; } public void setSubject(String s) { subject = s; } public void setBody(String s) { body = s; } public void setHost(String s) { host = s; } public boolean send() { boolean result = false; try { Properties properties = System.getProperties(); properties.put("mail.smtp.host",host); Session session = Session.getDefaultInstance(properties, null); session.setDebug(false); MimeMessage mimemessage = new MimeMessage(session); mimemessage.setFrom(new InternetAddress(from)); if(toflag) { InternetAddress ainternetaddress[] = new InternetAddress[to.length]; for(int i = 0; i < to.length; i++) { if(to[i].indexOf(" ")!=-1){ to[i]="/""+to[i]+"/""; } ainternetaddress[i] = new InternetAddress(to[i]); } mimemessage.setRecipients(javax.mail.Message.RecipientType.TO, ainternetaddress); } if(ccflag) { InternetAddress ainternetaddress1[] = new InternetAddress[cc.length]; for(int j = 0; j < cc.length; j++) { ainternetaddress1[j] = new InternetAddress(cc[j]); } mimemessage.setRecipients(javax.mail.Message.RecipientType.CC, ainternetaddress1); } if(bccflag) { InternetAddress ainternetaddress2[] = new InternetAddress[bcc.length]; for(int k = 0; k < bcc.length; k++) { ainternetaddress2[k] = new InternetAddress(bcc[k]); } mimemessage.setRecipients(javax.mail.Message.RecipientType.BCC, ainternetaddress2); } mimemessage.setSubject(subject, "big5"); MimeMultipart mimemultipart = new MimeMultipart(); MimeBodyPart mimebodypart = new MimeBodyPart(); mimebodypart.setContent(body, "text/html;charset=big5"); mimemultipart.addBodyPart(mimebodypart); mimemessage.setContent(mimemultipart); Transport.send(mimemessage); result = true; } catch (AddressException e) { e.printStackTrace(); return false; } catch (MessagingException e) { e.printStackTrace(); return false; } return result; } public static void main(String args[]) { String addr="CP-IT4B/CCPBG/FOXCONN"; //String[] copyaddr={"CP-SCM2B/CPBG/FOXCONN"}; //String[] bcopyaddr={"CP-SCM3B/CPBG/FOXCONN"}; //String[] attachfile={"D://aa.txt","D://aa1.txt"};//附件中中文文件名不能識別 String mailhost="10.194.216.121"; Date date = new Date(); MailSender sender=new MailSender(); sender.setHost(mailhost); sender.setFrom("CP-SCM5B/CCPBG/FOXCONN"); sender.setTo(addr); //sender.setCc(copyaddr); //sender.setBcc(bcopyaddr); sender.setBody("This is a test Mail
successful,ok
last testing+date.toString()"); sender.setSubject("成型廠設備咨詢管理系統測試郵件yyyymm"); //sender.setAttachfile(attachfile); boolean aa=sender.send(); if(aa) System.out.println("success"); } public static final String mailEnd = "


***********************************************************************
" + "     ??虜???雿輻????????????
" + "     ???雿輻???葉???噶???,隢蝟餅??????嚗?" + "     
" + "     CP蝟餌絞鞈????:
" + "     ?蝟颱犖:?銝?曀?/???    TEL:563-79962/79961
" + "     Mail: CP-IT4B/CCPBG/FOXCONN" + "
     CCPBG???瘚雯蝡???:http://10.194.203.36/indexlist/index.asp" + "
***********************************************************************
"; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值