pop.126.com
smtp.126.com
需要认证
设置好之后收发邮件都很正常,可用下面的代码就怎么都不行,真是奇了怪了!!
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.126.com");
props.put("mail.smtp.auth","true");
try {
Authenticator auth = new PopupAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
MimeMessage mimeMsg = new MimeMessage(mailSession);
mimeMsg.setFrom(new InternetAddress("XXX@126.com"));
mimeMsg.setRecipient(RecipientType.TO, new InternetAddress("XXX@126.com"));
mimeMsg.setText("Hello");
Transport transport = mailSession.getTransport("smtp");
transport.connect((String)props.get("mail.smtp.host"),"nnn","ppp");
transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
认证类是:
class PopupAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("nnn", "ppp");
}
}
帮我看看吧,高分相送
6 个解决方案
#1
顶,学习。
#2
给你发个正确的:
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import java.util.Date;
import javax.mail.Message.RecipientType;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.Session;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.Multipart;
import javax.mail.internet.MimeMultipart;
import org.apache.log4j.Logger;
/**
* 发送Email类
*/
public class SendEmail {
Logger logger=Logger.getLogger(this.getClass());
/**
* Email发送方法
* @param toemails 需要一个字符串参数,用来设置收件人地址,如果收件人为多个,则用","隔开
* @param content 邮件内容
* @param subject 邮件主题
* @return 邮件成功发送则返回true,否则返回false
*/
public boolean sendSimpleEmail(String toemails,String content,String subject){
boolean result=false;
logger.info("进入发送Email类");
try {
//创建属性对象
Properties props = new Properties();
//设置邮件传输协议为:smtp
props.put("mail.transpost.protocol","smtp");
//设置邮件服务器地址
props.put("mail.smtp.host", "smtp.163.com");
//设置邮件验证为真
props.put("mail.smtp.auth", "true");
//设置邮件服务器端口
props.put("mail.smtp.port","25");
//调用验证类进行验证
logger.info("验证类实例化==============");
CheckSendEmail auth=new CheckSendEmail("link2friend","lip009");
logger.info("验证通过");
//创建session对象
Session sendMailSession;
sendMailSession = Session.getInstance(props, auth);
//设置输出调试信息
sendMailSession.setDebug(true);
logger.info("开始创建消息对象");
//创建信息对象
Message newMessage = new MimeMessage(sendMailSession);
//输入发送信息
//设置发信人地址
logger.info("设置发信人地址");
newMessage.setFrom(new InternetAddress("link2friend@163.com"));
//设置收信人地址,只支持单用户发送
// newMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("lip0091981@163.com"));
//设置收信人地址,可以支持多用户发送
logger.info("设置收信人地址");
newMessage.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toemails));
//附件
//==============================================================================
//msgText是信件的正文,共有两行
// String msgText = content;
//msgAttachment是一段字符串作为附件内容
// String msgAttachment = "This is an attachment string!";
// MimeBodyPart mbp1 = new MimeBodyPart();
// mbp1.setText(msgText); //把前面定义的msgText中的文字设定为邮件正文的内容
//创建附件部分
// MimeBodyPart mbp2 = new MimeBodyPart();
//使用setText(text, charset)来加入附件
// mbp2.setText(msgAttachment, "gb2312");
//创建Multipart
// Multipart mp = new MimeMultipart();
// mp.addBodyPart(mbp1);
// mp.addBodyPart(mbp2);
// 添加 Multipart到Message中
// newMessage.setContent(mp);
//==============================================================================
//设置信件文本格式(当设置了附件,这里就不能有)
logger.info("设置格式");
newMessage.setContent("SendMail", "text/html");
//设置信件主题
logger.info("设置主题");
newMessage.setSubject(subject);
//设置信件发送日期
logger.info("设置发送日期");
newMessage.setSentDate(new Date());
//设置信件正文(当设置了附件,这里就不能有)
newMessage.setText(content);
logger.info("设置完消息");
//创建对象
Transport transport;
transport = sendMailSession.getTransport("smtp");
logger.info("将要发送");
//发送
result=true;
transport.send(newMessage);//此处总是抛出异常,让人很是郁闷,但是邮件却发送成功!
logger.info("恭喜你!您的邮件已经成功发送!");
}
catch (MessagingException ex) {
// ex.printStackTrace();
}
return result;
}
}
import javax.mail.*;
/**
* Email发送帐号密码验证类
*/
public class CheckSendEmail extends Authenticator{
private String m_username = null;
private String m_userpass = null;
public void setUsername(String username)
{
m_username = username;
}
public void setUserpass(String userpass)
{
m_userpass = userpass;
}
public CheckSendEmail(String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(m_username,m_userpass);
}
}
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import java.util.Date;
import javax.mail.Message.RecipientType;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.Session;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.Multipart;
import javax.mail.internet.MimeMultipart;
import org.apache.log4j.Logger;
/**
* 发送Email类
*/
public class SendEmail {
Logger logger=Logger.getLogger(this.getClass());
/**
* Email发送方法
* @param toemails 需要一个字符串参数,用来设置收件人地址,如果收件人为多个,则用","隔开
* @param content 邮件内容
* @param subject 邮件主题
* @return 邮件成功发送则返回true,否则返回false
*/
public boolean sendSimpleEmail(String toemails,String content,String subject){
boolean result=false;
logger.info("进入发送Email类");
try {
//创建属性对象
Properties props = new Properties();
//设置邮件传输协议为:smtp
props.put("mail.transpost.protocol","smtp");
//设置邮件服务器地址
props.put("mail.smtp.host", "smtp.163.com");
//设置邮件验证为真
props.put("mail.smtp.auth", "true");
//设置邮件服务器端口
props.put("mail.smtp.port","25");
//调用验证类进行验证
logger.info("验证类实例化==============");
CheckSendEmail auth=new CheckSendEmail("link2friend","lip009");
logger.info("验证通过");
//创建session对象
Session sendMailSession;
sendMailSession = Session.getInstance(props, auth);
//设置输出调试信息
sendMailSession.setDebug(true);
logger.info("开始创建消息对象");
//创建信息对象
Message newMessage = new MimeMessage(sendMailSession);
//输入发送信息
//设置发信人地址
logger.info("设置发信人地址");
newMessage.setFrom(new InternetAddress("link2friend@163.com"));
//设置收信人地址,只支持单用户发送
// newMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("lip0091981@163.com"));
//设置收信人地址,可以支持多用户发送
logger.info("设置收信人地址");
newMessage.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toemails));
//附件
//==============================================================================
//msgText是信件的正文,共有两行
// String msgText = content;
//msgAttachment是一段字符串作为附件内容
// String msgAttachment = "This is an attachment string!";
// MimeBodyPart mbp1 = new MimeBodyPart();
// mbp1.setText(msgText); //把前面定义的msgText中的文字设定为邮件正文的内容
//创建附件部分
// MimeBodyPart mbp2 = new MimeBodyPart();
//使用setText(text, charset)来加入附件
// mbp2.setText(msgAttachment, "gb2312");
//创建Multipart
// Multipart mp = new MimeMultipart();
// mp.addBodyPart(mbp1);
// mp.addBodyPart(mbp2);
// 添加 Multipart到Message中
// newMessage.setContent(mp);
//==============================================================================
//设置信件文本格式(当设置了附件,这里就不能有)
logger.info("设置格式");
newMessage.setContent("SendMail", "text/html");
//设置信件主题
logger.info("设置主题");
newMessage.setSubject(subject);
//设置信件发送日期
logger.info("设置发送日期");
newMessage.setSentDate(new Date());
//设置信件正文(当设置了附件,这里就不能有)
newMessage.setText(content);
logger.info("设置完消息");
//创建对象
Transport transport;
transport = sendMailSession.getTransport("smtp");
logger.info("将要发送");
//发送
result=true;
transport.send(newMessage);//此处总是抛出异常,让人很是郁闷,但是邮件却发送成功!
logger.info("恭喜你!您的邮件已经成功发送!");
}
catch (MessagingException ex) {
// ex.printStackTrace();
}
return result;
}
}
import javax.mail.*;
/**
* Email发送帐号密码验证类
*/
public class CheckSendEmail extends Authenticator{
private String m_username = null;
private String m_userpass = null;
public void setUsername(String username)
{
m_username = username;
}
public void setUserpass(String userpass)
{
m_userpass = userpass;
}
public CheckSendEmail(String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(m_username,m_userpass);
}
}
#3
mark
#4
谢谢
#5
我是敏敏,呵呵,有不懂的话群里叫我
<%@ page contentType="text/html;charset=gb2312" language="java" import="java.sql.*,java.io.*,java.util.*"%>
<%@ include file="include.inc"%>
<%@ page import="com.bwrn.page.Show"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%>
<%@ page import="javax.mail.*"%>
<%
request.setCharacterEncoding("GB2312");
Show show=new Show();
boolean blnError=true;//保留异常信息
boolean blnSend=false;//保留发送信息
String strTo="";
Class.forName(CLASSFORNAME);
Connection con=DriverManager.getConnection(SERVER,USER,PWD);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select addrees from foremail where username='aa'");
if(rs.next())
{
strTo=rs.getString(1);
}
InternetAddress[] address=null;
String strMailserver="smtp.163.com";
String [] arrstrFrom=request.getParameterValues("addrres");
String strSubject=request.getParameter("title");
String strMessage=request.getParameter("content");
String file=request.getParameter("file");
if(arrstrFrom.length>0)
{
int intI=0;
for(intI=0;intI<arrstrFrom.length;intI++)
{
if(!arrstrFrom[intI].equals(""))
{
try
{
//设定所要用的Mail服务器和所使用的传输协议
java.util.Properties props=System.getProperties();
props.put("mail.host",strMailserver);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.auth", "true");
//产生新的Session服务
javax.mail.Session mailSession=javax.mail.Session.getDefaultInstance(props,null);
mailSession.setDebug(true);
Message msg=new MimeMessage(mailSession);
//设定传送邮件的发信人
msg.setFrom(new InternetAddress(strTo));
address=InternetAddress.parse(arrstrFrom[intI],false);
msg.setRecipients(Message.RecipientType.TO,address);
//设定信中主题
msg.setSubject(strSubject);
//设定送信的时间
msg.setSentDate(new java.util.Date());
//设定送信的MIME Type
msg.setText(strMessage);
//设定信中附件;
msg.setFileName("file");
//送信
Transport transport;
transport = mailSession.getTransport("smtp");
transport.connect((String)props.get("smtp.163.com"),username,password);
transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));
System.out.println("abbcca");
out.print("The email has been succefully send to"+arrstrFrom[intI]+"<br>");
blnSend=true;
}
catch(Exception e)
{
if(blnError)
out.print(show.errorBox(e.getMessage(),"Bad send email"));
blnError=false;
e.printStackTrace();
}
}
}
}
else
{
out.print(show.errorBox("Please give more infomation","wrong infomation"));
}
if(blnSend)
{
out.print(show.errorBox("Succefully","Send Infomation"));
}
else if(blnError&!blnSend)
{
out.print(show.errorBox("Bad Send","Send Infomation"));
}
%>
<%@ page contentType="text/html;charset=gb2312" language="java" import="java.sql.*,java.io.*,java.util.*"%>
<%@ include file="include.inc"%>
<%@ page import="com.bwrn.page.Show"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%>
<%@ page import="javax.mail.*"%>
<%
request.setCharacterEncoding("GB2312");
Show show=new Show();
boolean blnError=true;//保留异常信息
boolean blnSend=false;//保留发送信息
String strTo="";
Class.forName(CLASSFORNAME);
Connection con=DriverManager.getConnection(SERVER,USER,PWD);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select addrees from foremail where username='aa'");
if(rs.next())
{
strTo=rs.getString(1);
}
InternetAddress[] address=null;
String strMailserver="smtp.163.com";
String [] arrstrFrom=request.getParameterValues("addrres");
String strSubject=request.getParameter("title");
String strMessage=request.getParameter("content");
String file=request.getParameter("file");
if(arrstrFrom.length>0)
{
int intI=0;
for(intI=0;intI<arrstrFrom.length;intI++)
{
if(!arrstrFrom[intI].equals(""))
{
try
{
//设定所要用的Mail服务器和所使用的传输协议
java.util.Properties props=System.getProperties();
props.put("mail.host",strMailserver);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.auth", "true");
//产生新的Session服务
javax.mail.Session mailSession=javax.mail.Session.getDefaultInstance(props,null);
mailSession.setDebug(true);
Message msg=new MimeMessage(mailSession);
//设定传送邮件的发信人
msg.setFrom(new InternetAddress(strTo));
address=InternetAddress.parse(arrstrFrom[intI],false);
msg.setRecipients(Message.RecipientType.TO,address);
//设定信中主题
msg.setSubject(strSubject);
//设定送信的时间
msg.setSentDate(new java.util.Date());
//设定送信的MIME Type
msg.setText(strMessage);
//设定信中附件;
msg.setFileName("file");
//送信
Transport transport;
transport = mailSession.getTransport("smtp");
transport.connect((String)props.get("smtp.163.com"),username,password);
transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));
System.out.println("abbcca");
out.print("The email has been succefully send to"+arrstrFrom[intI]+"<br>");
blnSend=true;
}
catch(Exception e)
{
if(blnError)
out.print(show.errorBox(e.getMessage(),"Bad send email"));
blnError=false;
e.printStackTrace();
}
}
}
}
else
{
out.print(show.errorBox("Please give more infomation","wrong infomation"));
}
if(blnSend)
{
out.print(show.errorBox("Succefully","Send Infomation"));
}
else if(blnError&!blnSend)
{
out.print(show.errorBox("Bad Send","Send Infomation"));
}
%>
#6
mark
#1
顶,学习。
#2
给你发个正确的:
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import java.util.Date;
import javax.mail.Message.RecipientType;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.Session;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.Multipart;
import javax.mail.internet.MimeMultipart;
import org.apache.log4j.Logger;
/**
* 发送Email类
*/
public class SendEmail {
Logger logger=Logger.getLogger(this.getClass());
/**
* Email发送方法
* @param toemails 需要一个字符串参数,用来设置收件人地址,如果收件人为多个,则用","隔开
* @param content 邮件内容
* @param subject 邮件主题
* @return 邮件成功发送则返回true,否则返回false
*/
public boolean sendSimpleEmail(String toemails,String content,String subject){
boolean result=false;
logger.info("进入发送Email类");
try {
//创建属性对象
Properties props = new Properties();
//设置邮件传输协议为:smtp
props.put("mail.transpost.protocol","smtp");
//设置邮件服务器地址
props.put("mail.smtp.host", "smtp.163.com");
//设置邮件验证为真
props.put("mail.smtp.auth", "true");
//设置邮件服务器端口
props.put("mail.smtp.port","25");
//调用验证类进行验证
logger.info("验证类实例化==============");
CheckSendEmail auth=new CheckSendEmail("link2friend","lip009");
logger.info("验证通过");
//创建session对象
Session sendMailSession;
sendMailSession = Session.getInstance(props, auth);
//设置输出调试信息
sendMailSession.setDebug(true);
logger.info("开始创建消息对象");
//创建信息对象
Message newMessage = new MimeMessage(sendMailSession);
//输入发送信息
//设置发信人地址
logger.info("设置发信人地址");
newMessage.setFrom(new InternetAddress("link2friend@163.com"));
//设置收信人地址,只支持单用户发送
// newMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("lip0091981@163.com"));
//设置收信人地址,可以支持多用户发送
logger.info("设置收信人地址");
newMessage.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toemails));
//附件
//==============================================================================
//msgText是信件的正文,共有两行
// String msgText = content;
//msgAttachment是一段字符串作为附件内容
// String msgAttachment = "This is an attachment string!";
// MimeBodyPart mbp1 = new MimeBodyPart();
// mbp1.setText(msgText); //把前面定义的msgText中的文字设定为邮件正文的内容
//创建附件部分
// MimeBodyPart mbp2 = new MimeBodyPart();
//使用setText(text, charset)来加入附件
// mbp2.setText(msgAttachment, "gb2312");
//创建Multipart
// Multipart mp = new MimeMultipart();
// mp.addBodyPart(mbp1);
// mp.addBodyPart(mbp2);
// 添加 Multipart到Message中
// newMessage.setContent(mp);
//==============================================================================
//设置信件文本格式(当设置了附件,这里就不能有)
logger.info("设置格式");
newMessage.setContent("SendMail", "text/html");
//设置信件主题
logger.info("设置主题");
newMessage.setSubject(subject);
//设置信件发送日期
logger.info("设置发送日期");
newMessage.setSentDate(new Date());
//设置信件正文(当设置了附件,这里就不能有)
newMessage.setText(content);
logger.info("设置完消息");
//创建对象
Transport transport;
transport = sendMailSession.getTransport("smtp");
logger.info("将要发送");
//发送
result=true;
transport.send(newMessage);//此处总是抛出异常,让人很是郁闷,但是邮件却发送成功!
logger.info("恭喜你!您的邮件已经成功发送!");
}
catch (MessagingException ex) {
// ex.printStackTrace();
}
return result;
}
}
import javax.mail.*;
/**
* Email发送帐号密码验证类
*/
public class CheckSendEmail extends Authenticator{
private String m_username = null;
private String m_userpass = null;
public void setUsername(String username)
{
m_username = username;
}
public void setUserpass(String userpass)
{
m_userpass = userpass;
}
public CheckSendEmail(String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(m_username,m_userpass);
}
}
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import java.util.Date;
import javax.mail.Message.RecipientType;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.Session;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.Multipart;
import javax.mail.internet.MimeMultipart;
import org.apache.log4j.Logger;
/**
* 发送Email类
*/
public class SendEmail {
Logger logger=Logger.getLogger(this.getClass());
/**
* Email发送方法
* @param toemails 需要一个字符串参数,用来设置收件人地址,如果收件人为多个,则用","隔开
* @param content 邮件内容
* @param subject 邮件主题
* @return 邮件成功发送则返回true,否则返回false
*/
public boolean sendSimpleEmail(String toemails,String content,String subject){
boolean result=false;
logger.info("进入发送Email类");
try {
//创建属性对象
Properties props = new Properties();
//设置邮件传输协议为:smtp
props.put("mail.transpost.protocol","smtp");
//设置邮件服务器地址
props.put("mail.smtp.host", "smtp.163.com");
//设置邮件验证为真
props.put("mail.smtp.auth", "true");
//设置邮件服务器端口
props.put("mail.smtp.port","25");
//调用验证类进行验证
logger.info("验证类实例化==============");
CheckSendEmail auth=new CheckSendEmail("link2friend","lip009");
logger.info("验证通过");
//创建session对象
Session sendMailSession;
sendMailSession = Session.getInstance(props, auth);
//设置输出调试信息
sendMailSession.setDebug(true);
logger.info("开始创建消息对象");
//创建信息对象
Message newMessage = new MimeMessage(sendMailSession);
//输入发送信息
//设置发信人地址
logger.info("设置发信人地址");
newMessage.setFrom(new InternetAddress("link2friend@163.com"));
//设置收信人地址,只支持单用户发送
// newMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("lip0091981@163.com"));
//设置收信人地址,可以支持多用户发送
logger.info("设置收信人地址");
newMessage.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toemails));
//附件
//==============================================================================
//msgText是信件的正文,共有两行
// String msgText = content;
//msgAttachment是一段字符串作为附件内容
// String msgAttachment = "This is an attachment string!";
// MimeBodyPart mbp1 = new MimeBodyPart();
// mbp1.setText(msgText); //把前面定义的msgText中的文字设定为邮件正文的内容
//创建附件部分
// MimeBodyPart mbp2 = new MimeBodyPart();
//使用setText(text, charset)来加入附件
// mbp2.setText(msgAttachment, "gb2312");
//创建Multipart
// Multipart mp = new MimeMultipart();
// mp.addBodyPart(mbp1);
// mp.addBodyPart(mbp2);
// 添加 Multipart到Message中
// newMessage.setContent(mp);
//==============================================================================
//设置信件文本格式(当设置了附件,这里就不能有)
logger.info("设置格式");
newMessage.setContent("SendMail", "text/html");
//设置信件主题
logger.info("设置主题");
newMessage.setSubject(subject);
//设置信件发送日期
logger.info("设置发送日期");
newMessage.setSentDate(new Date());
//设置信件正文(当设置了附件,这里就不能有)
newMessage.setText(content);
logger.info("设置完消息");
//创建对象
Transport transport;
transport = sendMailSession.getTransport("smtp");
logger.info("将要发送");
//发送
result=true;
transport.send(newMessage);//此处总是抛出异常,让人很是郁闷,但是邮件却发送成功!
logger.info("恭喜你!您的邮件已经成功发送!");
}
catch (MessagingException ex) {
// ex.printStackTrace();
}
return result;
}
}
import javax.mail.*;
/**
* Email发送帐号密码验证类
*/
public class CheckSendEmail extends Authenticator{
private String m_username = null;
private String m_userpass = null;
public void setUsername(String username)
{
m_username = username;
}
public void setUserpass(String userpass)
{
m_userpass = userpass;
}
public CheckSendEmail(String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(m_username,m_userpass);
}
}
#3
mark
#4
谢谢
#5
我是敏敏,呵呵,有不懂的话群里叫我
<%@ page contentType="text/html;charset=gb2312" language="java" import="java.sql.*,java.io.*,java.util.*"%>
<%@ include file="include.inc"%>
<%@ page import="com.bwrn.page.Show"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%>
<%@ page import="javax.mail.*"%>
<%
request.setCharacterEncoding("GB2312");
Show show=new Show();
boolean blnError=true;//保留异常信息
boolean blnSend=false;//保留发送信息
String strTo="";
Class.forName(CLASSFORNAME);
Connection con=DriverManager.getConnection(SERVER,USER,PWD);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select addrees from foremail where username='aa'");
if(rs.next())
{
strTo=rs.getString(1);
}
InternetAddress[] address=null;
String strMailserver="smtp.163.com";
String [] arrstrFrom=request.getParameterValues("addrres");
String strSubject=request.getParameter("title");
String strMessage=request.getParameter("content");
String file=request.getParameter("file");
if(arrstrFrom.length>0)
{
int intI=0;
for(intI=0;intI<arrstrFrom.length;intI++)
{
if(!arrstrFrom[intI].equals(""))
{
try
{
//设定所要用的Mail服务器和所使用的传输协议
java.util.Properties props=System.getProperties();
props.put("mail.host",strMailserver);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.auth", "true");
//产生新的Session服务
javax.mail.Session mailSession=javax.mail.Session.getDefaultInstance(props,null);
mailSession.setDebug(true);
Message msg=new MimeMessage(mailSession);
//设定传送邮件的发信人
msg.setFrom(new InternetAddress(strTo));
address=InternetAddress.parse(arrstrFrom[intI],false);
msg.setRecipients(Message.RecipientType.TO,address);
//设定信中主题
msg.setSubject(strSubject);
//设定送信的时间
msg.setSentDate(new java.util.Date());
//设定送信的MIME Type
msg.setText(strMessage);
//设定信中附件;
msg.setFileName("file");
//送信
Transport transport;
transport = mailSession.getTransport("smtp");
transport.connect((String)props.get("smtp.163.com"),username,password);
transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));
System.out.println("abbcca");
out.print("The email has been succefully send to"+arrstrFrom[intI]+"<br>");
blnSend=true;
}
catch(Exception e)
{
if(blnError)
out.print(show.errorBox(e.getMessage(),"Bad send email"));
blnError=false;
e.printStackTrace();
}
}
}
}
else
{
out.print(show.errorBox("Please give more infomation","wrong infomation"));
}
if(blnSend)
{
out.print(show.errorBox("Succefully","Send Infomation"));
}
else if(blnError&!blnSend)
{
out.print(show.errorBox("Bad Send","Send Infomation"));
}
%>
<%@ page contentType="text/html;charset=gb2312" language="java" import="java.sql.*,java.io.*,java.util.*"%>
<%@ include file="include.inc"%>
<%@ page import="com.bwrn.page.Show"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%>
<%@ page import="javax.mail.*"%>
<%
request.setCharacterEncoding("GB2312");
Show show=new Show();
boolean blnError=true;//保留异常信息
boolean blnSend=false;//保留发送信息
String strTo="";
Class.forName(CLASSFORNAME);
Connection con=DriverManager.getConnection(SERVER,USER,PWD);
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select addrees from foremail where username='aa'");
if(rs.next())
{
strTo=rs.getString(1);
}
InternetAddress[] address=null;
String strMailserver="smtp.163.com";
String [] arrstrFrom=request.getParameterValues("addrres");
String strSubject=request.getParameter("title");
String strMessage=request.getParameter("content");
String file=request.getParameter("file");
if(arrstrFrom.length>0)
{
int intI=0;
for(intI=0;intI<arrstrFrom.length;intI++)
{
if(!arrstrFrom[intI].equals(""))
{
try
{
//设定所要用的Mail服务器和所使用的传输协议
java.util.Properties props=System.getProperties();
props.put("mail.host",strMailserver);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.auth", "true");
//产生新的Session服务
javax.mail.Session mailSession=javax.mail.Session.getDefaultInstance(props,null);
mailSession.setDebug(true);
Message msg=new MimeMessage(mailSession);
//设定传送邮件的发信人
msg.setFrom(new InternetAddress(strTo));
address=InternetAddress.parse(arrstrFrom[intI],false);
msg.setRecipients(Message.RecipientType.TO,address);
//设定信中主题
msg.setSubject(strSubject);
//设定送信的时间
msg.setSentDate(new java.util.Date());
//设定送信的MIME Type
msg.setText(strMessage);
//设定信中附件;
msg.setFileName("file");
//送信
Transport transport;
transport = mailSession.getTransport("smtp");
transport.connect((String)props.get("smtp.163.com"),username,password);
transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));
System.out.println("abbcca");
out.print("The email has been succefully send to"+arrstrFrom[intI]+"<br>");
blnSend=true;
}
catch(Exception e)
{
if(blnError)
out.print(show.errorBox(e.getMessage(),"Bad send email"));
blnError=false;
e.printStackTrace();
}
}
}
}
else
{
out.print(show.errorBox("Please give more infomation","wrong infomation"));
}
if(blnSend)
{
out.print(show.errorBox("Succefully","Send Infomation"));
}
else if(blnError&!blnSend)
{
out.print(show.errorBox("Bad Send","Send Infomation"));
}
%>
#6
mark