|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
最初被命名为Oak,目标设定在家用电器等小型系统的编程语言,来解决诸如电视机、电话、闹钟、烤面包机等家用电器的控制和通讯问题。程序|发邮件以下是一个邮件发HTML的例子,供人人写发邮件参考.
功效是发送UL地点的HTML到邮件.个中的图片会伴同邮件一同发给和户.
packagecom.fswan.memo;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.MalformedURLException;
importjava.net.URL;
importjava.text.SimpleDateFormat;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.Properties;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
importjavax.activation.DataHandler;
importjavax.mail.Authenticator;
importjavax.mail.Message;
importjavax.mail.MessagingException;
importjavax.mail.PasswordAuthentication;
importjavax.mail.Session;
importjavax.mail.Transport;
importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeBodyPart;
importjavax.mail.internet.MimeMessage;
importjavax.mail.internet.MimeMultipart;
/**
*@authorswanFong(方志文)
*
*/
publicclassTestMail
{
publicstaticfinalStringul="http://www.qxzw.com/htmpage/0/14/123.htm";
publicstaticvoidmain(String[]args)
{
try
{
URLurl=newURL(ul);
//InputStreamins=url.openStream();
sendMail(url,"newsbot@yeah.net","smtp.yeah.net");
}catch(MalformedURLExceptione)
{
e.printStackTrace();
}
}
publicstaticvoidsendMail(URLurl,Stringmail,StringsmtpServer)
{
System.out.println("send");
MimeMultipartmp=newMimeMultipart();
StringidStr=newSimpleDateFormat("hhmmsss").format(newDate());
System.out.println(idStr);
try
{
InputStreamins=url.openStream();
StringBuffersb=newStringBuffer();
byte[]conts=newbyte[10240];
intlen=ins.read(conts);
while(len>0)
{
sb.append(newString(conts,0,len));
//System.out.println(newString(conts));
//conts=newbyte[10240];
len=ins.read(conts);
}
Stringcon=sb.toString();
Stringcon2=sb.toString();
sb=newStringBuffer();
Stringregex="]*)"?.*/?s*>";
Patternp=Pattern.compile(regex);
Matcherm=p.matcher(con);
ArrayList_mailAttachment=newArrayList();
intlastPos=0;
while(m.find())
{
sb.append(con.substring(lastPos,m.start()));
sb.append(m.group().replaceAll(m.group(1),"cid:"+idStr+_mailAttachment.size()));
_mailAttachment.add(newURL(url,m.group(1)));
lastPos=m.end();
}
sb.append(con.substring(lastPos,con.length()));
MimeBodyPartmbp2=newMimeBodyPart();
mbp2.setContent(sb.toString(),"text/html;charset=GB2312");
mp.addBodyPart(mbp2);
for(inti=0;i<_mailAttachment.size();i++)
{
MimeBodyPartmbp=newMimeBodyPart();
mbp.setDataHandler(newDataHandler((URL)_mailAttachment.get(i)));
mbp.setHeader("Content-ID",idStr+i);
mp.addBodyPart(mbp);
}
Propertiesprop=newProperties();
prop.put("mail.smtp.host",smtpServer);
prop.put("mail.smtp.auth","true");
Sessions=Session.getInstance(prop,newAuthenticator(){
publicPasswordAuthenticationgetPasswordAuthentication()
{
returnnewPasswordAuthentication("newsbot@yeah.net","fswan123");
}
});
MimeMessagemessage=newMimeMessage(s);
message.setSubject("mail");
message.setFrom(newInternetAddress(mail));
message.setRecipient(Message.RecipientType.TO,newInternetAddress(mail));
message.setContent(mp);
Transport.send(message);
}catch(MalformedURLExceptione)
{
e.printStackTrace();
}catch(IOExceptione)
{
e.printStackTrace();
}catch(MessagingExceptione)
{
e.printStackTrace();
}
System.out.println("Finished");
}
}
JAVA学习必须明确这是一项投资,对于大多数的人来说,学习JAVA是为了就业,还有就是刚走向工作位置的朋友想尽快赶上工作的节奏。 |
|