?? mail.java
字號:
/*
* Created on 2005-7-28
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.ahbay.mailMgr;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
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;
/**
* @author: 王仁超
* @Date: 2005-7-28
* @FileName: mail.java
* @PackageName: com.ahbay.mailMgr
* @Method Name: mail
*/
public class mail {
/*
* public property
*/
public String strTo = "";
public String strFrom = "";
public String strSubject = "";
public String strText = "";
public String strUsername = "";
public String strPassword = "";
public String strSmtp = "";
public String strFilename = "";
public boolean needAuth = false;
/*
* private property
*/
private MimeMessage mimeMsg;
private Session session;
private Properties props;
private Multipart mptMailContent;
/*
* public method
*/
public void setStrTo(String strTo) {
this.strTo = strTo;
}
public void setStrFrom(String strFrom) {
this.strFrom = strFrom;
}
public void setStrSubject(String strSubject) {
this.strSubject = strSubject;
}
public void setStrText(String strText) {
this.strText = strText;
}
public void setStrUsername(String strUsername) {
this.strUsername = strUsername;
}
public void setStrPassword(String strPassword) {
this.strPassword = strPassword;
}
public void setStrSmtp(String strSmtp) {
this.strSmtp = strSmtp;
}
public void setStrFilename(String strFilename) {
this.strFilename = strFilename;
}
/**
*
* @author: 王仁超
* @Date: 2005-7-28
* @FileName: mail.java
* @PackageName: com.ahbay.mailMgr
* @Method Name: sendSimpleMail
*/
public boolean sendSimpleMail()
{
if(this.props == null)
{
this.props = new Properties();
}
if (!this.setSmtpHost()) return false;
if (!this.createMimeMessage()) return false;
if (!this.setNeedAuth()) return false;
if (!this.setMailBody()) return false;
try
{
this.mimeMsg.setFrom(new InternetAddress(this.strFrom));
this.mimeMsg.setRecipient(Message.RecipientType.TO, new InternetAddress(this.strTo));
this.mimeMsg.setSubject(this.strSubject);
this.mimeMsg.setSentDate(new Date());
this.mimeMsg.setContent(mptMailContent);
this.mimeMsg.saveChanges();
Transport transport;
transport = this.session.getTransport("smtp");
if (needAuth)
{
transport.connect((String)props.get("mail.smtp.host"),this.strUsername,this.strPassword);
}
transport.send(this.mimeMsg);
//transport.sendMessage(this.mimeMsg,this.mimeMsg.getAllRecipients());
transport.close();
}
catch(MessagingException m)
{
System.err.println(m.getMessage());
return false;
}
return true;
}
/**
*
* @author: 王仁超
* @Date: 2005-7-28
* @FileName: mail.java
* @PackageName: com.ahbay.mailMgr
* @Method Name: setMailFileAffix
*/
public boolean setMailFileAffix()
{
try
{
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(this.strFilename);
bp.setFileName(fileds.getName());
bp.setDataHandler(new DataHandler(fileds));
this.mptMailContent.addBodyPart(bp);
BodyPart mdp=new MimeBodyPart();
DataHandler dh=new DataHandler("JavaMail附件測試","text/plain;charset=gb2312");
mdp.setFileName("xxf.txt");
mdp.setDataHandler(dh);
this.mptMailContent.addBodyPart(mdp);
}
catch(Exception e)
{
System.err.println("增加郵件附件:"+this.strFilename+"發生錯誤!"+e);
return false;
}
return true;
}
/*
* private method
*/
/**
*
* @author: 王仁超
* @Date: 2005-7-28
* @FileName: mail.java
* @PackageName: com.ahbay.mailMgr
* @Method Name: setSmtpHost
*/
private boolean setSmtpHost()
{
this.props.put("mail.smtp.host",this.strSmtp);
return true;
}
/**
*
* @author: 王仁超
* @Date: 2005-7-28
* @FileName: mail.java
* @PackageName: com.ahbay.mailMgr
* @Method Name: createMimeMessage
*/
private boolean createMimeMessage()
{
try
{
this.session = Session.getDefaultInstance(this.props,null);
}
catch(Exception e)
{
System.err.println("獲取郵件會話對象時發生錯誤!"+e);
return false;
}
try
{
this.mimeMsg = new MimeMessage(this.session);
mptMailContent = new MimeMultipart();
}
catch(Exception e)
{
System.err.println("創建MIME郵件對象失敗!"+e);
return false;
}
return true;
}
/**
*
* @author: 王仁超
* @Date: 2005-7-28
* @FileName: mail.java
* @PackageName: com.ahbay.mailMgr
* @Method Name: setNeedAuth
*/
private boolean setNeedAuth()
{
if(this.needAuth)
{
this.props.put("mail.smtp.auth","true");
}
else
{
this.props.put("mail.smtp.auth","false");
}
return true;
}
/**
*
* @author: 王仁超
* @Date: 2005-7-28
* @FileName: mail.java
* @PackageName: com.ahbay.mailMgr
* @Method Name: setMailBody
*/
private boolean setMailBody()
{
try
{
BodyPart bp = new MimeBodyPart();
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+this.strText,"text/html;charset=GB2312");
this.mptMailContent.addBodyPart(bp);
}
catch(Exception e)
{
System.err.println("設置郵件正文時發生錯誤!"+e);
return false;
}
return true;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -