- packagecom.usernet.stk.mail.util;
- importjava.util.Date;
- importjava.util.Properties;
- importjavax.mail.Authenticator;
- importjavax.mail.Message;
- importjavax.mail.MessagingException;
- importjavax.mail.PasswordAuthentication;
- importjavax.mail.Session;
- importjavax.mail.Transport;
- importjavax.mail.internet.AddressException;
- importjavax.mail.internet.InternetAddress;
- importjavax.mail.internet.MimeMessage;
- importconf.Property;
- publicclassSender{
- /**
- *@return
- *@throwsMessagingException
- *@throwsAddressException
- */
- publicstaticbooleansender(Stringto,Stringcontent)
- throwsAddressException,MessagingException{
- try{
- Propertiesprops=newProperties();
- props.put("mail.smtp.host",Property.getPara("conf","mailhost"));
- props.put("mail.smtp.auth","true");
- //session
- SessionsendMailSession=Session.getInstance(props,
- newAuthenticator(){
- publicPasswordAuthenticationgetPasswordAuthentication(){
- returnnewPasswordAuthentication(Property.getPara(
- "conf","mailaccounts"),Property.getPara(
- "conf","mailpasswod"));
- }
- });
- //message
- MessagenewMessage=newMimeMessage(sendMailSession);
- //from
- newMessage.setFrom(newInternetAddress(Property.getPara("conf",
- "from")));
- //to
- newMessage.setRecipient(Message.RecipientType.TO,
- newInternetAddress(to));
- //subject
- newMessage.setSubject(Property.getPara("conf","subject"));
- //senddate
- newMessage.setSentDate(newDate());
- //content
- newMessage.setContent(content,"text/html;charset=UTF-8");
- //newMessage.setText(content);
- //newMessage.setHeader("Content-Type","text/plain;charset=gb2312");
- //newMessage.setHeader("Content-Transfer-Encoding","8bit");
- //
- Transporttransport=sendMailSession.getTransport("smtp");
- transport.send(newMessage);
- returntrue;
- }catch(AddressExceptionae){
- ae.printStackTrace();
- returnfalse;
- }catch(MessagingExceptionme){
- me.printStackTrace();
- returnfalse;
- }
- }
- }
JavaMail发邮件
最新推荐文章于 2024-04-29 14:45:34 发布