You are not authorized to send mail as <MAIL FROM:<hanbaoju@163.com>>, authentication is required
QUIT
Sending failed;
nested exception is:
class javax.mail.MessagingException: 553 You are not authorized to send mail as <MAIL FROM:<hanbaoju@163.com>>, authentication is required
这应该怎样解决?
我在向公司邮件服务器发信时并不需要进行认证阿??
9 个解决方案
#1
網上有認證的代碼 ̄!找一下就有的了
#2
以下摘自网上:
先建两个类,AuthenticatorUtil.java和EMail.java;
调用方法:new Email().sendMail(接收邮箱地址,邮件主题,邮件体内容)
AuthenticatorUtil.java清单:
package p1;
import javax.mail.PasswordAuthentication;
//当smtp需要验证时所需要用到的验证类
class AuthenticatorUtil
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public AuthenticatorUtil(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
EMail.java清单:
package p1;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import p1.AuthenticatorUtil;
public class EMail{
public EMail() {}
//此方法执行邮件发送的操作
//mailTo:目标邮件地址
//mailSubject:邮件主题
//mailBody:邮件体,在这里是邮件正文
public boolean sendMail(String mailTo,String mailSubject,String mailBody){
try {
//初始化所有必要的参数
String smtpServer = "smtp.bbbb.com";//smtp服务器
String smtpAuth = "true";//smtp服务器是否需要进行验证
String smtpUser = "uuuuu";//smtp分配的用户名(就是你的E-Mail地址中@符号前面部分)
String smtpPassword = "pppp";//登陆口令
String from = "aaaa@bbbb.com";//用于在对方收到的邮件中作为发送人地址显示
String to = mailTo;//邮件需要发送到的目标邮件地址
String subject = mailSubject;//邮件主题
String text = mailBody;//邮件体,在这里是邮件正文
Properties props = new Properties();//构造Properties,作为后面的Session参数容器
Session sendMailSession;
Transport transport;
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth", smtpAuth);
if ("true".equals(smtpAuth)) {
//如果smtp服务器需要验证,则构建AuthertiactorUtil用于mail session的创建
AuthenticatorUtil authutil = new AuthenticatorUtil(smtpUser, smtpPassword);
sendMailSession = Session.getInstance(props, authutil);
}
else {
sendMailSession = Session.getInstance(props);
}
//设置是否是调试状态,如果这里设置为true的话,则会在控制台输出发送过程,输出信息类似于在telnet上发送邮件
sendMailSession.setDebug(true);
//初始化邮件消息
Message message = new MimeMessage(sendMailSession);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(mailTo));
message.setSubject(subject);
message.setSentDate(new Date());
message.setText(text);
message.saveChanges();
transport = sendMailSession.getTransport("smtp");//此次会话是发送邮件(smtp)
transport.send(message, message.getAllRecipients());//从这里可以看出来,可以设置多个发送目的地址
transport.close();
}catch (Exception e) {
System.err.println("邮件发送失败:" + e.getMessage());
return false;
}
return true;
}
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
先建两个类,AuthenticatorUtil.java和EMail.java;
调用方法:new Email().sendMail(接收邮箱地址,邮件主题,邮件体内容)
AuthenticatorUtil.java清单:
package p1;
import javax.mail.PasswordAuthentication;
//当smtp需要验证时所需要用到的验证类
class AuthenticatorUtil
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public AuthenticatorUtil(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
EMail.java清单:
package p1;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import p1.AuthenticatorUtil;
public class EMail{
public EMail() {}
//此方法执行邮件发送的操作
//mailTo:目标邮件地址
//mailSubject:邮件主题
//mailBody:邮件体,在这里是邮件正文
public boolean sendMail(String mailTo,String mailSubject,String mailBody){
try {
//初始化所有必要的参数
String smtpServer = "smtp.bbbb.com";//smtp服务器
String smtpAuth = "true";//smtp服务器是否需要进行验证
String smtpUser = "uuuuu";//smtp分配的用户名(就是你的E-Mail地址中@符号前面部分)
String smtpPassword = "pppp";//登陆口令
String from = "aaaa@bbbb.com";//用于在对方收到的邮件中作为发送人地址显示
String to = mailTo;//邮件需要发送到的目标邮件地址
String subject = mailSubject;//邮件主题
String text = mailBody;//邮件体,在这里是邮件正文
Properties props = new Properties();//构造Properties,作为后面的Session参数容器
Session sendMailSession;
Transport transport;
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth", smtpAuth);
if ("true".equals(smtpAuth)) {
//如果smtp服务器需要验证,则构建AuthertiactorUtil用于mail session的创建
AuthenticatorUtil authutil = new AuthenticatorUtil(smtpUser, smtpPassword);
sendMailSession = Session.getInstance(props, authutil);
}
else {
sendMailSession = Session.getInstance(props);
}
//设置是否是调试状态,如果这里设置为true的话,则会在控制台输出发送过程,输出信息类似于在telnet上发送邮件
sendMailSession.setDebug(true);
//初始化邮件消息
Message message = new MimeMessage(sendMailSession);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(mailTo));
message.setSubject(subject);
message.setSentDate(new Date());
message.setText(text);
message.saveChanges();
transport = sendMailSession.getTransport("smtp");//此次会话是发送邮件(smtp)
transport.send(message, message.getAllRecipients());//从这里可以看出来,可以设置多个发送目的地址
transport.close();
}catch (Exception e) {
System.err.println("邮件发送失败:" + e.getMessage());
return false;
}
return true;
}
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
#3
现在的smtp服务器基本上都需要验证,网上很多javamail验证的例子,你可以找找
#4
首先从SUN的网站上下2个包,很小,这2个包没有被包含在JSDK中……
JavaMail类文件包和JAF(JavaBeans Activation Framework )
一、导入包
分别解压缩你下载的2个包,随便放哪里无所谓。我是放在JSDK文件夹下。然后分别找到mail.jar和activation.jar这2个包,复制下这2个包到 H:\jdk1.4\jre\lib\ext。如果你照着里面的README做,会报错的。
二、编写代码
2个页面(简单点,说明问题就行)
表单:用来填写一些基本信息,发信者邮箱、收信者信箱、标题、邮件内容。
JSP处理页面:用来发信。
代码如下:(表单)
<HTML>
<BODY>
<FORM action="sendmail.jsp" method="post">
<TABLE align="center">
<TR>
<TD width="50%">
To:<BR><INPUT name="to" size="25">
</TD>
<TD width="50%">
From:<BR><INPUT name="from" size="25">
</TD>
</TR>
<TR>
<TD colspan="2">
Subject:<BR><INPUT name="subject" size="50">
</TD>
</TR>
<TR>
<TD colspan="2">
Message:<BR><TEXTAREA name="text" rows=25 cols=85></TEXTAREA>
</TD>
</TR>
</TABLE>
<INPUT type="submit" name="cb_submit" value=" Send ">
<INPUT type="reset" name="cb_reset" value=" Clear ">
</FORM>
</BODY>
</HTML>
JSP页面代码:
%@ page
import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"
%>
<html>
<head>
<TITLE>JSP meets JavaMail, what a sweet combo.</TITLE>
</HEAD>
<BODY>
<%
try{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
String usr="××";//这里填写你发信者的邮箱地址
String pwd="××";//这里填写你发信者的邮箱密码
props.put("mail.smtp.host","smtp.××.com"); //这里填写你发信者的SMTP主机,如:smtp.sohu.com
props.put("mail.smtp.user",usr);
props.put("mail.smtp.password",pwd);
props.put("mail.smtp.auth","true");
sendMailSession = Session.getInstance(props,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("××", "××"); }//这里填写你发信者的邮箱地址和密码
});//关键是这句,网上文档几乎没有,我查了大半天才查出来。如果你的邮箱是SMTP验证的,就得这么写。否则会报错。Session.getInstance(props)这个方法是针对SMTP不要求验证的,我的邮箱要验证,所以得这么写。
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(request.getParameter("from")));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to")));
newMessage.setSubject(request.getParameter("subject"));
newMessage.setSentDate(new Date());
newMessage.setText(request.getParameter("text"));
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
%>
<P>Your mail has been sent.</P>
<%
}
catch(MessagingException m)
{
out.println(m.toString());
}
%>
</BODY>
</HTML>
三、调试
开启TOMCAT,调试一下就可以了。
四、注意点
在表单填写的发信者的邮箱地址千万记住要和JSP页面里的对应,否则会报错!
JavaMail类文件包和JAF(JavaBeans Activation Framework )
一、导入包
分别解压缩你下载的2个包,随便放哪里无所谓。我是放在JSDK文件夹下。然后分别找到mail.jar和activation.jar这2个包,复制下这2个包到 H:\jdk1.4\jre\lib\ext。如果你照着里面的README做,会报错的。
二、编写代码
2个页面(简单点,说明问题就行)
表单:用来填写一些基本信息,发信者邮箱、收信者信箱、标题、邮件内容。
JSP处理页面:用来发信。
代码如下:(表单)
<HTML>
<BODY>
<FORM action="sendmail.jsp" method="post">
<TABLE align="center">
<TR>
<TD width="50%">
To:<BR><INPUT name="to" size="25">
</TD>
<TD width="50%">
From:<BR><INPUT name="from" size="25">
</TD>
</TR>
<TR>
<TD colspan="2">
Subject:<BR><INPUT name="subject" size="50">
</TD>
</TR>
<TR>
<TD colspan="2">
Message:<BR><TEXTAREA name="text" rows=25 cols=85></TEXTAREA>
</TD>
</TR>
</TABLE>
<INPUT type="submit" name="cb_submit" value=" Send ">
<INPUT type="reset" name="cb_reset" value=" Clear ">
</FORM>
</BODY>
</HTML>
JSP页面代码:
%@ page
import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"
%>
<html>
<head>
<TITLE>JSP meets JavaMail, what a sweet combo.</TITLE>
</HEAD>
<BODY>
<%
try{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
String usr="××";//这里填写你发信者的邮箱地址
String pwd="××";//这里填写你发信者的邮箱密码
props.put("mail.smtp.host","smtp.××.com"); //这里填写你发信者的SMTP主机,如:smtp.sohu.com
props.put("mail.smtp.user",usr);
props.put("mail.smtp.password",pwd);
props.put("mail.smtp.auth","true");
sendMailSession = Session.getInstance(props,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("××", "××"); }//这里填写你发信者的邮箱地址和密码
});//关键是这句,网上文档几乎没有,我查了大半天才查出来。如果你的邮箱是SMTP验证的,就得这么写。否则会报错。Session.getInstance(props)这个方法是针对SMTP不要求验证的,我的邮箱要验证,所以得这么写。
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(request.getParameter("from")));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to")));
newMessage.setSubject(request.getParameter("subject"));
newMessage.setSentDate(new Date());
newMessage.setText(request.getParameter("text"));
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
%>
<P>Your mail has been sent.</P>
<%
}
catch(MessagingException m)
{
out.println(m.toString());
}
%>
</BODY>
</HTML>
三、调试
开启TOMCAT,调试一下就可以了。
四、注意点
在表单填写的发信者的邮箱地址千万记住要和JSP页面里的对应,否则会报错!
#5
首先感谢xiaohuasz和mascotzhuang(基督山伯爵)
主要是增加一个Authenticator类的子类,并覆盖
protected PasswordAuthentication getPasswordAuthentication()方法,对吗?
我两个都试了一下,结果不行,下面是我按xiaohuasz方法写的:
1
class AuthenticatorUtil
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public AuthenticatorUtil(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
2
AuthenticatorUtil authutil = new AuthenticatorUtil("hanbaoju", "123456");
Properties props = new Properties();
rops.put("mail.smtp.auth","true");
props.put("mail.smtp.host","smtp.163.com");
session = Session.getDefaultInstance(props, authutil);
3 出现异常提示:
DEBUG: setDebug: JavaMail version 1.3.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
Sending failed;
nested exception is:
class javax.mail.AuthenticationFailedException
我什么地方写得不对??
谢谢!!
主要是增加一个Authenticator类的子类,并覆盖
protected PasswordAuthentication getPasswordAuthentication()方法,对吗?
我两个都试了一下,结果不行,下面是我按xiaohuasz方法写的:
1
class AuthenticatorUtil
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public AuthenticatorUtil(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
2
AuthenticatorUtil authutil = new AuthenticatorUtil("hanbaoju", "123456");
Properties props = new Properties();
rops.put("mail.smtp.auth","true");
props.put("mail.smtp.host","smtp.163.com");
session = Session.getDefaultInstance(props, authutil);
3 出现异常提示:
DEBUG: setDebug: JavaMail version 1.3.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
Sending failed;
nested exception is:
class javax.mail.AuthenticationFailedException
我什么地方写得不对??
谢谢!!
#6
你给出的代码应该没有问题
确认帐号和密码是否正确
确认帐号和密码是否正确
#7
我可以给263.net发邮件,也有认证,但给163发邮件就会出现
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class com.sun.mail.smtp.SMTPAddressFailedException: 554 <linuxlong@163.com>: Recipient address rejected: Relay access denied
~~~~~~~~~~~~~~~
借人气同问
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class com.sun.mail.smtp.SMTPAddressFailedException: 554 <linuxlong@163.com>: Recipient address rejected: Relay access denied
~~~~~~~~~~~~~~~
借人气同问
#8
给你我的程序看看,直接永久可以了(包自己下载吧):
html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>撰写邮件</title>
</head>
<body>
<form action="testall.jsp" method="post" name="form1">
<table width="424" border="0" align="center" cellspacing="1" bgcolor="#006600" class="black" height="287">
<tr bgcolor="#FFFFFF">
<td width="102" height="29"><font face="宋体"><font size="3">收信人</font>:</font></td>
<td width="308" height="23"> <input name="to1" type="text" id="to1" size="19"><font size="2"><i><font face="Basemic Times">@
</font></i></font><input name="to2" type="text" id="to2" size="17"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="12"><font size="3"><font face="宋体">寄信人</font></font><font face="宋体">:</font></td>
<td width="308" height="23"> <input name="from1" type="text" id="from1" size="19"><i><font face="Basemic Times" size="2">@
</font></i><input name="from2" type="text" id="from2" size="17"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="14"><font size="3"><font face="宋体">用户名</font></font><font face="宋体">:</font></td>
<td width="308" height="25"> <input name="yhm" type="text" id="yhm" size="41">
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="14"><font size="3"><font face="宋体">密 码</font></font><font face="宋体">:</font></td>
<td width="308" height="25"> <input name="mm" type="text" id="mm" size="41">
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="25"><font size="3" face="宋体">主 题:</font></td>
<td width="308" height="25"> <input name="title" type="text" id="title" size="41"></td>
</tr>
<tr>
<td height="25" colspan="2" bgcolor="#FFFFFF" width="401"><font size="3"><font face="宋体">信件类型</font>:
</font><select name="emailtype" id="emailtype" size="1">
<option value="text/plain">Text</option>
<option value="text/html">Html</option>
<option selected>-Select-</option>
</select></td>
</tr>
<tr>
<td height="63" colspan="2" bgcolor="#FFFFFF" width="401"><textarea name="content" cols="56" rows="8" id="content"></textarea></td>
</tr>
<tr align="center" valign="bottom">
<td colspan="2" bgcolor="#FFFFFF" width="401" height="33">
<p align="left"><font size="3">附件(本地):</font>
<input name="fj2" type="file" id="fj2" size="47"></p>
</td>
</tr>
<tr align="center">
<td colspan="2" bgcolor="#FFFFFF" width="401" height="11"> <input type="submit" name="Submit" value="发送">
<input type="reset" name="Submit2" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%request.setCharacterEncoding("gb2312");%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%><!--要发送附件必须引入该库-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>发送成功</title>
</head>
<body>
<%
try{
String tto1=request.getParameter("to1");
String tto2=request.getParameter("to2");
String tfrom1=request.getParameter("from1");
String tfrom2=request.getParameter("from2");
String tyhm=request.getParameter("yhm");
String tmm=request.getParameter("mm");
String ttitle=request.getParameter("title");
String emailtype=request.getParameter("emailtype");//获取email类型
String tcontent=request.getParameter("content");
//String tfj1=request.getParameter("fj1");
String tfj2=request.getParameter("fj2");
String tfj3=request.getParameter("fj3");
String smtptype="";
if(tfrom2.equals("163.com"))
{smtptype="smtp.163.com";
}//支持
else if(tfrom2.equals("126.com"))
{smtptype="smtp.126.com";
}//支持
else if(tfrom2.equals("21cn.com"))
{smtptype="smtp.21cn.com";
}//支持
else if(tfrom2.equals("yahoo.com.cn"))
{smtptype="smtp.mail.yahoo.com.cn";
}//支持
else if(tfrom2.equals("tom.com"))
{smtptype="smtp.tom.com";
}//支持
else if(tfrom2.equals("263.net"))
{smtptype="smtp.263.net";
}//未试
else if(tfrom2.equals("mail.china.com"))
{smtptype="mail.china.com";
}//不支持
else if(tfrom2.equals("sina.com"))
{smtptype="smtp.sina.com";
}//不支持
Properties props=new Properties();
props.put("mail.smtp.host",smtptype);
props.put("mail.smtp.auth","true");
Session s=Session.getInstance(props);
s.setDebug(true);
MimeMessage message=new MimeMessage(s);
//给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from=new InternetAddress(tfrom1+"@"+tfrom2,tfrom1);
message.setFrom(from);
InternetAddress to=new InternetAddress(tto1+"@"+tto2);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate(new Date());
Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放多个BodyPart对象
//设置信件文本内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent,emailtype+";charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
mm.addBodyPart(mdp);//将含有信件内容的BodyPart加入到MimeMultipart对象中
//设置信件的附件1(自定义附件:直接将所设文本内容加到自定义文件中作为附件发送)
//mdp=new MimeBodyPart();//新建一个存放附件的BodyPart
DataHandler dh=new DataHandler(tfj2,"text/plain;charset=gb2312");
//新建一个DataHandler对象,并设置其内容和格式/编码方式
//mdp.setFileName("text.txt");//加上这句将作为附件发送,否则将作为信件的文本内容
//mdp.setDataHandler(dh);//给BodyPart对象设置内容为dh
//mm.addBodyPart(mdp);//将含有附件的BodyPart加入到MimeMultipart对象中
//设置信件的附件2(用本地上的文件作为附件)
mdp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(tfj2);
dh=new DataHandler(fds);
int ddd=tfj2.lastIndexOf("\\");
String fname=tfj2.substring(ddd);//提取文件名
String ffname=new String(fname.getBytes("gb2312"),"ISO8859-1");//处理文件名是中文的情况
mdp.setFileName(ffname);//可以和原文件名不一致,但最好一样
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
//设置信件的附件3(用远程文件作为附件)
//mdp=new MimeBodyPart();
/*URL urlfj=new URL(tfj3);
URLDataSource ur=new URLDataSource(urlfj);
//注:这里用的参数只能为URL对象,不能为URL字串,在前面类介绍时有误(请谅解),这里纠正一下.
dh=new DataHandler(ur);
int ttt=tfj3.lastIndexOf("/");
String urlname=tfj3.substring(ttt);
//String urlfname=new String(urlname.getBytes("gb2312"),"ISO8859-1");//不知怎么回事,这里不能处理中文问题
mdp.setFileName(urlname);
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
*/
message.setContent(mm);//把mm作为消息对象的内容
message.saveChanges();
Transport transport=s.getTransport("smtp");
transport.connect(smtptype,tyhm,tmm);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
<div align="center">
<p><font color="#FF6600">发送成功!</font></p>
<br>
<a href="m.html">再发一封</a> </p>
</div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>
html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>撰写邮件</title>
</head>
<body>
<form action="testall.jsp" method="post" name="form1">
<table width="424" border="0" align="center" cellspacing="1" bgcolor="#006600" class="black" height="287">
<tr bgcolor="#FFFFFF">
<td width="102" height="29"><font face="宋体"><font size="3">收信人</font>:</font></td>
<td width="308" height="23"> <input name="to1" type="text" id="to1" size="19"><font size="2"><i><font face="Basemic Times">@
</font></i></font><input name="to2" type="text" id="to2" size="17"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="12"><font size="3"><font face="宋体">寄信人</font></font><font face="宋体">:</font></td>
<td width="308" height="23"> <input name="from1" type="text" id="from1" size="19"><i><font face="Basemic Times" size="2">@
</font></i><input name="from2" type="text" id="from2" size="17"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="14"><font size="3"><font face="宋体">用户名</font></font><font face="宋体">:</font></td>
<td width="308" height="25"> <input name="yhm" type="text" id="yhm" size="41">
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="14"><font size="3"><font face="宋体">密 码</font></font><font face="宋体">:</font></td>
<td width="308" height="25"> <input name="mm" type="text" id="mm" size="41">
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="25"><font size="3" face="宋体">主 题:</font></td>
<td width="308" height="25"> <input name="title" type="text" id="title" size="41"></td>
</tr>
<tr>
<td height="25" colspan="2" bgcolor="#FFFFFF" width="401"><font size="3"><font face="宋体">信件类型</font>:
</font><select name="emailtype" id="emailtype" size="1">
<option value="text/plain">Text</option>
<option value="text/html">Html</option>
<option selected>-Select-</option>
</select></td>
</tr>
<tr>
<td height="63" colspan="2" bgcolor="#FFFFFF" width="401"><textarea name="content" cols="56" rows="8" id="content"></textarea></td>
</tr>
<tr align="center" valign="bottom">
<td colspan="2" bgcolor="#FFFFFF" width="401" height="33">
<p align="left"><font size="3">附件(本地):</font>
<input name="fj2" type="file" id="fj2" size="47"></p>
</td>
</tr>
<tr align="center">
<td colspan="2" bgcolor="#FFFFFF" width="401" height="11"> <input type="submit" name="Submit" value="发送">
<input type="reset" name="Submit2" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%request.setCharacterEncoding("gb2312");%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%><!--要发送附件必须引入该库-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>发送成功</title>
</head>
<body>
<%
try{
String tto1=request.getParameter("to1");
String tto2=request.getParameter("to2");
String tfrom1=request.getParameter("from1");
String tfrom2=request.getParameter("from2");
String tyhm=request.getParameter("yhm");
String tmm=request.getParameter("mm");
String ttitle=request.getParameter("title");
String emailtype=request.getParameter("emailtype");//获取email类型
String tcontent=request.getParameter("content");
//String tfj1=request.getParameter("fj1");
String tfj2=request.getParameter("fj2");
String tfj3=request.getParameter("fj3");
String smtptype="";
if(tfrom2.equals("163.com"))
{smtptype="smtp.163.com";
}//支持
else if(tfrom2.equals("126.com"))
{smtptype="smtp.126.com";
}//支持
else if(tfrom2.equals("21cn.com"))
{smtptype="smtp.21cn.com";
}//支持
else if(tfrom2.equals("yahoo.com.cn"))
{smtptype="smtp.mail.yahoo.com.cn";
}//支持
else if(tfrom2.equals("tom.com"))
{smtptype="smtp.tom.com";
}//支持
else if(tfrom2.equals("263.net"))
{smtptype="smtp.263.net";
}//未试
else if(tfrom2.equals("mail.china.com"))
{smtptype="mail.china.com";
}//不支持
else if(tfrom2.equals("sina.com"))
{smtptype="smtp.sina.com";
}//不支持
Properties props=new Properties();
props.put("mail.smtp.host",smtptype);
props.put("mail.smtp.auth","true");
Session s=Session.getInstance(props);
s.setDebug(true);
MimeMessage message=new MimeMessage(s);
//给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from=new InternetAddress(tfrom1+"@"+tfrom2,tfrom1);
message.setFrom(from);
InternetAddress to=new InternetAddress(tto1+"@"+tto2);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate(new Date());
Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放多个BodyPart对象
//设置信件文本内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent,emailtype+";charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
mm.addBodyPart(mdp);//将含有信件内容的BodyPart加入到MimeMultipart对象中
//设置信件的附件1(自定义附件:直接将所设文本内容加到自定义文件中作为附件发送)
//mdp=new MimeBodyPart();//新建一个存放附件的BodyPart
DataHandler dh=new DataHandler(tfj2,"text/plain;charset=gb2312");
//新建一个DataHandler对象,并设置其内容和格式/编码方式
//mdp.setFileName("text.txt");//加上这句将作为附件发送,否则将作为信件的文本内容
//mdp.setDataHandler(dh);//给BodyPart对象设置内容为dh
//mm.addBodyPart(mdp);//将含有附件的BodyPart加入到MimeMultipart对象中
//设置信件的附件2(用本地上的文件作为附件)
mdp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(tfj2);
dh=new DataHandler(fds);
int ddd=tfj2.lastIndexOf("\\");
String fname=tfj2.substring(ddd);//提取文件名
String ffname=new String(fname.getBytes("gb2312"),"ISO8859-1");//处理文件名是中文的情况
mdp.setFileName(ffname);//可以和原文件名不一致,但最好一样
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
//设置信件的附件3(用远程文件作为附件)
//mdp=new MimeBodyPart();
/*URL urlfj=new URL(tfj3);
URLDataSource ur=new URLDataSource(urlfj);
//注:这里用的参数只能为URL对象,不能为URL字串,在前面类介绍时有误(请谅解),这里纠正一下.
dh=new DataHandler(ur);
int ttt=tfj3.lastIndexOf("/");
String urlname=tfj3.substring(ttt);
//String urlfname=new String(urlname.getBytes("gb2312"),"ISO8859-1");//不知怎么回事,这里不能处理中文问题
mdp.setFileName(urlname);
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
*/
message.setContent(mm);//把mm作为消息对象的内容
message.saveChanges();
Transport transport=s.getTransport("smtp");
transport.connect(smtptype,tyhm,tmm);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
<div align="center">
<p><font color="#FF6600">发送成功!</font></p>
<br>
<a href="m.html">再发一封</a> </p>
</div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>
#9
自己写mail网上例子很多,95%邮件服务器以上都需密码验证,我试过163邮件,必须要密码,上传最大48兆
#1
網上有認證的代碼 ̄!找一下就有的了
#2
以下摘自网上:
先建两个类,AuthenticatorUtil.java和EMail.java;
调用方法:new Email().sendMail(接收邮箱地址,邮件主题,邮件体内容)
AuthenticatorUtil.java清单:
package p1;
import javax.mail.PasswordAuthentication;
//当smtp需要验证时所需要用到的验证类
class AuthenticatorUtil
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public AuthenticatorUtil(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
EMail.java清单:
package p1;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import p1.AuthenticatorUtil;
public class EMail{
public EMail() {}
//此方法执行邮件发送的操作
//mailTo:目标邮件地址
//mailSubject:邮件主题
//mailBody:邮件体,在这里是邮件正文
public boolean sendMail(String mailTo,String mailSubject,String mailBody){
try {
//初始化所有必要的参数
String smtpServer = "smtp.bbbb.com";//smtp服务器
String smtpAuth = "true";//smtp服务器是否需要进行验证
String smtpUser = "uuuuu";//smtp分配的用户名(就是你的E-Mail地址中@符号前面部分)
String smtpPassword = "pppp";//登陆口令
String from = "aaaa@bbbb.com";//用于在对方收到的邮件中作为发送人地址显示
String to = mailTo;//邮件需要发送到的目标邮件地址
String subject = mailSubject;//邮件主题
String text = mailBody;//邮件体,在这里是邮件正文
Properties props = new Properties();//构造Properties,作为后面的Session参数容器
Session sendMailSession;
Transport transport;
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth", smtpAuth);
if ("true".equals(smtpAuth)) {
//如果smtp服务器需要验证,则构建AuthertiactorUtil用于mail session的创建
AuthenticatorUtil authutil = new AuthenticatorUtil(smtpUser, smtpPassword);
sendMailSession = Session.getInstance(props, authutil);
}
else {
sendMailSession = Session.getInstance(props);
}
//设置是否是调试状态,如果这里设置为true的话,则会在控制台输出发送过程,输出信息类似于在telnet上发送邮件
sendMailSession.setDebug(true);
//初始化邮件消息
Message message = new MimeMessage(sendMailSession);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(mailTo));
message.setSubject(subject);
message.setSentDate(new Date());
message.setText(text);
message.saveChanges();
transport = sendMailSession.getTransport("smtp");//此次会话是发送邮件(smtp)
transport.send(message, message.getAllRecipients());//从这里可以看出来,可以设置多个发送目的地址
transport.close();
}catch (Exception e) {
System.err.println("邮件发送失败:" + e.getMessage());
return false;
}
return true;
}
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
先建两个类,AuthenticatorUtil.java和EMail.java;
调用方法:new Email().sendMail(接收邮箱地址,邮件主题,邮件体内容)
AuthenticatorUtil.java清单:
package p1;
import javax.mail.PasswordAuthentication;
//当smtp需要验证时所需要用到的验证类
class AuthenticatorUtil
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public AuthenticatorUtil(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
EMail.java清单:
package p1;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import p1.AuthenticatorUtil;
public class EMail{
public EMail() {}
//此方法执行邮件发送的操作
//mailTo:目标邮件地址
//mailSubject:邮件主题
//mailBody:邮件体,在这里是邮件正文
public boolean sendMail(String mailTo,String mailSubject,String mailBody){
try {
//初始化所有必要的参数
String smtpServer = "smtp.bbbb.com";//smtp服务器
String smtpAuth = "true";//smtp服务器是否需要进行验证
String smtpUser = "uuuuu";//smtp分配的用户名(就是你的E-Mail地址中@符号前面部分)
String smtpPassword = "pppp";//登陆口令
String from = "aaaa@bbbb.com";//用于在对方收到的邮件中作为发送人地址显示
String to = mailTo;//邮件需要发送到的目标邮件地址
String subject = mailSubject;//邮件主题
String text = mailBody;//邮件体,在这里是邮件正文
Properties props = new Properties();//构造Properties,作为后面的Session参数容器
Session sendMailSession;
Transport transport;
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth", smtpAuth);
if ("true".equals(smtpAuth)) {
//如果smtp服务器需要验证,则构建AuthertiactorUtil用于mail session的创建
AuthenticatorUtil authutil = new AuthenticatorUtil(smtpUser, smtpPassword);
sendMailSession = Session.getInstance(props, authutil);
}
else {
sendMailSession = Session.getInstance(props);
}
//设置是否是调试状态,如果这里设置为true的话,则会在控制台输出发送过程,输出信息类似于在telnet上发送邮件
sendMailSession.setDebug(true);
//初始化邮件消息
Message message = new MimeMessage(sendMailSession);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(mailTo));
message.setSubject(subject);
message.setSentDate(new Date());
message.setText(text);
message.saveChanges();
transport = sendMailSession.getTransport("smtp");//此次会话是发送邮件(smtp)
transport.send(message, message.getAllRecipients());//从这里可以看出来,可以设置多个发送目的地址
transport.close();
}catch (Exception e) {
System.err.println("邮件发送失败:" + e.getMessage());
return false;
}
return true;
}
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
#3
现在的smtp服务器基本上都需要验证,网上很多javamail验证的例子,你可以找找
#4
首先从SUN的网站上下2个包,很小,这2个包没有被包含在JSDK中……
JavaMail类文件包和JAF(JavaBeans Activation Framework )
一、导入包
分别解压缩你下载的2个包,随便放哪里无所谓。我是放在JSDK文件夹下。然后分别找到mail.jar和activation.jar这2个包,复制下这2个包到 H:\jdk1.4\jre\lib\ext。如果你照着里面的README做,会报错的。
二、编写代码
2个页面(简单点,说明问题就行)
表单:用来填写一些基本信息,发信者邮箱、收信者信箱、标题、邮件内容。
JSP处理页面:用来发信。
代码如下:(表单)
<HTML>
<BODY>
<FORM action="sendmail.jsp" method="post">
<TABLE align="center">
<TR>
<TD width="50%">
To:<BR><INPUT name="to" size="25">
</TD>
<TD width="50%">
From:<BR><INPUT name="from" size="25">
</TD>
</TR>
<TR>
<TD colspan="2">
Subject:<BR><INPUT name="subject" size="50">
</TD>
</TR>
<TR>
<TD colspan="2">
Message:<BR><TEXTAREA name="text" rows=25 cols=85></TEXTAREA>
</TD>
</TR>
</TABLE>
<INPUT type="submit" name="cb_submit" value=" Send ">
<INPUT type="reset" name="cb_reset" value=" Clear ">
</FORM>
</BODY>
</HTML>
JSP页面代码:
%@ page
import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"
%>
<html>
<head>
<TITLE>JSP meets JavaMail, what a sweet combo.</TITLE>
</HEAD>
<BODY>
<%
try{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
String usr="××";//这里填写你发信者的邮箱地址
String pwd="××";//这里填写你发信者的邮箱密码
props.put("mail.smtp.host","smtp.××.com"); //这里填写你发信者的SMTP主机,如:smtp.sohu.com
props.put("mail.smtp.user",usr);
props.put("mail.smtp.password",pwd);
props.put("mail.smtp.auth","true");
sendMailSession = Session.getInstance(props,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("××", "××"); }//这里填写你发信者的邮箱地址和密码
});//关键是这句,网上文档几乎没有,我查了大半天才查出来。如果你的邮箱是SMTP验证的,就得这么写。否则会报错。Session.getInstance(props)这个方法是针对SMTP不要求验证的,我的邮箱要验证,所以得这么写。
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(request.getParameter("from")));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to")));
newMessage.setSubject(request.getParameter("subject"));
newMessage.setSentDate(new Date());
newMessage.setText(request.getParameter("text"));
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
%>
<P>Your mail has been sent.</P>
<%
}
catch(MessagingException m)
{
out.println(m.toString());
}
%>
</BODY>
</HTML>
三、调试
开启TOMCAT,调试一下就可以了。
四、注意点
在表单填写的发信者的邮箱地址千万记住要和JSP页面里的对应,否则会报错!
JavaMail类文件包和JAF(JavaBeans Activation Framework )
一、导入包
分别解压缩你下载的2个包,随便放哪里无所谓。我是放在JSDK文件夹下。然后分别找到mail.jar和activation.jar这2个包,复制下这2个包到 H:\jdk1.4\jre\lib\ext。如果你照着里面的README做,会报错的。
二、编写代码
2个页面(简单点,说明问题就行)
表单:用来填写一些基本信息,发信者邮箱、收信者信箱、标题、邮件内容。
JSP处理页面:用来发信。
代码如下:(表单)
<HTML>
<BODY>
<FORM action="sendmail.jsp" method="post">
<TABLE align="center">
<TR>
<TD width="50%">
To:<BR><INPUT name="to" size="25">
</TD>
<TD width="50%">
From:<BR><INPUT name="from" size="25">
</TD>
</TR>
<TR>
<TD colspan="2">
Subject:<BR><INPUT name="subject" size="50">
</TD>
</TR>
<TR>
<TD colspan="2">
Message:<BR><TEXTAREA name="text" rows=25 cols=85></TEXTAREA>
</TD>
</TR>
</TABLE>
<INPUT type="submit" name="cb_submit" value=" Send ">
<INPUT type="reset" name="cb_reset" value=" Clear ">
</FORM>
</BODY>
</HTML>
JSP页面代码:
%@ page
import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"
%>
<html>
<head>
<TITLE>JSP meets JavaMail, what a sweet combo.</TITLE>
</HEAD>
<BODY>
<%
try{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
String usr="××";//这里填写你发信者的邮箱地址
String pwd="××";//这里填写你发信者的邮箱密码
props.put("mail.smtp.host","smtp.××.com"); //这里填写你发信者的SMTP主机,如:smtp.sohu.com
props.put("mail.smtp.user",usr);
props.put("mail.smtp.password",pwd);
props.put("mail.smtp.auth","true");
sendMailSession = Session.getInstance(props,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("××", "××"); }//这里填写你发信者的邮箱地址和密码
});//关键是这句,网上文档几乎没有,我查了大半天才查出来。如果你的邮箱是SMTP验证的,就得这么写。否则会报错。Session.getInstance(props)这个方法是针对SMTP不要求验证的,我的邮箱要验证,所以得这么写。
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(request.getParameter("from")));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to")));
newMessage.setSubject(request.getParameter("subject"));
newMessage.setSentDate(new Date());
newMessage.setText(request.getParameter("text"));
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
%>
<P>Your mail has been sent.</P>
<%
}
catch(MessagingException m)
{
out.println(m.toString());
}
%>
</BODY>
</HTML>
三、调试
开启TOMCAT,调试一下就可以了。
四、注意点
在表单填写的发信者的邮箱地址千万记住要和JSP页面里的对应,否则会报错!
#5
首先感谢xiaohuasz和mascotzhuang(基督山伯爵)
主要是增加一个Authenticator类的子类,并覆盖
protected PasswordAuthentication getPasswordAuthentication()方法,对吗?
我两个都试了一下,结果不行,下面是我按xiaohuasz方法写的:
1
class AuthenticatorUtil
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public AuthenticatorUtil(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
2
AuthenticatorUtil authutil = new AuthenticatorUtil("hanbaoju", "123456");
Properties props = new Properties();
rops.put("mail.smtp.auth","true");
props.put("mail.smtp.host","smtp.163.com");
session = Session.getDefaultInstance(props, authutil);
3 出现异常提示:
DEBUG: setDebug: JavaMail version 1.3.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
Sending failed;
nested exception is:
class javax.mail.AuthenticationFailedException
我什么地方写得不对??
谢谢!!
主要是增加一个Authenticator类的子类,并覆盖
protected PasswordAuthentication getPasswordAuthentication()方法,对吗?
我两个都试了一下,结果不行,下面是我按xiaohuasz方法写的:
1
class AuthenticatorUtil
extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public AuthenticatorUtil(String user, String password) {
this.strUser = user;
this.strPwd = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(strUser, strPwd);
}
}
2
AuthenticatorUtil authutil = new AuthenticatorUtil("hanbaoju", "123456");
Properties props = new Properties();
rops.put("mail.smtp.auth","true");
props.put("mail.smtp.host","smtp.163.com");
session = Session.getDefaultInstance(props, authutil);
3 出现异常提示:
DEBUG: setDebug: JavaMail version 1.3.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
Sending failed;
nested exception is:
class javax.mail.AuthenticationFailedException
我什么地方写得不对??
谢谢!!
#6
你给出的代码应该没有问题
确认帐号和密码是否正确
确认帐号和密码是否正确
#7
我可以给263.net发邮件,也有认证,但给163发邮件就会出现
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class com.sun.mail.smtp.SMTPAddressFailedException: 554 <linuxlong@163.com>: Recipient address rejected: Relay access denied
~~~~~~~~~~~~~~~
借人气同问
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class com.sun.mail.smtp.SMTPAddressFailedException: 554 <linuxlong@163.com>: Recipient address rejected: Relay access denied
~~~~~~~~~~~~~~~
借人气同问
#8
给你我的程序看看,直接永久可以了(包自己下载吧):
html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>撰写邮件</title>
</head>
<body>
<form action="testall.jsp" method="post" name="form1">
<table width="424" border="0" align="center" cellspacing="1" bgcolor="#006600" class="black" height="287">
<tr bgcolor="#FFFFFF">
<td width="102" height="29"><font face="宋体"><font size="3">收信人</font>:</font></td>
<td width="308" height="23"> <input name="to1" type="text" id="to1" size="19"><font size="2"><i><font face="Basemic Times">@
</font></i></font><input name="to2" type="text" id="to2" size="17"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="12"><font size="3"><font face="宋体">寄信人</font></font><font face="宋体">:</font></td>
<td width="308" height="23"> <input name="from1" type="text" id="from1" size="19"><i><font face="Basemic Times" size="2">@
</font></i><input name="from2" type="text" id="from2" size="17"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="14"><font size="3"><font face="宋体">用户名</font></font><font face="宋体">:</font></td>
<td width="308" height="25"> <input name="yhm" type="text" id="yhm" size="41">
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="14"><font size="3"><font face="宋体">密 码</font></font><font face="宋体">:</font></td>
<td width="308" height="25"> <input name="mm" type="text" id="mm" size="41">
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="25"><font size="3" face="宋体">主 题:</font></td>
<td width="308" height="25"> <input name="title" type="text" id="title" size="41"></td>
</tr>
<tr>
<td height="25" colspan="2" bgcolor="#FFFFFF" width="401"><font size="3"><font face="宋体">信件类型</font>:
</font><select name="emailtype" id="emailtype" size="1">
<option value="text/plain">Text</option>
<option value="text/html">Html</option>
<option selected>-Select-</option>
</select></td>
</tr>
<tr>
<td height="63" colspan="2" bgcolor="#FFFFFF" width="401"><textarea name="content" cols="56" rows="8" id="content"></textarea></td>
</tr>
<tr align="center" valign="bottom">
<td colspan="2" bgcolor="#FFFFFF" width="401" height="33">
<p align="left"><font size="3">附件(本地):</font>
<input name="fj2" type="file" id="fj2" size="47"></p>
</td>
</tr>
<tr align="center">
<td colspan="2" bgcolor="#FFFFFF" width="401" height="11"> <input type="submit" name="Submit" value="发送">
<input type="reset" name="Submit2" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%request.setCharacterEncoding("gb2312");%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%><!--要发送附件必须引入该库-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>发送成功</title>
</head>
<body>
<%
try{
String tto1=request.getParameter("to1");
String tto2=request.getParameter("to2");
String tfrom1=request.getParameter("from1");
String tfrom2=request.getParameter("from2");
String tyhm=request.getParameter("yhm");
String tmm=request.getParameter("mm");
String ttitle=request.getParameter("title");
String emailtype=request.getParameter("emailtype");//获取email类型
String tcontent=request.getParameter("content");
//String tfj1=request.getParameter("fj1");
String tfj2=request.getParameter("fj2");
String tfj3=request.getParameter("fj3");
String smtptype="";
if(tfrom2.equals("163.com"))
{smtptype="smtp.163.com";
}//支持
else if(tfrom2.equals("126.com"))
{smtptype="smtp.126.com";
}//支持
else if(tfrom2.equals("21cn.com"))
{smtptype="smtp.21cn.com";
}//支持
else if(tfrom2.equals("yahoo.com.cn"))
{smtptype="smtp.mail.yahoo.com.cn";
}//支持
else if(tfrom2.equals("tom.com"))
{smtptype="smtp.tom.com";
}//支持
else if(tfrom2.equals("263.net"))
{smtptype="smtp.263.net";
}//未试
else if(tfrom2.equals("mail.china.com"))
{smtptype="mail.china.com";
}//不支持
else if(tfrom2.equals("sina.com"))
{smtptype="smtp.sina.com";
}//不支持
Properties props=new Properties();
props.put("mail.smtp.host",smtptype);
props.put("mail.smtp.auth","true");
Session s=Session.getInstance(props);
s.setDebug(true);
MimeMessage message=new MimeMessage(s);
//给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from=new InternetAddress(tfrom1+"@"+tfrom2,tfrom1);
message.setFrom(from);
InternetAddress to=new InternetAddress(tto1+"@"+tto2);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate(new Date());
Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放多个BodyPart对象
//设置信件文本内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent,emailtype+";charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
mm.addBodyPart(mdp);//将含有信件内容的BodyPart加入到MimeMultipart对象中
//设置信件的附件1(自定义附件:直接将所设文本内容加到自定义文件中作为附件发送)
//mdp=new MimeBodyPart();//新建一个存放附件的BodyPart
DataHandler dh=new DataHandler(tfj2,"text/plain;charset=gb2312");
//新建一个DataHandler对象,并设置其内容和格式/编码方式
//mdp.setFileName("text.txt");//加上这句将作为附件发送,否则将作为信件的文本内容
//mdp.setDataHandler(dh);//给BodyPart对象设置内容为dh
//mm.addBodyPart(mdp);//将含有附件的BodyPart加入到MimeMultipart对象中
//设置信件的附件2(用本地上的文件作为附件)
mdp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(tfj2);
dh=new DataHandler(fds);
int ddd=tfj2.lastIndexOf("\\");
String fname=tfj2.substring(ddd);//提取文件名
String ffname=new String(fname.getBytes("gb2312"),"ISO8859-1");//处理文件名是中文的情况
mdp.setFileName(ffname);//可以和原文件名不一致,但最好一样
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
//设置信件的附件3(用远程文件作为附件)
//mdp=new MimeBodyPart();
/*URL urlfj=new URL(tfj3);
URLDataSource ur=new URLDataSource(urlfj);
//注:这里用的参数只能为URL对象,不能为URL字串,在前面类介绍时有误(请谅解),这里纠正一下.
dh=new DataHandler(ur);
int ttt=tfj3.lastIndexOf("/");
String urlname=tfj3.substring(ttt);
//String urlfname=new String(urlname.getBytes("gb2312"),"ISO8859-1");//不知怎么回事,这里不能处理中文问题
mdp.setFileName(urlname);
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
*/
message.setContent(mm);//把mm作为消息对象的内容
message.saveChanges();
Transport transport=s.getTransport("smtp");
transport.connect(smtptype,tyhm,tmm);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
<div align="center">
<p><font color="#FF6600">发送成功!</font></p>
<br>
<a href="m.html">再发一封</a> </p>
</div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>
html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>撰写邮件</title>
</head>
<body>
<form action="testall.jsp" method="post" name="form1">
<table width="424" border="0" align="center" cellspacing="1" bgcolor="#006600" class="black" height="287">
<tr bgcolor="#FFFFFF">
<td width="102" height="29"><font face="宋体"><font size="3">收信人</font>:</font></td>
<td width="308" height="23"> <input name="to1" type="text" id="to1" size="19"><font size="2"><i><font face="Basemic Times">@
</font></i></font><input name="to2" type="text" id="to2" size="17"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="12"><font size="3"><font face="宋体">寄信人</font></font><font face="宋体">:</font></td>
<td width="308" height="23"> <input name="from1" type="text" id="from1" size="19"><i><font face="Basemic Times" size="2">@
</font></i><input name="from2" type="text" id="from2" size="17"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="14"><font size="3"><font face="宋体">用户名</font></font><font face="宋体">:</font></td>
<td width="308" height="25"> <input name="yhm" type="text" id="yhm" size="41">
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="14"><font size="3"><font face="宋体">密 码</font></font><font face="宋体">:</font></td>
<td width="308" height="25"> <input name="mm" type="text" id="mm" size="41">
</tr>
<tr bgcolor="#FFFFFF">
<td width="102" height="25"><font size="3" face="宋体">主 题:</font></td>
<td width="308" height="25"> <input name="title" type="text" id="title" size="41"></td>
</tr>
<tr>
<td height="25" colspan="2" bgcolor="#FFFFFF" width="401"><font size="3"><font face="宋体">信件类型</font>:
</font><select name="emailtype" id="emailtype" size="1">
<option value="text/plain">Text</option>
<option value="text/html">Html</option>
<option selected>-Select-</option>
</select></td>
</tr>
<tr>
<td height="63" colspan="2" bgcolor="#FFFFFF" width="401"><textarea name="content" cols="56" rows="8" id="content"></textarea></td>
</tr>
<tr align="center" valign="bottom">
<td colspan="2" bgcolor="#FFFFFF" width="401" height="33">
<p align="left"><font size="3">附件(本地):</font>
<input name="fj2" type="file" id="fj2" size="47"></p>
</td>
</tr>
<tr align="center">
<td colspan="2" bgcolor="#FFFFFF" width="401" height="11"> <input type="submit" name="Submit" value="发送">
<input type="reset" name="Submit2" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%request.setCharacterEncoding("gb2312");%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%><!--要发送附件必须引入该库-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>发送成功</title>
</head>
<body>
<%
try{
String tto1=request.getParameter("to1");
String tto2=request.getParameter("to2");
String tfrom1=request.getParameter("from1");
String tfrom2=request.getParameter("from2");
String tyhm=request.getParameter("yhm");
String tmm=request.getParameter("mm");
String ttitle=request.getParameter("title");
String emailtype=request.getParameter("emailtype");//获取email类型
String tcontent=request.getParameter("content");
//String tfj1=request.getParameter("fj1");
String tfj2=request.getParameter("fj2");
String tfj3=request.getParameter("fj3");
String smtptype="";
if(tfrom2.equals("163.com"))
{smtptype="smtp.163.com";
}//支持
else if(tfrom2.equals("126.com"))
{smtptype="smtp.126.com";
}//支持
else if(tfrom2.equals("21cn.com"))
{smtptype="smtp.21cn.com";
}//支持
else if(tfrom2.equals("yahoo.com.cn"))
{smtptype="smtp.mail.yahoo.com.cn";
}//支持
else if(tfrom2.equals("tom.com"))
{smtptype="smtp.tom.com";
}//支持
else if(tfrom2.equals("263.net"))
{smtptype="smtp.263.net";
}//未试
else if(tfrom2.equals("mail.china.com"))
{smtptype="mail.china.com";
}//不支持
else if(tfrom2.equals("sina.com"))
{smtptype="smtp.sina.com";
}//不支持
Properties props=new Properties();
props.put("mail.smtp.host",smtptype);
props.put("mail.smtp.auth","true");
Session s=Session.getInstance(props);
s.setDebug(true);
MimeMessage message=new MimeMessage(s);
//给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from=new InternetAddress(tfrom1+"@"+tfrom2,tfrom1);
message.setFrom(from);
InternetAddress to=new InternetAddress(tto1+"@"+tto2);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate(new Date());
Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放多个BodyPart对象
//设置信件文本内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent,emailtype+";charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
mm.addBodyPart(mdp);//将含有信件内容的BodyPart加入到MimeMultipart对象中
//设置信件的附件1(自定义附件:直接将所设文本内容加到自定义文件中作为附件发送)
//mdp=new MimeBodyPart();//新建一个存放附件的BodyPart
DataHandler dh=new DataHandler(tfj2,"text/plain;charset=gb2312");
//新建一个DataHandler对象,并设置其内容和格式/编码方式
//mdp.setFileName("text.txt");//加上这句将作为附件发送,否则将作为信件的文本内容
//mdp.setDataHandler(dh);//给BodyPart对象设置内容为dh
//mm.addBodyPart(mdp);//将含有附件的BodyPart加入到MimeMultipart对象中
//设置信件的附件2(用本地上的文件作为附件)
mdp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(tfj2);
dh=new DataHandler(fds);
int ddd=tfj2.lastIndexOf("\\");
String fname=tfj2.substring(ddd);//提取文件名
String ffname=new String(fname.getBytes("gb2312"),"ISO8859-1");//处理文件名是中文的情况
mdp.setFileName(ffname);//可以和原文件名不一致,但最好一样
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
//设置信件的附件3(用远程文件作为附件)
//mdp=new MimeBodyPart();
/*URL urlfj=new URL(tfj3);
URLDataSource ur=new URLDataSource(urlfj);
//注:这里用的参数只能为URL对象,不能为URL字串,在前面类介绍时有误(请谅解),这里纠正一下.
dh=new DataHandler(ur);
int ttt=tfj3.lastIndexOf("/");
String urlname=tfj3.substring(ttt);
//String urlfname=new String(urlname.getBytes("gb2312"),"ISO8859-1");//不知怎么回事,这里不能处理中文问题
mdp.setFileName(urlname);
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);
*/
message.setContent(mm);//把mm作为消息对象的内容
message.saveChanges();
Transport transport=s.getTransport("smtp");
transport.connect(smtptype,tyhm,tmm);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
<div align="center">
<p><font color="#FF6600">发送成功!</font></p>
<br>
<a href="m.html">再发一封</a> </p>
</div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>
#9
自己写mail网上例子很多,95%邮件服务器以上都需密码验证,我试过163邮件,必须要密码,上传最大48兆