马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
java比较简单,没有C++的烦琐,但学习时最好有C++为基础.与JSP和SQL起应用,功能强大.smtp|成绩MailBean.java
packagefastfox.util;
importjava.util.*;
importjava.text.SimpleDateFormat;
importjavax.mail.*;
importjavax.mail.internet.*;
importjavax.activation.*;
importjava.io.*;
publicclassMailBean{
publicstaticvoidsendMessage(StringsmtpHost,Stringfrom,Stringto,Stringsubject,StringmessageText)throwsMessagingException{
System.out.println("Configuringmailsessionfor:"+smtpHost);
SmtpAuthsa=newSmtpAuth();
sa.getuserinfo("jcren","mypassword");
java.util.Propertiesprops=newjava.util.Properties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host",smtpHost);
System.out.println("Constructingmessage-from="+from+"to="+to);
InternetAddressfromAddress=newInternetAddress(from);
InternetAddress[]toAddresss=newInternetAddress[3];
toAddresss[0]=newInternetAddress("jcren@163.com");
toAddresss[1]=newInternetAddress("xxxxx@163.com");
toAddresss[2]=newInternetAddress("ccccc@163.com");
inti=0;
while(i<toAddresss.length){
SessionmailSession=Session.getDefaultInstance(props,sa);
MimeMessagetestMessage=newMimeMessage(mailSession);
testMessage.setFrom(fromAddress);
testMessage.addRecipient(javax.mail.Message.RecipientType.TO,toAddresss[i]);
testMessage.setSentDate(newjava.util.Date());
testMessage.setSubject(subject);
testMessage.setText(messageText);
System.out.println("Messageconstructed");
Transport.send(testMessage);
System.out.println("Messagesent!");
i++;
}
}
publicstaticvoidmain(String[]args){
StringsmtpHost="smtp.163.com";
Stringfrom="jcren@163.com";
Stringto="jcren@163.com";
Stringsubject="Testmessage";
StringBuffertheMessage=newStringBuffer();
theMessage.append("ddddhello22,
");
theMessage.append("Hopealliswellonyouend.
");
theMessage.append("Cheers");
try{
MailBean.sendMessage(smtpHost,from,to,subject,theMessage.toString());
}catch(javax.mail.MessagingExceptionexc){
exc.printStackTrace();
}
}
staticclassSmtpAuthextendsjavax.mail.Authenticator{
privateStringuser,password;
publicvoidgetuserinfo(Stringgetuser,Stringgetpassword){
user=getuser;
password=getpassword;
}
protectedjavax.mail.PasswordAuthenticationgetPasswordAuthentication(){
returnnewjavax.mail.PasswordAuthentication(user,password);
}
}
}
JSP援用:
<%@pagecontentType="text/html;charset=gb2312"import="java.util.*,java.sql.*"%>
<jsp:useBeanid="mailbean"scope="page"class="fastfox.util.MailBean"/>
<%
Stringmailto=request.getParameter("cust_mail");
Stringmailfrom="jcren@163.com";
StringSubject="Subject";
StringContent=request.getParameter("content");
mailbean.sendMessage("smtp.163.com",mailfrom,mailto,Subject,Content);
%>
JAVA是一种可以撰写跨平台应用软件的面向对象的程序设计语言,由升阳(SunMicrosystems)公司的詹姆斯·高斯林(JamesGosling)等人于1990年代初开发。 |