当前位置: 技术问答>java相关
发email时报Session session = null;,请看代码解决我的问题
来源: 互联网 发布时间:2017-04-27
本文导语: 在jsp里: | boolean sendMail( String to, String subject, String body ) { //smtp Authenticate class SmtpAuthenticator extends javax.mail.Authenticator { ...
在jsp里:
|
boolean sendMail( String to, String subject, String body )
{
//smtp Authenticate
class SmtpAuthenticator extends javax.mail.Authenticator
{
String usr=null;
String pw=null;
public SmtpAuthenticator(String user,String pass)
{
this.usr = user;
this.pw = pass;
}
protected javax.mail.PasswordAuthentication getPasswordAuthentication()
{
return new javax.mail.PasswordAuthentication(usr, pw);
}
}
if( to.length() == 0 ) return false;
if( subject.length() == 0 ) return false;
if( body.length() == 0 ) return false;
try
{
SystemSetupMaster sysM=new SystemSetupMaster();
String sSmtpServer = sysM.getSystem( "SmtpServer" );
String sName = sysM.getSystem( "UserName" );
String sFrom = sysM.getSystem( "MailFrom" );
String sPwd = sysM.getSystem( "PassWord" );
Transport transport;
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put( "mail.smtp.auth", "true");
props.put("mail.smtp.host", sSmtpServer);
SmtpAuthenticator sa = new SmtpAuthenticator(sName,sPwd);
Session sess=Session.getInstance(props,sa);
sess.setDebug(false);
// -- Create a new message --
Message msg=new MimeMessage(sess);
msg.setSentDate(new java.util.Date());
// -- Set the FROM and TO fields --
msg.setSentDate(new Date());
msg.setFrom(new InternetAddress(sFrom));
msg.setSubject(subject);
msg.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
//msg.setText(body);
// -- Set some other header information --
//msg.setHeader("X-Mailer", "Sendmail");
msg.setContent(body, "text/plain;charset=EUC-JP");
// -- Send the message --
transport=sess.getTransport( "smtp" );
transport.connect(sSmtpServer,sName,sPwd);
transport.sendMessage(msg,msg.getAllRecipients());
transport.close();
return true;
}
catch( Exception ex )
{
ex.printStackTrace();
return false;
}
}
{
//smtp Authenticate
class SmtpAuthenticator extends javax.mail.Authenticator
{
String usr=null;
String pw=null;
public SmtpAuthenticator(String user,String pass)
{
this.usr = user;
this.pw = pass;
}
protected javax.mail.PasswordAuthentication getPasswordAuthentication()
{
return new javax.mail.PasswordAuthentication(usr, pw);
}
}
if( to.length() == 0 ) return false;
if( subject.length() == 0 ) return false;
if( body.length() == 0 ) return false;
try
{
SystemSetupMaster sysM=new SystemSetupMaster();
String sSmtpServer = sysM.getSystem( "SmtpServer" );
String sName = sysM.getSystem( "UserName" );
String sFrom = sysM.getSystem( "MailFrom" );
String sPwd = sysM.getSystem( "PassWord" );
Transport transport;
Properties props = System.getProperties();
// -- Attaching to default Session, or we could start a new one --
props.put( "mail.smtp.auth", "true");
props.put("mail.smtp.host", sSmtpServer);
SmtpAuthenticator sa = new SmtpAuthenticator(sName,sPwd);
Session sess=Session.getInstance(props,sa);
sess.setDebug(false);
// -- Create a new message --
Message msg=new MimeMessage(sess);
msg.setSentDate(new java.util.Date());
// -- Set the FROM and TO fields --
msg.setSentDate(new Date());
msg.setFrom(new InternetAddress(sFrom));
msg.setSubject(subject);
msg.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
//msg.setText(body);
// -- Set some other header information --
//msg.setHeader("X-Mailer", "Sendmail");
msg.setContent(body, "text/plain;charset=EUC-JP");
// -- Send the message --
transport=sess.getTransport( "smtp" );
transport.connect(sSmtpServer,sName,sPwd);
transport.sendMessage(msg,msg.getAllRecipients());
transport.close();
return true;
}
catch( Exception ex )
{
ex.printStackTrace();
return false;
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。