javamail發信的連接SMTP驗證的問題

 MailBean.java 

package fastfox.util; 
import java.util.*; 
import java.text.SimpleDateFormat; 
import javax.mail.*; 
import javax.mail.internet.*; 
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("jcren@163.com"); 
toAddresss[1]=new InternetAddress("xxxxx@163.com"); 
toAddresss[2]=new InternetAddress("ccccc@163.com"); 
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="jcren@163.com"; 
String to="jcren@163.com"; 
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="jcren@163.com"; 
String Subject="Subject"; 
String Content=request.getParameter("content"); 
mailbean.sendMessage("smtp.163.com",mailfrom,mailto,Subject,Content); 
%> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值