当前位置: 技术问答>java相关
无法发送邮件,请问是什么问题,源码如下
来源: 互联网 发布时间:2017-04-07
本文导语: bean源码如下: package mail; import javax.mail.*; import javax.mail.internet.*; import java.util.*; public class sendMailBean { private String EntryName; private String Password; private String pop3Server; private String smtpServer; private String To; private ...
bean源码如下:
package mail;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class sendMailBean {
private String EntryName;
private String Password;
private String pop3Server;
private String smtpServer;
private String To;
private String Body;
private String Subject;
Properties props = new Properties ();
Session session;
Message msg;
public sendMailBean() {}
public void setEntryname(String name){
this.EntryName=name;
}
public void setPassword(String password){
this.Password=password;
}
public void setPop3Server(String pop3){
this.pop3Server=pop3;
}
public void setSmtpServer(String smtp){
this.smtpServer=smtp;
}
public void setRecipient(String to){
this.To=to;
}
public void setBody(String body){
this.Body=body;
}
public void setSubject(String subject){
this.Subject=subject;
}
public String sendmail(){
try{
props=System.getProperties();
props.put("mail.smtp.host",smtpServer);
props.put("mail.smtp.auth","true");
PopupAuthenticator popAuthenticator = new PopupAuthenticator();
PasswordAuthentication pop = popAuthenticator.performCheck(EntryName,Password);
Session session=Session.getInstance(props,popAuthenticator );
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(EntryName));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(To,false));
msg.setSubject(Subject);
msg.setText(Body);
msg.setSentDate(new Date());
Transport.send(msg);
return "Message sent OK.";
}
catch(Exception ex)
{
ex.printStackTrace();
}
return "Message sent error.";
}
}
然后在jsp页面中设置属性如下:
package mail;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class sendMailBean {
private String EntryName;
private String Password;
private String pop3Server;
private String smtpServer;
private String To;
private String Body;
private String Subject;
Properties props = new Properties ();
Session session;
Message msg;
public sendMailBean() {}
public void setEntryname(String name){
this.EntryName=name;
}
public void setPassword(String password){
this.Password=password;
}
public void setPop3Server(String pop3){
this.pop3Server=pop3;
}
public void setSmtpServer(String smtp){
this.smtpServer=smtp;
}
public void setRecipient(String to){
this.To=to;
}
public void setBody(String body){
this.Body=body;
}
public void setSubject(String subject){
this.Subject=subject;
}
public String sendmail(){
try{
props=System.getProperties();
props.put("mail.smtp.host",smtpServer);
props.put("mail.smtp.auth","true");
PopupAuthenticator popAuthenticator = new PopupAuthenticator();
PasswordAuthentication pop = popAuthenticator.performCheck(EntryName,Password);
Session session=Session.getInstance(props,popAuthenticator );
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(EntryName));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(To,false));
msg.setSubject(Subject);
msg.setText(Body);
msg.setSentDate(new Date());
Transport.send(msg);
return "Message sent OK.";
}
catch(Exception ex)
{
ex.printStackTrace();
}
return "Message sent error.";
}
}
然后在jsp页面中设置属性如下:
|
给你点参考吧!你得那个身份认证写法有问题!呵呵!看我下面得源码!如果觉得有用就给分吧!
package peace;
import java.util.* ;
import java.io.* ;
import javax.mail.* ;
import javax.mail.internet.* ;
import javax.activation.* ;
public class sendMail {
//定义发件人、收件人、主题等
String to="";
String from="";
String host="";
String body="";
String filename="";
String subject="";
//用于保存发送附件的文件名的集合
Vector file = new Vector();
//做一个可以传发件人等参数的构造
public void sendMail (String to,String subject,String textstr){
//初始化发件人、收件人、主题等
this.to=to;
this.from="bridgec@cncpic.com";
this.host="www.cncpic.com";
this.subject=subject;
this.body=textstr;
}
//该方法用于收集附件名
public void attachfile(String fname){
file.addElement(fname);
}
//开始发送信件的方法
public boolean startSend(){
//创建Properties对象
Properties props =System.getProperties();
//创建信件服务器
props.put("mail.smtp.host", "www.cncpic.com");
//身份认证
props.put("mail.smtp.auth", "true");
//得到默认的对话对象
Session session=Session.getDefaultInstance(props, null);
try {
//创建一个消息,并初始化该消息的各项元素
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
//后面的BodyPart将加入到此处创建的Multipart中
Multipart mp = new MimeMultipart();
//利用枚举器方便的遍历集合
Enumeration efile=file.elements();
MimeBodyPart mbp1=new MimeBodyPart();
mbp1.setText(body);
mp.addBodyPart(mbp1);
//检查序列中是否还有更多的对象
while(efile.hasMoreElements()){
MimeBodyPart mbp=new MimeBodyPart();
//选择出每一个附件名
filename=efile .nextElement().toString();
//得到数据源
FileDataSource fds=new FileDataSource(filename);
//得到附件本身并至入BodyPart
mbp.setDataHandler(new DataHandler(fds));
//得到文件名同样至入BodyPart
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
//移走集合中的所有元素
file.removeAllElements();
//Multipart加入到信件
msg.setContent(mp);
//设置信件头的发送日期
msg.setSentDate(new Date());
//发送信件
System.out.print(to);
Transport transport=session.getTransport("smtp");
transport.connect("www.cncpic.com","bridgec","123456");
transport.send(msg);
} catch (Exception mex)
{
System.out.println(mex);
}
return true;
}
}
jsp页面调用如下:
package peace;
import java.util.* ;
import java.io.* ;
import javax.mail.* ;
import javax.mail.internet.* ;
import javax.activation.* ;
public class sendMail {
//定义发件人、收件人、主题等
String to="";
String from="";
String host="";
String body="";
String filename="";
String subject="";
//用于保存发送附件的文件名的集合
Vector file = new Vector();
//做一个可以传发件人等参数的构造
public void sendMail (String to,String subject,String textstr){
//初始化发件人、收件人、主题等
this.to=to;
this.from="bridgec@cncpic.com";
this.host="www.cncpic.com";
this.subject=subject;
this.body=textstr;
}
//该方法用于收集附件名
public void attachfile(String fname){
file.addElement(fname);
}
//开始发送信件的方法
public boolean startSend(){
//创建Properties对象
Properties props =System.getProperties();
//创建信件服务器
props.put("mail.smtp.host", "www.cncpic.com");
//身份认证
props.put("mail.smtp.auth", "true");
//得到默认的对话对象
Session session=Session.getDefaultInstance(props, null);
try {
//创建一个消息,并初始化该消息的各项元素
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
//后面的BodyPart将加入到此处创建的Multipart中
Multipart mp = new MimeMultipart();
//利用枚举器方便的遍历集合
Enumeration efile=file.elements();
MimeBodyPart mbp1=new MimeBodyPart();
mbp1.setText(body);
mp.addBodyPart(mbp1);
//检查序列中是否还有更多的对象
while(efile.hasMoreElements()){
MimeBodyPart mbp=new MimeBodyPart();
//选择出每一个附件名
filename=efile .nextElement().toString();
//得到数据源
FileDataSource fds=new FileDataSource(filename);
//得到附件本身并至入BodyPart
mbp.setDataHandler(new DataHandler(fds));
//得到文件名同样至入BodyPart
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
//移走集合中的所有元素
file.removeAllElements();
//Multipart加入到信件
msg.setContent(mp);
//设置信件头的发送日期
msg.setSentDate(new Date());
//发送信件
System.out.print(to);
Transport transport=session.getTransport("smtp");
transport.connect("www.cncpic.com","bridgec","123456");
transport.send(msg);
} catch (Exception mex)
{
System.out.println(mex);
}
return true;
}
}
jsp页面调用如下:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!