📄 emailsender.java
字号:
package com.javacat.jsp.beans;
import java.util.*;
import javax.mail.*;
import java.io.*;
import javax.mail.internet.*;
import javax.mail.event.*;
/**
*a simple e-mail sender bean
*to send email contians only plain text.
*/
public class EmailSender{
//fields
private String from;
private String to;
private String subject;
private String comment;
private String filename;
private String id;
private String pwd;
String smtpHost="smtp.263.net";
public EmailSender(){
super();
}
/**
*set the message from where
*@param from from where
*/
public void setFrom(String from)
{
if(from!=null&&!from.equals("")){
this.from=from;
this.setId(from.substring(0,from.indexOf('@')));
}
}
/**
*set the message to whom
*@param to who to receive this message
*/
public void setTo(String to)
{
if(to!=null&&!to.equals(""))
this.to=to;
}
public String getTo()
{
return this.to;
}
/**
*set the subject of the message
*@param subject the message's subject
*/
public void setSubject(String subject)
{
this.subject=parseChinese(subject);
}
/**
*get the subject of the special message
@return the subject
*/
public String getSubject()
{
return this.subject;
}
/**
*set the smtp host which to send your mail
*@param host the host you want to connect
*/
public void setSmtpHost(String host)
{
if(host!=null)
this.smtpHost=host;
}
/**
*set the message yout want to send
*@param words the words you want to send
*/
public void setComment(String words)
{
this.comment=parseChinese(words);
}
/**
*set attempt filename
*@param filename the attempt file's name
*/
public void setFilename(String filename)
{
if(filename!=null&&!filename.equals(""))
this.filename=filename;
}
/**
*set your id of the smtp system
*@param id the id to be set
*/
public void setId(String id)
{
if(id!=null&&!id.equals(""))
this.id=id;
}
/**
*get the id
*@return the id who send this message
*/
public String getId()
{
return this.id;
}
/**
*set your password of the specified id
*@param password the password to be set
*/
public void setPassword(String password)
{
this.pwd=password;
}
public void sendmail() throws Exception
{
Session session;
MimeMessage message;
if(from.equals(""))
from="javacat@263.net";
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpHost);
session=Session.getInstance(props,null);
message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(subject);
message.setText(comment);
message.setSentDate(new java.util.Date());
Transport transport=session.getTransport("smtp");
transport.connect(smtpHost,id,pwd);
transport.send(message);
}//send mail
/**
*parse string to Chinese Charator in order to
*display it correctly
*@param in the source string to be parsed
*@return parsed String
*/
public String parseChinese(String in)
{
String s = null;
byte temp [];
if (in == null)
{
System.out.println("Warn:Chinese null founded!");
return new String("");
}
try
{
temp=in.getBytes("iso-8859-1");
s = new String(temp);
}catch(UnsupportedEncodingException e)
{
System.out.println (e.toString());
}
return s;
}
}//~~~~~~~email sender~~~~~~~~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -