📄 mailapiclient.java
字号:
package org.rapla.components.mail;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Properties;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
public class MailapiClient implements MailInterface
{
String mailhost = "localhost";
int port = 25;
String username;
String password;
public MailapiClient(Configuration config) throws ConfigurationException {
// get the configuration entry text with the default-value "Welcome"
setPort(config.getChild("smtp-port").getValueAsInteger(25));
setSmtpHost(config.getChild("smtp-host").getValue());
setUsername( config.getChild("username").getValue(""));
setPassword( config.getChild("password").getValue(""));
}
public MailapiClient()
{
}
public void sendMail( String senderMail, String recipient, String subject, String mailBody ) throws MailException
{
Properties props = new Properties();
props.put("mail.smtp.host", mailhost);
props.put("mail.smtp.port", new Integer(port));
try {
Class TransportC = Class.forName("javax.mail.Transport");
Class SessionC = Class.forName("javax.mail.Session");
Class MessageC = Class.forName("javax.mail.Message");
Class RecipientTypeC = Class.forName("javax.mail.Message$RecipientType");
Class MimeMessageC = Class.forName("javax.mail.internet.MimeMessage");
Class InternetAddressC = Class.forName("javax.mail.internet.InternetAddress");
Class AddressC = Class.forName("javax.mail.Address");
Method GetInstanceM = SessionC.getMethod("getInstance", new Class[] {Properties.class});
Constructor MimeMessageConst = MimeMessageC.getConstructor(new Class[] {SessionC});
Constructor InternetAdressConst = InternetAddressC.getConstructor(new Class[] {String.class});
Method SetFromM = MessageC.getMethod("setFrom", new Class[] {AddressC});
Method SetSubjectM = MessageC.getMethod("setSubject", new Class[] {String.class});
Method SetTextM = MessageC.getMethod("setText", new Class[] {String.class});
Method SetRecipientM = MessageC.getMethod("setRecipient", new Class[] {RecipientTypeC,AddressC});
Method SendM = TransportC.getMethod("send", new Class[] {MessageC});
Object RecipientTypeTo = RecipientTypeC.getField("TO").get( null);
// Session session = Session.getInstance(props, null);
Object session = GetInstanceM.invoke( null, new Object[] {props});
//MimeMessage msg = new MimeMessage(session);
Object msg = MimeMessageConst.newInstance( new Object[] {session});
//msg.setFrom(new InternetAddress( senderMail));
Object senderMailAddr = InternetAdressConst.newInstance( new Object[] { senderMail});
SetFromM.invoke( msg, new Object[] {senderMailAddr} );
// msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress( recipient));
Object recipientAddr = InternetAdressConst.newInstance( new Object[] { recipient});
SetRecipientM.invoke( msg, new Object[] { RecipientTypeTo, recipientAddr} );
//msg.setSubject(subject);
SetSubjectM.invoke( msg, new Object[] {subject} );
//msg.setText(mailBody);
SetTextM.invoke( msg, new Object[] {mailBody} );
//Transport.send(msg);
SendM.invoke( null, new Object[] {msg});
} catch (ClassNotFoundException ex ) {
throw new MailException( "Mail API not available (please provide the mailapi jars from sun in the classpath):" + ex.getMessage());
}
catch (Exception e)
{
throw new MailException( e.getMessage(), e);
}
}
/*
final PasswordAuthentication passw = new PasswordAuthentication(username, password);
Authenticator auth = new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return passw;
}
};
Session session = Session.getInstance(props, auth);
*/
public String getSmtpHost()
{
return mailhost;
}
public void setSmtpHost( String mailhost )
{
this.mailhost = mailhost;
}
public String getPassword()
{
return password;
}
public void setPassword( String password )
{
this.password = password;
}
public int getPort()
{
return port;
}
public void setPort( int port )
{
this.port = port;
}
public String getUsername()
{
return username;
}
public void setUsername( String username )
{
this.username = username;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -