当前位置: 代码迷 >> Java Web开发 >> 散分,送上邮件发送代码
  详细解决方案

散分,送上邮件发送代码

热度:55   发布时间:2016-04-17 15:43:57.0
散分,送上邮件发送代码!

import   java.util.Date;
import   java.util.Properties;

import   javax.mail.BodyPart;
import   javax.mail.Message;
import   javax.mail.Multipart;
import   javax.mail.Session;
import   javax.mail.Transport;
import   javax.mail.internet.InternetAddress;
import   javax.mail.internet.MimeBodyPart;
import   javax.mail.internet.MimeMessage;
import   javax.mail.internet.MimeMultipart;


public   class   SendEmail   {

public   static   void   sendMail(String   con){
try{  

                        //主机
String   smtpserver= "mail.anycare.cn ";
                   
Properties   props=new   Properties();  
props.put( "mail.smtp.host ",smtpserver);  
props.put( "mail.smtp.auth ", "true ");  
Session   s=Session.getInstance(props);  
s.setDebug(false);   //控制台是否输出信息
                       
//mail主题
MimeMessage   message=new   MimeMessage(s);  
                          //邮件中显示的地址和名称
InternetAddress   from=new   InternetAddress( "xp1204@163.cn ", "登山临山 ");  
message.setFrom(from);  
//收件人列表
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse( "xp1204@163.com,xp@163.com "));  
message.setSubject( "有人登陆系统! ");
message.setSentDate(new   Date());  

//
Multipart   mm=new   MimeMultipart();
BodyPart   mdp=new   MimeBodyPart();
// out.print(tcontent);
mdp.setContent(con, "text/plain;charset=UTF-8 ");
mm.addBodyPart(mdp);  

/*
DataHandler   dh=new   DataHandler(tfj2, "text/plain;charset=UTF-8 ");
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( "UTF-8 "), "ISO8859-1 ");
mdp.setFileName(ffname);
mdp.setDataHandler(dh);  
mm.addBodyPart(mdp);  

*/

message.setContent(mm);

message.saveChanges();  
Transport   transport=s.getTransport( "smtp ");  
//连接,主机,用户名,密码
transport.connect(smtpserver, "xxx@163.cn ", "xxxxx ");  
transport.sendMessage(message,message.getAllRecipients());  
transport.close();  
}catch(Exception   e){
e.printStackTrace();
}
}
}


调用
SendEmail.sendMail( "login   id: "+loginForm.getUserId());

------解决方案--------------------
upup
------解决方案--------------------
谢了,我要包,xueqizi05@163.com
------解决方案--------------------
我也要个包,谢谢.yunlai.li@eduqn.com
------解决方案--------------------
太好了。我自己用Java的mail包跟我的J2ee总冲突。
  相关解决方案