• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

javamail发信的连接SMTP验证的问题

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 24次 | 进入软件测试论坛讨论

领测软件测试网 MailBean.java 

package fastfox.util; 
import java.util.*; 
import java.text.SimpleDateFormat; 
import javax.mail.*; 
import javax.mail.inte.net.*; 
import javax.activation.*; 
import java.io.*; 
public class MailBean { 


public static void sendMessage(String smtpHost,String from,String to,String subject,String messageText)throws MessagingException{ 
System.out.println("Configuring mail session for:"+smtpHost); 
SmtpAuth sa=new SmtpAuth(); 
sa.getuserinfo("jcren","mypassword"); 
java.util.Properties props=new java.util.Properties(); 
props.put("mail.smtp.auth","true"); 
props.put("mail.smtp.host",smtpHost); 
System.out.println("Constructing message- from="+from+" to="+to); 
InternetAddress fromAddress=new InternetAddress(from); 
InternetAddress[] toAddresss=new InternetAddress[3]; 
toAddresss[0]=new InternetAddress("[email protected]"); 
toAddresss[1]=new InternetAddress("[email protected]"); 
toAddresss[2]=new InternetAddress("[email protected]"); 
int i=0; 
while(i<toAddresss.length){ 
Session mailSession=Session.getDefaultInstance(props,sa); 
MimeMessage testMessage=new MimeMessage(mailSession); 
testMessage.setFrom(fromAddress); 
testMessage.addRecipient(javax.mail.Message.RecipientType.TO,toAddresss[i]); 
testMessage.setSentDate(new java.util.Date()); 
testMessage.setSubject(subject); 
testMessage.setText(messageText); 
System.out.println("Message constructed"); 

Transport.send(testMessage); 
System.out.println("Message sent!"); 
i++; 
} 
} 

public static void main(String[] args){ 
String smtpHost="smtp.163.com"; 
String from="[email protected]"; 
String to="[email protected]"; 
String subject="Test message"; 
StringBuffer theMessage=new StringBuffer(); 
theMessage.append("ddddhello22,\n\n"); 
theMessage.append("Hope all is well on you end.\n"); 
theMessage.append("Cheers"); 
try{ 
MailBean.sendMessage(smtpHost,from,to,subject,theMessage.toString()); 
}catch(javax.mail.MessagingException exc){ 
exc.printStackTrace(); 
} 
} 

static class SmtpAuth extends javax.mail.Authenticator { 
private String user,password; 

public void getuserinfo(String getuser,String getpassword){ 
user = getuser; 
password = getpassword; 
} 
protected javax.mail.PasswordAuthentication getPasswordAuthentication(){ 
return new javax.mail.PasswordAuthentication(user,password); 
} 
} 
} 
JSP引用: 
<%@ page contentType="text/html;charset=gb2312" import="java.util.*,java.sql.*" %> 
<jsp:useBean id="mailbean" scope="page" class="fastfox.util.MailBean" /> 
<% 
String mailto=request.getParameter("cust_mail"); 
String mailfrom="[email protected]"; 
String Subject="Subject"; 
String Content=request.getParameter("content"); 
mailbean.sendMessage("smtp.163.com",mailfrom,mailto,Subject,Content); 
%>

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网